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
//! Toml config module

use crate::error::Result;
use serde_derive::{Deserialize, Serialize};
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Deserialize, Serialize)]
pub struct Config {
    pub host: String,
    pub port: usize,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            host: "127.0.0.1".to_string(),
            port: 10001,
        }
    }
}

impl Config {
    /// Parse a TOML config file.
    /// If the file can't be found, default settings are assumed and returned.
    pub fn parse_toml<P: AsRef<Path>>(toml_path: P) -> Result<Self> {
        if let Ok(mut file) = File::open(toml_path) {
            let mut contents = String::new();
            file.read_to_string(&mut contents)?;
            return Ok(toml::from_str(&contents)?);
        } else {
            Ok(Self::default())
        }
    }

    /// Write `self` to a TOML config file @ `toml_path`.
    /// If `overwrite_policy` is `OverwritePolicy::Overwrite`, the file will
    /// be written regardless of whether or not it previously existed.
    /// However, if `overwrite_policy` is `OverwritePolicy::DontOverwrite`, the
    /// file will only be written iff. it did not previously exist.
    pub fn write_toml<P: AsRef<Path>>(
        &self,
        toml_path: P,
        overwrite_policy: OverwritePolicy
    ) -> Result<()> {
        let toml_path: &Path = toml_path.as_ref();
        let mut file = File::create(toml_path)?;
        match overwrite_policy {
            OverwritePolicy::DontOverwrite if toml_path.exists() => {
                // NOP  // NOTE don't remove this branch
            },
            OverwritePolicy::DontOverwrite | OverwritePolicy::Overwrite => {
                let contents: String = toml::to_string_pretty(&self)?;
                file.write_all(contents.as_bytes())?;
            },
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Deserialize, Serialize)]
pub enum OverwritePolicy {
    DontOverwrite,
    Overwrite,
}