1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::fmt::Display;
use std::future::Future;
use std::net::IpAddr;
use std::pin::Pin;
#[derive(Debug)]
pub enum Error {
Http(hyper::error::Error),
HttpInvalidUri(http::uri::InvalidUri),
DecodeError(std::str::Utf8Error),
InvalidAddress(std::net::AddrParseError),
Dns(trust_dns_resolver::error::ResolveError),
DnsResolutionEmpty,
#[cfg(feature = "igd")]
IgdExternalIp(igd::GetExternalIpError),
#[cfg(feature = "igd")]
IgdSearch(igd::SearchError),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Error {:?}", self)
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Http(e) => Some(e),
Error::HttpInvalidUri(e) => Some(e),
Error::DecodeError(e) => Some(e),
Error::InvalidAddress(e) => Some(e),
Error::Dns(e) => Some(e),
Error::DnsResolutionEmpty => None,
#[cfg(feature = "igd")]
Error::IgdExternalIp(e) => Some(e),
#[cfg(feature = "igd")]
Error::IgdSearch(e) => Some(e),
}
}
}
impl From<hyper::error::Error> for Error {
fn from(err: hyper::error::Error) -> Error {
Error::Http(err)
}
}
impl From<http::uri::InvalidUri> for Error {
fn from(err: http::uri::InvalidUri) -> Error {
Error::HttpInvalidUri(err)
}
}
impl From<std::str::Utf8Error> for Error {
fn from(err: std::str::Utf8Error) -> Error {
Error::DecodeError(err)
}
}
impl From<std::net::AddrParseError> for Error {
fn from(err: std::net::AddrParseError) -> Error {
Error::InvalidAddress(err)
}
}
impl From<trust_dns_resolver::error::ResolveError> for Error {
fn from(err: trust_dns_resolver::error::ResolveError) -> Error {
Error::Dns(err)
}
}
#[cfg(feature = "igd")]
impl From<igd::GetExternalIpError> for Error {
fn from(err: igd::GetExternalIpError) -> Error {
Error::IgdExternalIp(err)
}
}
#[cfg(feature = "igd")]
impl From<igd::SearchError> for Error {
fn from(err: igd::SearchError) -> Error {
Error::IgdSearch(err)
}
}
pub type IpResult = Result<IpAddr, Error>;
pub type IpFuture<'a> = Pin<Box<dyn Future<Output = IpResult> + Send + 'a>>;
#[cfg_attr(test, mockall::automock)]
pub trait Source: Display {
fn get_ip<'a>(&'a self) -> IpFuture<'a>;
fn box_clone(&self) -> Box<dyn Source>;
}
#[cfg(test)]
impl Display for MockSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "MockedSource")
}
}
impl Clone for Box<dyn Source> {
fn clone(&self) -> Box<dyn Source> {
self.box_clone()
}
}