breed_web/
config.rs

1use serde::Deserialize;
2
3use error::Error;
4
5fn defaultListenAddr() -> String
6{
7    String::from("127.0.0.1")
8}
9
10fn defaultServePath() -> String
11{
12    String::from("/")
13}
14
15fn defaultListenPort() -> u16 { 8080 }
16
17#[derive(Deserialize, Clone)]
18pub struct Configuration
19{
20    pub data_dir: String,
21    #[serde(default = "defaultListenAddr")]
22    pub listen_address: String,
23    #[serde(default = "defaultListenPort")]
24    pub listen_port: u16,
25    #[serde(default = "defaultServePath")]
26    pub serve_under_path: String,
27}
28
29impl Configuration
30{
31    pub fn fromFile(path: &str) -> Result<Self, Error>
32    {
33        let content = std::fs::read_to_string(path).map_err(
34            |_| rterr!("Failed to read config file at {}", path))?;
35        toml::from_str(&content).map_err(
36            |_| rterr!("Failed to parse config file"))
37    }
38}
39
40impl Default for Configuration
41{
42    fn default() -> Self
43    {
44        Self {
45            data_dir: String::from("."),
46            listen_address: String::from("127.0.0.1"),
47            listen_port: 8080,
48            serve_under_path: String::from("/"),
49        }
50    }
51}