1use crate::error::ProviderError;
8use crate::types::{IpVersion, Protocol};
9use std::net::IpAddr;
10use std::time::Duration;
11
12#[cfg(feature = "tokio")]
13use std::future::Future;
14#[cfg(feature = "tokio")]
15use std::pin::Pin;
16
17pub trait BlockingProvider: Send + Sync {
19 fn name(&self) -> &str;
21
22 fn protocol(&self) -> Protocol;
24
25 fn supports_v4(&self) -> bool {
27 true
28 }
29
30 fn supports_v6(&self) -> bool {
32 false
33 }
34
35 fn supports_version(&self, version: IpVersion) -> bool {
37 match version {
38 IpVersion::V4 => self.supports_v4(),
39 IpVersion::V6 => self.supports_v6(),
40 IpVersion::Any => self.supports_v4() || self.supports_v6(),
41 }
42 }
43
44 fn get_ip(&self, version: IpVersion, timeout: Duration) -> Result<IpAddr, ProviderError>;
52
53 fn clone_box(&self) -> BoxedBlockingProvider;
55}
56
57pub type BoxedBlockingProvider = Box<dyn BlockingProvider>;
59
60impl Clone for BoxedBlockingProvider {
61 fn clone(&self) -> Self {
62 self.clone_box()
63 }
64}
65
66#[cfg(feature = "tokio")]
68pub trait Provider: Send + Sync {
69 fn name(&self) -> &str;
71
72 fn protocol(&self) -> Protocol;
74
75 fn supports_v4(&self) -> bool {
77 true
78 }
79
80 fn supports_v6(&self) -> bool {
82 false
83 }
84
85 fn supports_version(&self, version: IpVersion) -> bool {
87 match version {
88 IpVersion::V4 => self.supports_v4(),
89 IpVersion::V6 => self.supports_v6(),
90 IpVersion::Any => self.supports_v4() || self.supports_v6(),
91 }
92 }
93
94 fn get_ip(
96 &self,
97 version: IpVersion,
98 ) -> Pin<Box<dyn Future<Output = Result<IpAddr, ProviderError>> + Send + '_>>;
99}
100
101#[cfg(feature = "tokio")]
103pub type BoxedProvider = Box<dyn Provider>;
104
105#[derive(Clone)]
108pub(crate) struct DisabledProvider(pub(crate) String);
109
110impl BlockingProvider for DisabledProvider {
111 fn name(&self) -> &str {
112 &self.0
113 }
114
115 fn protocol(&self) -> Protocol {
116 Protocol::Http }
118
119 fn get_ip(&self, _version: IpVersion, _timeout: Duration) -> Result<IpAddr, ProviderError> {
120 Err(ProviderError::message(
121 &self.0,
122 "provider feature not enabled",
123 ))
124 }
125
126 fn clone_box(&self) -> BoxedBlockingProvider {
127 Box::new(self.clone())
128 }
129}
130
131#[cfg(feature = "tokio")]
132impl Provider for DisabledProvider {
133 fn name(&self) -> &str {
134 &self.0
135 }
136
137 fn protocol(&self) -> Protocol {
138 Protocol::Http }
140
141 fn get_ip(
142 &self,
143 _version: IpVersion,
144 ) -> Pin<Box<dyn Future<Output = Result<IpAddr, ProviderError>> + Send + '_>> {
145 let name = self.0.clone();
146 Box::pin(async move {
147 Err(ProviderError::message(
148 &name,
149 "provider feature not enabled",
150 ))
151 })
152 }
153}