aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs109
1 files changed, 38 insertions, 71 deletions
diff --git a/src/config.rs b/src/config.rs
index e73b7e1..cc1f28f 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,4 +1,3 @@
-use figment::{providers::Serialized, value, Figment, Metadata, Profile, Provider};
use serde::{Deserialize, Serialize};
use std::{convert::Infallible, path::PathBuf, str::FromStr};
@@ -6,34 +5,12 @@ use std::{convert::Infallible, path::PathBuf, str::FromStr};
#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
pub struct Config {
#[serde_as(as = "Vec<serde_with::PickFirst<(_, serde_with::DisplayFromStr)>>")]
- pub(crate) paths: Vec<Entry>,
-}
-
-impl Config {
- // Extract the configuration from any `Provider`
- pub fn extract<T: Provider>(provider: T) -> figment::error::Result<Config> {
- Figment::from(provider).extract()
- }
-
- // Provide a default provider, a `Figment`.
- pub fn figment() -> Figment {
- Figment::from(Config::default())
- }
-}
-
-impl Provider for Config {
- fn metadata(&self) -> Metadata {
- Metadata::named("Projectr config")
- }
-
- fn data(&self) -> figment::error::Result<value::Map<Profile, value::Dict>> {
- Serialized::defaults(self).data()
- }
+ pub paths: Vec<SearchEntryConfig>,
}
#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
-pub struct Entry {
+pub struct SearchEntryConfig {
pub path_buf: PathBuf,
pub hidden: bool,
pub max_depth: Option<usize>,
@@ -42,7 +19,7 @@ pub struct Entry {
pub git: bool,
}
-impl Entry {
+impl SearchEntryConfig {
pub fn new(path_buf: PathBuf) -> Self {
Self {
path_buf,
@@ -51,13 +28,13 @@ impl Entry {
}
}
-impl From<PathBuf> for Entry {
+impl From<PathBuf> for SearchEntryConfig {
fn from(path_buf: PathBuf) -> Self {
Self::new(path_buf)
}
}
-impl FromStr for Entry {
+impl FromStr for SearchEntryConfig {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
@@ -71,54 +48,44 @@ impl FromStr for Entry {
#[cfg(test)]
mod tests {
use super::*;
- use figment::providers::{Format, Serialized, Toml};
use pretty_assertions::assert_eq;
#[test]
fn test_extract_config() {
- figment::Jail::expect_with(|jail| {
- jail.create_file(
- "file.toml",
- r#"
- paths = [
- "/path/to/projects",
- { path_buf = "/path/to/other_projects", hidden = true, max_depth = 1 },
- { path_buf = "/path/to/another_project", max_depth = 0 }
- ]
- "#,
- )?;
-
- let config: Config = Figment::from(Serialized::defaults(Config::default()))
- .merge(Toml::file("file.toml"))
- .extract()?;
+ let s = r#"
+ paths = [
+ "/path/to/projects",
+ { path_buf = "/path/to/other_projects", hidden = true, max_depth = 1 },
+ { path_buf = "/path/to/another_project", max_depth = 0 }
+ ]
+ "#;
- assert_eq!(
- config,
- Config {
- paths: Vec::from([
- Entry {
- path_buf: "/path/to/projects".into(),
- hidden: false,
- max_depth: None,
- ..Default::default()
- },
- Entry {
- path_buf: "/path/to/other_projects".into(),
- hidden: true,
- max_depth: Some(1),
- ..Default::default()
- },
- Entry {
- path_buf: "/path/to/another_project".into(),
- hidden: false,
- max_depth: Some(0),
- ..Default::default()
- },
- ]),
- }
- );
+ let config: Config = toml::from_str(s).unwrap();
- Ok(())
- });
+ assert_eq!(
+ config,
+ Config {
+ paths: Vec::from([
+ SearchEntryConfig {
+ path_buf: "/path/to/projects".into(),
+ hidden: false,
+ max_depth: None,
+ ..Default::default()
+ },
+ SearchEntryConfig {
+ path_buf: "/path/to/other_projects".into(),
+ hidden: true,
+ max_depth: Some(1),
+ ..Default::default()
+ },
+ SearchEntryConfig {
+ path_buf: "/path/to/another_project".into(),
+ hidden: false,
+ max_depth: Some(0),
+ ..Default::default()
+ },
+ ]),
+ }
+ );
}
}