diff options
| author | Gus Power <gus@infinitesidequests.com> | 2025-05-20 17:39:48 +0100 |
|---|---|---|
| committer | Gus Power <gus@infinitesidequests.com> | 2025-05-20 17:39:48 +0100 |
| commit | 05157f8d5ba321a8886505a086db9f9f26d44ffe (patch) | |
| tree | 7642dbd6a7d0fb57f80ddc6de029119c5db3abe6 /src/config.rs | |
| parent | 5c5e7c59f9ae8932eb87d00ec7e4fea389faffde (diff) | |
added some test macros to reduce noise in ipservice tests
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 75 |
1 files changed, 47 insertions, 28 deletions
diff --git a/src/config.rs b/src/config.rs index 88fb280..4c8bef1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,37 +1,33 @@ -use std::{fs, io}; -use std::path::Path; use crate::dyndns_service::DynDnsProvider; +use crate::error::{AppError, AppResult}; +use crate::ip_service::IpServiceProvider; use fqdn::FQDN; use serde::{Deserialize, Serialize}; use serde_with::DisplayFromStr; use serde_with::serde_as; +use std::path::Path; +use std::{fs, io}; use strum::Display; -use crate::error::{AppError, AppResult}; -use crate::ip_service::IpServiceProvider; #[derive(Debug, Deserialize, Serialize)] #[serde(transparent)] pub struct Config { - pub networks: Vec<WanConfig> + 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(), - }) + 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)] @@ -41,14 +37,13 @@ pub struct WanConfig { pub dns_record: DnsRecord, pub providers: Vec<DynDnsProvider>, #[serde(default)] - pub ip_service: IpServiceProvider + pub ip_service: IpServiceProvider, } -#[derive(Debug, Display, Deserialize, Serialize)] -#[derive(PartialEq)] +#[derive(Debug, Display, Deserialize, Serialize, PartialEq)] pub enum DnsRecordType { A, - AAAA + AAAA, } #[serde_as] @@ -71,14 +66,15 @@ fn default_ttl() -> u32 { #[cfg(test)] mod tests { - use std::error::Error; - use std::fs::{read_dir, File}; - use std::io::BufReader; use super::*; - use serde_json::json; - use std::str::FromStr; use crate::dyndns_service::DynDnsProvider::GANDI; use crate::dyndns_service::gandi::Gandi; + use crate::{assert_config_parse_error, assert_error, assert_file_not_found, assert_io_error}; + use serde_json::json; + use std::error::Error; + use std::fs::{File, read_dir}; + use std::io::BufReader; + use std::str::FromStr; #[test] fn check_minimal_config() { @@ -120,7 +116,7 @@ mod tests { #[test] fn check_file_configs() -> Result<(), Box<dyn Error>> { - let path = std::path::Path::new("test"); + let path = Path::new("test"); for entry in read_dir(path)? { let entry = entry?; let test_file_path = entry.path(); @@ -137,4 +133,27 @@ mod tests { Ok(()) } + #[test] + fn check_missing_config() -> Result<(), Box<dyn Error>> { + let path = Path::new("test/unknown.yaml"); + + assert_file_not_found!(Config::load(path), path); + Ok(()) + } + + #[test] + fn check_broken_config() -> Result<(), Box<dyn Error>> { + let path = Path::new("test/config.bork"); + + assert_config_parse_error!(Config::load(path), path); + Ok(()) + } + + #[test] + fn check_inaccessible_config() -> Result<(), Box<dyn Error>> { + let path = Path::new("/root/secure-config.json"); + + assert_io_error!(Config::load(path)); + Ok(()) + } } |
