summaryrefslogtreecommitdiffstats
path: root/src/paths/config.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2022-11-10 17:37:24 -0600
committerToby Vincent <tobyv13@gmail.com>2022-11-10 17:37:24 -0600
commit4cac42d2299cecdc3d6a7f6d7661137da338278b (patch)
tree4b1954cd32eb54b170616b41f0c43b46f6647722 /src/paths/config.rs
parentc8681e50194855a2ad36bf984866a30cfcfb8ec9 (diff)
feat: add paths module and tests
Diffstat (limited to 'src/paths/config.rs')
-rw-r--r--src/paths/config.rs107
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(())
+ });
+ }
+}