diff --git a/cosmic-config/examples/app.rs b/cosmic-config/examples/app.rs new file mode 100644 index 00000000000..66f7a50c1c2 --- /dev/null +++ b/cosmic-config/examples/app.rs @@ -0,0 +1,27 @@ +use cosmic_config::setting::{App, Setting, AppConfig}; + +struct ExampleApp; + +impl App for ExampleApp { + const ID: &'static str = "com.Example.App"; + const VERSION: u64 = 1; +} + +struct DoFoo; + +impl Setting for DoFoo { + const NAME: &'static str = "do-foo"; + type Type = bool; +} + +struct WhatBar; + +impl Setting for WhatBar { + const NAME: &'static str = "what-bar"; + type Type = String; +} + +fn main() { + let config = AppConfig::::new().unwrap(); + config.set::(true).unwrap(); +} diff --git a/cosmic-config/src/lib.rs b/cosmic-config/src/lib.rs index 20e03a55cef..56327c43b6b 100644 --- a/cosmic-config/src/lib.rs +++ b/cosmic-config/src/lib.rs @@ -22,6 +22,8 @@ pub use cosmic_config_derive; #[cfg(feature = "calloop")] pub mod calloop; +pub mod setting; + #[derive(Debug)] pub enum Error { AtomicWrites(atomicwrites::Error), diff --git a/cosmic-config/src/setting.rs b/cosmic-config/src/setting.rs new file mode 100644 index 00000000000..445f2b5488b --- /dev/null +++ b/cosmic-config/src/setting.rs @@ -0,0 +1,36 @@ +use crate::{Config, ConfigGet, ConfigSet, Error}; + +pub trait App { + const ID: &'static str; + // XXX how to handle versioning? + const VERSION: u64; +} + +pub trait Setting { + const NAME: &'static str; + // TODO can't use &str to set? Need to serialize owned value. + type Type: serde::Serialize + serde::de::DeserializeOwned; +} + +pub struct AppConfig { + config: Config, + _app: std::marker::PhantomData, +} + +impl AppConfig { + pub fn new() -> Result { + Ok(Self { + config: Config::new(A::ID, A::VERSION)?, + _app: std::marker::PhantomData, + }) + } + + // XXX default value, if none set? + pub fn get>(&self) -> Result { + self.config.get(S::NAME) + } + + pub fn set>(&self, value: S::Type) -> Result<(), Error> { + self.config.set(S::NAME, value) + } +}