Skip to content

Commit

Permalink
fixup! Add generic config mechanism
Browse files Browse the repository at this point in the history
Simplify GetConfig response
  • Loading branch information
robin-nitrokey committed Sep 26, 2023
1 parent afe214c commit 3461dd6
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use core::str::FromStr;
use core::{
fmt::{self, Display, Formatter, Write as _},
str::FromStr,
};

use cbor_smol::{cbor_deserialize, cbor_serialize_bytes, Bytes};
use cbor_smol::{cbor_deserialize, cbor_serialize_bytes};
use littlefs2::{path, path::Path};
use serde::{de::DeserializeOwned, Serialize};
use trussed::{
Expand Down Expand Up @@ -43,6 +46,15 @@ impl<'a> ConfigValueMut<'a> {
}
}

impl<'a> Display for ConfigValueMut<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::Bool(value) => write!(f, "{}", value),
Self::U8(value) => write!(f, "{}", value),
}
}
}

#[derive(Debug)]
#[repr(u8)]
pub enum ConfigError {
Expand All @@ -69,13 +81,7 @@ pub fn get<C: Config, const N: usize>(
response: &mut Vec<u8, N>,
) -> Result<(), ConfigError> {
let field = config.field(key).ok_or(ConfigError::InvalidKey)?;
// TODO: reuse response buffer -- does not work out of the box because we have Vec<u8, _>
// and cbor-smol expects Bytes<_>
let data: Bytes<N> =
cbor_serialize_bytes(&field).map_err(|_| ConfigError::SerializationFailed)?;
response
.extend_from_slice(&data)
.map_err(|_| ConfigError::DataTooLong)
write!(response, "{}", field).map_err(|_| ConfigError::DataTooLong)
}

pub fn set<C: Config>(config: &mut C, key: &str, value: &str) -> Result<(), ConfigError> {
Expand Down

0 comments on commit 3461dd6

Please sign in to comment.