diff options
| author | Gus Power <gus@infinitesidequests.com> | 2025-05-15 15:18:39 +0100 |
|---|---|---|
| committer | Gus Power <gus@infinitesidequests.com> | 2025-05-15 15:18:39 +0100 |
| commit | 5f2466e463edcf6d161f9ba6371eaf7afc3549e3 (patch) | |
| tree | 6814ffd62c52245cc081ca6ebb419273d7b4b062 /src/config.rs | |
| parent | dd1483cb6d9c060a17dc68357975de2b1ec09c08 (diff) | |
construct full config, use Args to load it
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/src/config.rs b/src/config.rs index 9230f38..f098838 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,22 +1,47 @@ +use std::{fs, io}; +use std::path::{Path, PathBuf}; use crate::dyndns_service::DynDnsProvider; use fqdn::FQDN; use serde::{Deserialize, Serialize}; use serde_with::DisplayFromStr; use serde_with::serde_as; use strum::Display; +use crate::error::{AppError, AppResult}; +use crate::ip_service::IpServiceProvider; #[derive(Debug, Deserialize, Serialize)] #[serde(transparent)] -struct Config { - wans: Vec<WanConfig> +pub struct Config { + pub networks: Vec<WanConfig> +} + +impl Config { + + pub fn load<P: AsRef<Path>>(path: P) -> AppResult<Config> { + let path = path.as_ref(); + let content = fs::read_to_string(path) + .map_err(|e| match e.kind() { + io::ErrorKind::NotFound => AppError::FileNotFound(path.to_path_buf()), + _ => AppError::IoError(e), + })?; + + serde_json::from_str(&content) + .map_err(|e| AppError::ConfigParseError { + source: e, + path: path.to_path_buf(), + }) + } + } #[derive(Debug, Deserialize, Serialize)] -struct WanConfig { - interface: Option<String>, +pub struct WanConfig { + pub interface: Option<String>, #[serde(flatten)] - dns_record: DnsRecord, - providers: Vec<DynDnsProvider>, + pub dns_record: DnsRecord, + pub providers: Vec<DynDnsProvider>, + #[serde(default)] + pub ip_service: IpServiceProvider } #[derive(Debug, Display, Deserialize, Serialize)] @@ -107,7 +132,7 @@ mod tests { let reader = BufReader::new(file); let actual: Config = serde_json::from_reader(reader)?; - assert_eq!(actual.wans.len(), 2); + assert_eq!(actual.networks.len(), 2); } Ok(()) } |
