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
use std::fmt::Debug;
use std::io::Error as IoError;
use std::io::ErrorKind;
use std::io::Write;
use std::path::Path;
use std::fs::{File, read_to_string};

use tracing::debug;
use serde::{Serialize, de::DeserializeOwned};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum LoadConfigError {
    #[error("IoError: {0}")]
    IoError(IoError),
    #[error("TomlError: {0}")]
    TomlError(toml::de::Error),
}

pub trait SaveLoadConfig {
    fn save_to<T: AsRef<Path>>(&self, path: T) -> Result<(), IoError>;
    fn load_from<T: AsRef<Path>>(path: T) -> Result<Self, LoadConfigError>
    where
        Self: Sized;
    fn load_str(config: &str) -> Result<Self, LoadConfigError>
    where
        Self: Sized;
}

impl<S> SaveLoadConfig for S
where
    S: Serialize + DeserializeOwned + Debug,
{
    // save to file
    fn save_to<T: AsRef<Path>>(&self, path: T) -> Result<(), IoError> {
        let path_ref = path.as_ref();
        debug!("saving config: {:#?} to: {:#?}", self, path_ref);
        let toml = toml::to_string(self)
            .map_err(|err| IoError::new(ErrorKind::Other, format!("{err}")))?;

        let mut file = File::create(path_ref)?;
        file.write_all(toml.as_bytes())?;
        // On windows flush() is noop, but sync_all() calls FlushFileBuffers.
        file.sync_all()
    }

    fn load_from<T: AsRef<Path>>(path: T) -> Result<Self, LoadConfigError> {
        let path_ref = path.as_ref();
        debug!(?path_ref, "loading from");

        let file_str = read_to_string(path_ref).map_err(LoadConfigError::IoError)?;

        let config = toml::from_str(&file_str).map_err(LoadConfigError::TomlError)?;
        Ok(config)
    }

    fn load_str(config: &str) -> Result<Self, LoadConfigError>
    where
        Self: Sized,
    {
        let config = toml::from_str(config).map_err(LoadConfigError::TomlError)?;
        Ok(config)
    }
}