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
use std::fs;
use std::path::Path;

use serde::Deserialize;

use crate::error::DigsError;

#[derive(Deserialize, Debug)]
pub struct Server {
    pub ip: String,
    pub name: String,
}

#[derive(Deserialize, Debug)]
pub struct Config {
    pub servers: Vec<Server>,
}

/// Deserialize config intro struct.
/// # Errors
///
/// Will return `Err` if deserialization error.
/// Possibly the error contains a position (line number) of the occurred error
/// But this is not accurate. All the other apps that depend on toml.rs
/// share the same faith.
fn deserialize(content: &str) -> Result<Config, DigsError> {
    toml::from_str(content).map_err(|e| DigsError::InvalidConfig { source: e })
}

pub fn get(path: &Path) -> Result<Config, DigsError> {
    let file_content = fs::read_to_string(path)?;
    deserialize(&file_content)
}