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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct Endpoint {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
service_name: Option<String>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
ipv4: Option<Ipv4Addr>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
ipv6: Option<Ipv6Addr>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
port: Option<u16>,
}
impl Endpoint {
#[inline]
pub fn builder() -> Builder {
Builder {
service_name: None,
ipv4: None,
ipv6: None,
port: None,
}
}
#[inline]
pub fn service_name(&self) -> Option<&str> {
self.service_name.as_ref().map(|s| &**s)
}
#[inline]
pub fn ipv4(&self) -> Option<Ipv4Addr> {
self.ipv4
}
#[inline]
pub fn ipv6(&self) -> Option<Ipv6Addr> {
self.ipv6
}
#[inline]
pub fn port(&self) -> Option<u16> {
self.port
}
}
pub struct Builder {
service_name: Option<String>,
ipv4: Option<Ipv4Addr>,
ipv6: Option<Ipv6Addr>,
port: Option<u16>,
}
impl From<Endpoint> for Builder {
#[inline]
fn from(e: Endpoint) -> Builder {
Builder {
service_name: e.service_name,
ipv4: e.ipv4,
ipv6: e.ipv6,
port: e.port,
}
}
}
impl Builder {
#[inline]
pub fn service_name(&mut self, service_name: &str) -> &mut Builder {
self.service_name = Some(service_name.to_string());
self
}
#[inline]
pub fn ipv4(&mut self, ipv4: Ipv4Addr) -> &mut Builder {
self.ipv4 = Some(ipv4);
self
}
#[inline]
pub fn ipv6(&mut self, ipv6: Ipv6Addr) -> &mut Builder {
self.ipv6 = Some(ipv6);
self
}
#[inline]
pub fn ip(&mut self, ip: IpAddr) -> &mut Builder {
match ip {
IpAddr::V4(addr) => self.ipv4(addr),
IpAddr::V6(addr) => self.ipv6(addr),
}
}
#[inline]
pub fn port(&mut self, port: u16) -> &mut Builder {
self.port = Some(port);
self
}
#[inline]
pub fn build(&self) -> Endpoint {
Endpoint {
service_name: self.service_name.clone(),
ipv4: self.ipv4,
ipv6: self.ipv6,
port: self.port,
}
}
}