use crate::error::ProviderError;
use crate::types::{IpVersion, Protocol};
use std::future::Future;
use std::net::IpAddr;
use std::pin::Pin;
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 + '_>>;
}
pub type BoxedProvider = Box<dyn Provider>;
pub(crate) struct DisabledProvider(pub(crate) String);
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",
))
})
}
}