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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::path::PathBuf;
use crate::errors::DiaryError;
pub struct ConfigBuilder {
diary_path: PathBuf,
prefix: String,
file_type: String,
}
impl ConfigBuilder {
fn new() -> Self {
Self {
diary_path: PathBuf::from(""),
prefix: "diary".to_string(),
file_type: "md".to_string(),
}
}
#[must_use]
pub fn diary_path(mut self, diary_path: PathBuf) -> Self {
self.diary_path = diary_path;
self
}
#[must_use]
pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
self.prefix = prefix.into();
self
}
#[must_use]
pub fn file_type(mut self, file_type: impl Into<String>) -> Self {
self.file_type = file_type.into();
self
}
pub fn build(self) -> Config {
let Self {
diary_path,
prefix,
file_type,
} = self;
Config {
diary_path,
prefix,
file_type,
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
diary_path: PathBuf,
prefix: String,
file_type: String,
}
impl Config {
pub fn builder() -> ConfigBuilder {
ConfigBuilder::new()
}
pub fn diary_path(&self) -> &PathBuf {
&self.diary_path
}
pub fn prefix(&self) -> &String {
&self.prefix
}
pub fn file_type(&self) -> &String {
&self.file_type
}
pub fn initialised(&self) -> Result<(), DiaryError> {
if self.diary_path == PathBuf::from("") {
Err(DiaryError::UnInitialised { source: None })
} else {
Ok(())
}
}
}
impl ::std::default::Default for Config {
fn default() -> Self {
ConfigBuilder::new().build()
}
}
#[derive(Default)]
pub struct ConfigManager {
config: Config,
location: Option<PathBuf>,
}
impl ConfigManager {
pub fn location(&self) -> &Option<PathBuf> {
&self.location
}
pub fn config(&self) -> &Config {
&self.config
}
pub fn with_location(location: Option<PathBuf>) -> ConfigManager {
ConfigManager {
location,
..Default::default()
}
}
pub fn read(mut self) -> Result<ConfigManager, confy::ConfyError> {
let config: Config = match &self.location {
Some(l) => confy::load_path(l)?,
_ => confy::load("diary")?,
};
self.config = config;
Ok(self)
}
pub fn write(self) -> Result<(), confy::ConfyError> {
match self.location {
Some(l) => confy::store_path(l, self.config),
_ => confy::store("diary", self.config),
}
}
#[must_use]
pub fn update_config(mut self, config: Config) -> ConfigManager {
self.config = config;
self
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::{Config, ConfigManager};
#[test]
fn full_config_build() {
let cfg = Config::builder()
.diary_path(PathBuf::from("/home/"))
.prefix("dy")
.build();
assert_eq!(cfg.prefix(), "dy");
assert_eq!(cfg.diary_path(), &PathBuf::from("/home/"))
}
#[test]
fn config_manager_with_location() {
let location = Some(PathBuf::from("/tmp/"));
let cfg_manager = ConfigManager::with_location(location.clone());
assert!(cfg_manager.location().clone() == location)
}
}