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
use crate::Result;
use assert_fs::TempDir;
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::path::PathBuf;
use valgrind::Protocol;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Config {
    pub port: u16,
    pub token: Option<String>,
    pub working_directory: PathBuf,
    pub protocol: Protocol,
    pub local: bool,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            port: 7070,
            token: None,
            working_directory: TempDir::new().unwrap().into_persistent().to_path_buf(),
            protocol: Default::default(),
            local: true,
        }
    }
}

pub fn read_config<P: AsRef<Path>>(config: P) -> Result<Config> {
    let config = config.as_ref();
    if !config.exists() {
        return Err(crate::error::Error::CannotFindConfig(config.to_path_buf()));
    }

    let contents = std::fs::read_to_string(config)?;
    serde_json::from_str(&contents).map_err(Into::into)
}