From 5f2466e463edcf6d161f9ba6371eaf7afc3549e3 Mon Sep 17 00:00:00 2001 From: Gus Power Date: Thu, 15 May 2025 15:18:39 +0100 Subject: construct full config, use Args to load it --- src/config.rs | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) (limited to 'src/config.rs') 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 +pub struct Config { + pub networks: Vec +} + +impl Config { + + pub fn load>(path: P) -> AppResult { + 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, +pub struct WanConfig { + pub interface: Option, #[serde(flatten)] - dns_record: DnsRecord, - providers: Vec, + pub dns_record: DnsRecord, + pub providers: Vec, + #[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(()) } -- cgit v1.2.3