Skip to main content

pim_core/config/
mod.rs

1//! Configuration structures shared by the CLI and daemon.
2
3mod defaults;
4mod model;
5mod peer;
6mod peer_cleanup;
7
8#[cfg(test)]
9mod tests;
10
11pub use model::*;
12pub use peer::*;
13pub use peer_cleanup::PeerCleanupConfig;
14
15use std::path::Path;
16use std::str::FromStr;
17
18use crate::PimError;
19
20impl Config {
21    /// Load configuration from a TOML file.
22    pub fn load(path: &Path) -> Result<Self, PimError> {
23        let content = std::fs::read_to_string(path).map_err(PimError::Io)?;
24        content.parse()
25    }
26
27    /// Parse configuration from a TOML string.
28    pub fn from_toml_str(s: &str) -> Result<Self, PimError> {
29        s.parse()
30    }
31
32    /// Serialize configuration to a TOML string.
33    pub fn to_toml_string(&self) -> Result<String, PimError> {
34        toml::to_string_pretty(self).map_err(|e| PimError::Config(e.to_string()))
35    }
36}
37
38impl FromStr for Config {
39    type Err = PimError;
40
41    fn from_str(s: &str) -> Result<Self, Self::Err> {
42        toml::from_str(s).map_err(|e| PimError::Config(e.to_string()))
43    }
44}