scratchstack_config/
config.rs

1use {
2    crate::{ConfigError, ServiceConfig},
3    serde::Deserialize,
4    std::{fmt::Debug, fs::File, io::Read, path::Path},
5    toml::from_slice as toml_from_slice,
6};
7
8/// The configuration data for the server, as specified by the user. This allows for optional fields and references
9/// to files for things like TLS certificates and keys.
10#[derive(Debug, Deserialize)]
11pub struct Config {
12    pub service: Option<ServiceConfig>,
13}
14
15impl Config {
16    pub fn read_file<P: AsRef<Path>>(path: P) -> Result<Self, ConfigError> {
17        match File::open(path) {
18            Err(e) => Err(ConfigError::IO(e)),
19            Ok(mut file) => {
20                let metadata = file.metadata()?;
21                let mut raw = Vec::with_capacity(metadata.len() as usize);
22                file.read_to_end(&mut raw)?;
23                toml_from_slice(&raw).map_err(Into::into)
24            }
25        }
26    }
27}