diff options
| -rw-r--r-- | src/config.rs | 11 | ||||
| -rw-r--r-- | src/dyndns_service.rs | 5 | ||||
| -rw-r--r-- | src/dyndns_service/gandi.rs | 13 | ||||
| -rw-r--r-- | src/ip_service.rs | 42 |
4 files changed, 29 insertions, 42 deletions
diff --git a/src/config.rs b/src/config.rs index 4c8bef1..2c5427e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -43,7 +43,8 @@ pub struct WanConfig { #[derive(Debug, Display, Deserialize, Serialize, PartialEq)] pub enum DnsRecordType { A, - AAAA, + #[serde(rename = "AAAA")] + Aaaa, } #[serde_as] @@ -67,8 +68,8 @@ fn default_ttl() -> u32 { #[cfg(test)] mod tests { use super::*; - use crate::dyndns_service::DynDnsProvider::GANDI; - use crate::dyndns_service::gandi::Gandi; + use crate::dyndns_service::DynDnsProvider::Gandi; + use crate::dyndns_service::gandi::GandiConfig; use crate::{assert_config_parse_error, assert_error, assert_file_not_found, assert_io_error}; use serde_json::json; use std::error::Error; @@ -95,9 +96,9 @@ mod tests { assert_eq!(wan_config.interface, None); assert_eq!(wan_config.providers.len(), 1); - let expected = Gandi::new("SOME-API-KEY".to_string()); + let expected = GandiConfig::new("SOME-API-KEY".to_string()); let actual = wan_config.providers.get(0).unwrap(); - assert_eq!(&GANDI(expected), actual); + assert_eq!(&Gandi(expected), actual); } #[test] diff --git a/src/dyndns_service.rs b/src/dyndns_service.rs index 18cec3e..a80fe4b 100644 --- a/src/dyndns_service.rs +++ b/src/dyndns_service.rs @@ -1,6 +1,6 @@ pub mod gandi; -use crate::dyndns_service::gandi::Gandi; +use crate::dyndns_service::gandi::GandiConfig; use reqwest::ClientBuilder; use serde::{Deserialize, Serialize}; use std::error::Error; @@ -8,7 +8,8 @@ use std::error::Error; #[derive(Debug, Deserialize, PartialEq, Serialize)] #[serde(tag = "type")] pub enum DynDnsProvider { - GANDI(Gandi), + #[serde(rename = "GANDI")] + Gandi(GandiConfig), } pub struct DynDnsService {} diff --git a/src/dyndns_service/gandi.rs b/src/dyndns_service/gandi.rs index 8851b2c..c8fe336 100644 --- a/src/dyndns_service/gandi.rs +++ b/src/dyndns_service/gandi.rs @@ -14,16 +14,17 @@ use serde_with::serde_as; // See https://api.gandi.net/docs/livedns/ for more info #[serde_as] #[derive(Debug, Deserialize, PartialEq, Serialize)] -pub struct Gandi { +pub struct GandiConfig { api_key: String, - #[serde(default = "Gandi::default_method")] + #[serde(default = "GandiConfig::default_method")] #[serde_as(as = "DisplayFromStr")] method: Method, - #[serde(default = "Gandi::default_record_type")] + #[serde(default = "GandiConfig::default_record_type")] record_type: DnsRecordType, } -impl Gandi { +impl GandiConfig { + #[cfg(test)] pub fn new(api_key: String) -> Self { Self { api_key, @@ -40,7 +41,7 @@ impl Gandi { } } -impl DynDnsServiceConfiguration for Gandi { +impl DynDnsServiceConfiguration for GandiConfig { fn get_service_url(&self) -> String { format!( "https://api.gandi.net/api/v5/domains/{domain}/records/{subdomain}/{record_type}", @@ -62,7 +63,7 @@ mod tests { "api_key": "SOME-API-KEY", }); - let gandi = serde_json::from_value::<Gandi>(input).unwrap(); + let gandi = serde_json::from_value::<GandiConfig>(input).unwrap(); assert_eq!(gandi.record_type, DnsRecordType::A); assert_eq!(gandi.method, Method::PUT); diff --git a/src/ip_service.rs b/src/ip_service.rs index fcf3a80..f1e3078 100644 --- a/src/ip_service.rs +++ b/src/ip_service.rs @@ -1,5 +1,5 @@ use crate::error::{AppError, AppResult}; -use crate::ip_service::IpServiceProvider::IDENTME; +use crate::ip_service::IpServiceProvider::IdentMe; use http::StatusCode; use reqwest::{Client, Url}; use serde::{Deserialize, Serialize}; @@ -11,20 +11,21 @@ use std::str::FromStr; #[derive(Debug, Deserialize, PartialEq, Serialize)] #[serde(tag = "type")] pub enum IpServiceProvider { - IDENTME(IdentMe), + #[serde(rename = "IDENTME")] + IdentMe(IdentMeConfig), } impl IpService for IpServiceProvider { async fn resolve(&self, client: &Client) -> AppResult<IpAddr> { match self { - IDENTME(ident_me) => ident_me.resolve(client).await, + IdentMe(ident_me) => ident_me.resolve(client).await, } } } impl Default for IpServiceProvider { fn default() -> Self { - IDENTME(IdentMe::default()) + IdentMe(IdentMeConfig::default()) } } @@ -32,25 +33,14 @@ pub trait IpService { async fn resolve(&self, client: &Client) -> AppResult<IpAddr>; } -// impl IpService { -// pub(crate) async fn resolve(config: &impl IpServiceConfiguration) -> Result<IpAddr, Box<dyn Error>> { -// let response = reqwest::get(config.get_service_url()).await.unwrap(); -// Ok(IpAddr::from_str(&response.text().await.unwrap())?) -// } -// } - -pub trait IpServiceConfiguration { - fn get_service_url(&self) -> Url; -} - #[serde_as] #[derive(Debug, Serialize, Deserialize, PartialEq)] -pub struct IdentMe { +pub struct IdentMeConfig { #[serde_as(as = "DisplayFromStr")] url: Url, } -impl IdentMe { +impl IdentMeConfig { fn convert_string_to_ip_address(input: String, url: &Url) -> AppResult<IpAddr> { IpAddr::from_str(&input).map_err(|e| AppError::InvalidResponse { url: url.clone(), @@ -83,7 +73,7 @@ impl IdentMe { } } -impl Default for IdentMe { +impl Default for IdentMeConfig { fn default() -> Self { Self { url: Url::parse("https://v4.ident.me/").unwrap(), @@ -91,7 +81,7 @@ impl Default for IdentMe { } } -impl IpService for IdentMe { +impl IpService for IdentMeConfig { async fn resolve(&self, client: &Client) -> AppResult<IpAddr> { let response = client .get(self.url.clone()) @@ -111,12 +101,6 @@ impl IpService for IdentMe { } } -impl IpServiceConfiguration for IdentMe { - fn get_service_url(&self) -> Url { - self.url.clone() - } -} - #[cfg(test)] mod tests { use super::*; @@ -181,9 +165,9 @@ mod tests { async fn failed_no_reachable_server() -> Result<(), Box<dyn Error>> { let client = ClientBuilder::new().build()?; let url = Url::from_str("http://localhost:8765/path")?; - let service = IdentMe { url: url.clone() }; + let service = IdentMeConfig { url: url.clone() }; - let actual = IDENTME(service).resolve(&client).await; + let actual = IdentMe(service).resolve(&client).await; assert_request_failed!(actual, url); Ok(()) @@ -253,11 +237,11 @@ mod tests { let client = ClientBuilder::new() .timeout(Duration::from_millis(100)) .build()?; - let service = IdentMe { + let service = IdentMeConfig { url: ip_service.uri().parse()?, }; - let actual = IDENTME(service).resolve(&client).await; + let actual = IdentMe(service).resolve(&client).await; Ok((ip_service, actual)) } } |
