diff options
Diffstat (limited to 'src/paths/config.rs')
-rw-r--r-- | src/paths/config.rs | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/paths/config.rs b/src/paths/config.rs new file mode 100644 index 0000000..72c5da7 --- /dev/null +++ b/src/paths/config.rs @@ -0,0 +1,107 @@ +use figment::{providers::Serialized, value, Figment, Metadata, Profile, Provider}; +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; + +#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)] +pub struct Config { + pub(crate) paths: Vec<PathEntry>, +} + +impl Config { + pub fn from<T: Provider>(provider: T) -> figment::error::Result<Config> { + Figment::from(provider).extract() + } +} + +impl Provider for Config { + fn metadata(&self) -> Metadata { + Metadata::named("Tmuxr path config") + } + + fn data(&self) -> figment::error::Result<value::Map<Profile, value::Dict>> { + Serialized::defaults(Config::default()).data() + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)] +pub struct PathEntry { + pub path: PathBuf, + pub hidden: bool, + pub recurse: Option<usize>, +} + +impl std::str::FromStr for PathEntry { + type Err = std::convert::Infallible; + + fn from_str(s: &str) -> Result<Self, Self::Err> { + Ok(PathEntry { + path: s.to_string().into(), + ..Default::default() + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use figment::providers::{Format, Serialized, Toml}; + + #[test] + fn defaults() { + figment::Jail::expect_with(|jail| { + jail.create_file( + "tmuxr.toml", + r#" + paths = [] + "#, + )?; + + let config: Config = Figment::from(Serialized::defaults(Config::default())) + .merge(Toml::file("tmuxr.toml")) + .extract()?; + + assert_eq!(config, Config::default()); + + Ok(()) + }); + } + + #[test] + fn custom() { + figment::Jail::expect_with(|jail| { + jail.create_file( + "tmuxr.toml", + r#" + paths = [ + "/tmp/projects", + { path = "/tmp/tmuxr/test/other_projects", recursive = false, hidden = true }, + ] + "#, + )?; + + let config: Config = Figment::from(Serialized::defaults(Config::default())) + .merge(Toml::file("tmuxr.toml")) + .extract()?; + + assert_eq!( + config, + Config { + paths: Vec::from([ + PathEntry { + path: "/tmp/tmuxr/test/project_1".into(), + hidden: false, + recurse: None, + }, + PathEntry { + path: "/tmp/tmuxr/test/projects".into(), + hidden: false, + recurse: Some(1), + } + ]), + } + ); + + Ok(()) + }); + } +} |