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/ip_service.rs | |
| parent | dd1483cb6d9c060a17dc68357975de2b1ec09c08 (diff) | |
construct full config, use Args to load it
Diffstat (limited to 'src/ip_service.rs')
| -rw-r--r-- | src/ip_service.rs | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/src/ip_service.rs b/src/ip_service.rs index 8e2bee8..ee1a092 100644 --- a/src/ip_service.rs +++ b/src/ip_service.rs @@ -1,12 +1,28 @@ +use serde_with::DisplayFromStr; use reqwest::Url; use std::error::Error; use std::net::IpAddr; use std::str::FromStr; +use serde::{Deserialize, Serialize}; +use serde_with::serde_as; +use crate::ip_service::IpServiceProvider::IDENTME; + +#[derive(Debug, Deserialize, PartialEq, Serialize)] +#[serde(tag = "type")] +pub enum IpServiceProvider { + IDENTME(IdentMe) +} + +impl Default for IpServiceProvider { + fn default() -> Self { + IDENTME(IdentMe::default()) + } +} pub struct IpService {} impl IpService { - async fn resolve(config: &impl IpServiceConfiguration) -> Result<IpAddr, Box<dyn Error>> { + 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())?) } @@ -16,6 +32,25 @@ pub trait IpServiceConfiguration { fn get_service_url(&self) -> Url; } +#[serde_as] +#[derive(Debug, Serialize, Deserialize, PartialEq)] +pub struct IdentMe { + #[serde_as(as = "DisplayFromStr")] + url: Url +} + +impl Default for IdentMe { + fn default() -> Self { + Self { url: Url::parse("https://v4.ident.me/").unwrap() } + } +} + +impl IpServiceConfiguration for IdentMe { + fn get_service_url(&self) -> Url { + self.url.clone() + } +} + #[cfg(test)] mod tests { use super::*; |
