diff options
Diffstat (limited to 'src/ip_service.rs')
| -rw-r--r-- | src/ip_service.rs | 42 |
1 files changed, 13 insertions, 29 deletions
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)) } } |
