config-resolver 0.2.1

A modular configuration resolver for auto-detecting and resolving runtime values (IPs, ports, peer IDs)
Documentation
use crate::error::Error;
use crate::providers::ConfigProvider;
use crate::types::ResolvedValue;
use async_trait::async_trait;
use std::net::IpAddr;

/// A provider that resolves the public IP address using the external `api.ipify.org` service.
pub struct IpifyProvider;

#[async_trait]
impl ConfigProvider for IpifyProvider {
    fn name(&self) -> &str {
        "IPIFY"
    }

    async fn resolve(&self) -> Result<ResolvedValue, Error> {
        let res = reqwest::get("https://api.ipify.org").await?.text().await?;
        let ip = res
            .trim()
            .parse::<IpAddr>()
            .map_err(|e| Error::ParseError(e.to_string()))?;
        Ok(ResolvedValue::Ip(ip))
    }

    fn resolve_sync(&self) -> Result<ResolvedValue, Error> {
        let res = reqwest::blocking::get("https://api.ipify.org")?.text()?;
        let ip = res
            .trim()
            .parse::<IpAddr>()
            .map_err(|e| Error::ParseError(e.to_string()))?;
        Ok(ResolvedValue::Ip(ip))
    }
}