Skip to content

Commit

Permalink
Merge pull request #18 from Nitrokey/config-error
Browse files Browse the repository at this point in the history
Refactor App constructors
  • Loading branch information
robin-nitrokey authored Dec 1, 2023
2 parents 8d4aa58 + 7ecc984 commit 75c7c96
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,46 +188,58 @@ pub struct App<T, R, S, C = ()> {
config: C,
}

impl<T, R, S> App<T, R, S, ()>
impl<T, R, S, C> App<T, R, S, C>
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
C: Config,
{
/// Create an admin app instance without the configuration mechanism.
///
/// This is only intended for testing and example code. Firmware runners should use
/// [`App::load`][].
pub fn without_config(
/// Create an admin app instance, loading the configuration from the filesystem.
pub fn load_config<F: Filestore>(
client: T,
filestore: &mut F,
uuid: [u8; 16],
version: u32,
full_version: &'static str,
status: S,
) -> Self {
Self::new(client, uuid, version, full_version, status, ())
) -> Result<Self, (T, ConfigError)> {
match config::load(filestore) {
Ok(config) => Ok(Self::new(
client,
uuid,
version,
full_version,
status,
config,
)),
Err(err) => {
error!("failed to load configuration: {:?}", err);
Err((client, err))
}
}
}
}

impl<T, R, S, C> App<T, R, S, C>
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
C: Config,
{
/// Create an admin app instance, loading the configuration from the filesystem.
pub fn load<F: Filestore>(
/// Create an admin app instance without the configuration mechanism, using the default config
/// values.
///
/// This is only intended for debugging, testing and example code. In production,
/// [`App::load_config`][] should be used.
pub fn with_default_config(
client: T,
filestore: &mut F,
uuid: [u8; 16],
version: u32,
full_version: &'static str,
status: S,
) -> Self {
config::load(filestore)
.map(|config| Self::new(client, uuid, version, full_version, status, config))
.unwrap()
Self::new(
client,
uuid,
version,
full_version,
status,
Default::default(),
)
}

fn new(
Expand Down

0 comments on commit 75c7c96

Please sign in to comment.