aboutsummaryrefslogtreecommitdiff
path: root/src/ip_service.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/ip_service.rs')
-rw-r--r--src/ip_service.rs37
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::*;