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
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[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 {
pub fn builder() -> Builder {
Builder {
service_name: None,
ipv4: None,
ipv6: None,
port: None,
}
}
pub fn service_name(&self) -> Option<&str> {
self.service_name.as_ref().map(|s| &**s)
}
pub fn ipv4(&self) -> Option<Ipv4Addr> {
self.ipv4
}
pub fn ipv6(&self) -> Option<Ipv6Addr> {
self.ipv6
}
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 Builder {
pub fn service_name(&mut self, service_name: &str) -> &mut Builder {
self.service_name = Some(service_name.to_string());
self
}
pub fn ipv4(&mut self, ipv4: Ipv4Addr) -> &mut Builder {
self.ipv4 = Some(ipv4);
self
}
pub fn ipv6(&mut self, ipv6: Ipv6Addr) -> &mut Builder {
self.ipv6 = Some(ipv6);
self
}
pub fn ip(&mut self, ip: IpAddr) -> &mut Builder {
match ip {
IpAddr::V4(addr) => self.ipv4(addr),
IpAddr::V6(addr) => self.ipv6(addr),
}
}
pub fn port(&mut self, port: u16) -> &mut Builder {
self.port = Some(port);
self
}
pub fn build(&self) -> Endpoint {
Endpoint {
service_name: self.service_name.clone(),
ipv4: self.ipv4,
ipv6: self.ipv6,
port: self.port,
}
}
}