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
use std::collections::HashMap;
use serde_json;
use config::{Source, Value, ConfigError};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Settings {
pub theme: String,
pub site_name: String,
pub site_motto: String,
pub footer_note: String,
pub media_dir: String,
pub build_dir: String,
pub theme_root_dir: String,
pub rebuild_interval: u8,
pub url_prefix: String,
pub posts_per_page: usize,
}
impl Default for Settings {
fn default() -> Self {
return Settings {
theme: String::from("simple"),
site_name: String::from("Mdblog"),
site_motto: String::from("Simple is Beautiful!"),
footer_note: String::from("Keep It Simple, Stupid!"),
media_dir: String::from("media"),
build_dir: String::from("_build"),
theme_root_dir: String::from("_theme"),
rebuild_interval: 2,
url_prefix: Default::default(),
posts_per_page: 20,
}
}
}
impl Source for Settings {
fn clone_into_box(&self) -> Box<Source + Send + Sync> {
Box::new((*self).clone())
}
fn collect(&self) -> Result<HashMap<String, Value>, ConfigError> {
let serialized = serde_json::to_string(&self)
.expect("settings serialized error");
let map = serde_json::from_str::<HashMap<String, Value>>(&serialized)
.expect("settings deserialized error");
Ok(map)
}
}