1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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(())
});
}
}
|