use crate::error::ProviderError;
use crate::types::{IpVersion, Protocol};
use std::net::IpAddr;
use std::time::Duration;
#[cfg(feature = "tokio")]
use std::future::Future;
#[cfg(feature = "tokio")]
use std::pin::Pin;
pub trait BlockingProvider: Send + Sync {
fn name(&self) -> &str;
fn protocol(&self) -> Protocol;
fn supports_v4(&self) -> bool {
true
}
fn supports_v6(&self) -> bool {
false
}
fn supports_version(&self, version: IpVersion) -> bool {
match version {
IpVersion::V4 => self.supports_v4(),
IpVersion::V6 => self.supports_v6(),
IpVersion::Any => self.supports_v4() || self.supports_v6(),
}
}
fn get_ip(&self, version: IpVersion, timeout: Duration) -> Result<IpAddr, ProviderError>;
fn clone_box(&self) -> BoxedBlockingProvider;
}
pub type BoxedBlockingProvider = Box<dyn BlockingProvider>;
impl Clone for BoxedBlockingProvider {
fn clone(&self) -> Self {
self.clone_box()
}
}
#[cfg(feature = "tokio")]
pub trait Provider: Send + Sync {
fn name(&self) -> &str;
fn protocol(&self) -> Protocol;
fn supports_v4(&self) -> bool {
true
}
fn supports_v6(&self) -> bool {
false
}
fn supports_version(&self, version: IpVersion) -> bool {
match version {
IpVersion::V4 => self.supports_v4(),
IpVersion::V6 => self.supports_v6(),
IpVersion::Any => self.supports_v4() || self.supports_v6(),
}
}
fn get_ip(
&self,
version: IpVersion,
) -> Pin<Box<dyn Future<Output = Result<IpAddr, ProviderError>> + Send + '_>>;
}
#[cfg(feature = "tokio")]
pub type BoxedProvider = Box<dyn Provider>;
#[derive(Clone)]
pub(crate) struct DisabledProvider(pub(crate) String);
impl BlockingProvider for DisabledProvider {
fn name(&self) -> &str {
&self.0
}
fn protocol(&self) -> Protocol {
Protocol::Http }
fn get_ip(&self, _version: IpVersion, _timeout: Duration) -> Result<IpAddr, ProviderError> {
Err(ProviderError::message(
&self.0,
"provider feature not enabled",
))
}
fn clone_box(&self) -> BoxedBlockingProvider {
Box::new(self.clone())
}
}
#[cfg(feature = "tokio")]
impl Provider for DisabledProvider {
fn name(&self) -> &str {
&self.0
}
fn protocol(&self) -> Protocol {
Protocol::Http }
fn get_ip(
&self,
_version: IpVersion,
) -> Pin<Box<dyn Future<Output = Result<IpAddr, ProviderError>> + Send + '_>> {
let name = self.0.clone();
Box::pin(async move {
Err(ProviderError::message(
&name,
"provider feature not enabled",
))
})
}
}