Skip to content

Commit

Permalink
Re-order (outer should be before inner)
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Nov 14, 2024
1 parent 14c3e85 commit 5973e07
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions crates/omnix-common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ use nix_rs::{
};
use serde::{de::DeserializeOwned, Deserialize};

/// Represents the whole configuration for `omnix` parsed from JSON
#[derive(Debug, Default, Deserialize)]
pub struct Config(BTreeMap<String, BTreeMap<String, serde_json::Value>>);

impl Config {
/// Get all the configs of type `T` for a given sub-config
/// Returns None if sub_config doesn't exist, or Some(Err) if deserialization fails
pub fn get<T>(&self, sub_config: &str) -> Option<Result<BTreeMap<String, T>, serde_json::Error>>
where
T: DeserializeOwned,
{
self.0.get(sub_config).map(|config| {
config
.iter()
.map(|(k, v)| serde_json::from_value(v.clone()).map(|value| (k.clone(), value)))
.collect()
})
}
}

/// [Config] with additional metadata about the flake URL and reference.
///
/// `reference` here is the part of the flake URL after `#`
Expand Down Expand Up @@ -92,6 +72,26 @@ impl OmConfig {
}
}

/// Represents the whole configuration for `omnix` parsed from JSON
#[derive(Debug, Default, Deserialize)]
pub struct Config(BTreeMap<String, BTreeMap<String, serde_json::Value>>);

impl Config {
/// Get all the configs of type `T` for a given sub-config
/// Returns None if sub_config doesn't exist, or Some(Err) if deserialization fails
pub fn get<T>(&self, sub_config: &str) -> Option<Result<BTreeMap<String, T>, serde_json::Error>>
where
T: DeserializeOwned,
{
self.0.get(sub_config).map(|config| {
config
.iter()
.map(|(k, v)| serde_json::from_value(v.clone()).map(|value| (k.clone(), value)))
.collect()
})
}
}

/// Error type for OmConfig
#[derive(thiserror::Error, Debug)]
pub enum OmConfigError {
Expand Down

0 comments on commit 5973e07

Please sign in to comment.