Skip to content

Commit

Permalink
Improve config field metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Oct 4, 2024
1 parent e4d636f commit 6f6a1dd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ where
response.push(status).ok();
}
Command::ListAvailableConfig => {
cbor_serialize_to(&self.config.list_available_config(), response).ok();
cbor_serialize_to(&self.config.list_available_fields(), response).ok();
return Ok(());
}
#[cfg(feature = "factory-reset")]
Expand Down
47 changes: 43 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use trussed::{
///
/// It is expected to have one such structure for each application supporting factory-reset by the admin-app
///
/// ```rust
/// ```rust,ignore
///# use admin_app::{ResetSignalAllocation, ConfigValueMut};
///# use littlefs2::{path::Path, path};
/// #[derive(Default, PartialEq, serde::Deserialize, serde::Serialize)]
Expand Down Expand Up @@ -164,18 +164,33 @@ pub trait Config: Default + PartialEq + DeserializeOwned + Serialize {
/// Return false if the configuration does not support storing the migration version
fn set_migration_version(&mut self, _version: u32) -> bool;

fn list_available_config(&self) -> &'static [ConfigField];
fn list_available_fields(&self) -> &'static [ConfigField];
}

// No need to rename, cbor-smol already packs enum using ids
#[derive(Serialize)]
#[non_exhaustive]
pub enum FieldType {
Bool,
U8,
}

#[derive(Serialize)]
pub struct ConfigField {
#[serde(rename = "n")]
pub name: &'static str,
/// Changing the config field requires a touch
pub requires_touch: bool,
#[serde(rename = "c")]
pub requires_touch_confirmation: bool,
/// Changing the config field requires a power cycle
#[serde(rename = "r")]
pub requires_reboot: bool,
/// Changing the config field deletes data
#[serde(rename = "d")]
pub destructive: bool,
/// The type of data stored in this field
#[serde(rename = "t")]
pub ty: FieldType,
}

impl Config for () {
Expand All @@ -195,7 +210,7 @@ impl Config for () {
false
}

fn list_available_config(&self) -> &'static [ConfigField] {
fn list_available_fields(&self) -> &'static [ConfigField] {
&[]
}
}
Expand Down Expand Up @@ -330,3 +345,27 @@ fn load_if_exists<F: Filestore>(
}
})
}

#[cfg(test)]
mod tests {
use hex_literal::hex;

use super::*;

#[test]
fn config_field() {
let fields = &[ConfigField {
name: "test_name",
requires_touch_confirmation: true,
requires_reboot: false,
destructive: true,
ty: FieldType::Bool,
}];
let mut bytes: trussed::types::Vec<u8, 100> = Default::default();
cbor_smol::cbor_serialize_to(fields, &mut bytes).unwrap();
assert_eq!(
&bytes,
&hex!("81A5616E69746573745F6E616D656163F56172F46164F5617400")
);
}
}

0 comments on commit 6f6a1dd

Please sign in to comment.