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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use std::str::FromStr;
use std::num::ParseIntError;
use std::net::{IpAddr, SocketAddr};
use abstract_ns;
use abstract_ns::name::{self, Name};
use quick_error::ResultExt;
quick_error! {
#[derive(Debug)]
pub enum Error {
Name(name: String, err: name::Error) {
cause(err)
context(name: &'a str, err: name::Error)
-> (name.to_string(), err)
}
Port(name: String, err: ParseIntError) {
cause(err)
context(name: &'a str, err: ParseIntError)
-> (name.to_string(), err)
}
}
}
#[derive(Debug)]
pub enum AutoName<'a> {
Auto(&'a str),
HostPort(&'a str, u16),
HostDefaultPort(&'a str),
Service(&'a str),
IpAddr(IpAddr),
SocketAddr(SocketAddr),
}
pub trait IntoNameIter<'a> {
type Item: Into<AutoName<'a>>;
type IntoIter: Iterator<Item=Self::Item>;
fn into_name_iter(&'a self) -> Self::IntoIter;
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub(crate) enum InternalName {
HostPort(Name, u16),
Service(Name),
Addr(SocketAddr),
}
impl<'a> AutoName<'a> {
pub(crate) fn parse(&self, default_port: u16)
-> Result<InternalName, Error>
{
use self::AutoName as A;
use self::InternalName as I;
match *self {
A::Auto(x) => {
if let Ok(ip) = x.parse() {
Ok(I::Addr(SocketAddr::new(ip, default_port)))
} else if let Ok(sa) = x.parse() {
Ok(I::Addr(sa))
} else if x.starts_with("_") {
Ok(I::Service(Name::from_str(x).context(x)?))
} else if let Some(pos) = x.find(':') {
Ok(I::HostPort(Name::from_str(&x[..pos]).context(x)?,
x[pos+1..].parse().context(x)?))
} else {
Ok(I::HostPort(Name::from_str(x).context(x)?,
default_port))
}
}
A::HostPort(name, port)
=> Ok(I::HostPort(Name::from_str(name).context(name)?, port)),
A::HostDefaultPort(name)
=> Ok(I::HostPort(Name::from_str(name).context(name)?, default_port)),
A::Service(name)
=> Ok(I::Service(Name::from_str(name).context(name)?)),
A::IpAddr(ip) => Ok(I::Addr(SocketAddr::new(ip, default_port))),
A::SocketAddr(sa) => Ok(I::Addr(sa)),
}
}
}
impl<'a, T: AsRef<str> + 'a> From<&'a T> for AutoName<'a> {
fn from(val: &'a T) -> AutoName<'a> {
AutoName::Auto(val.as_ref())
}
}
impl<'a> From<&'a str> for AutoName<'a> {
fn from(val: &'a str) -> AutoName<'a> {
AutoName::Auto(val)
}
}
impl<'a, T: 'a> IntoNameIter<'a> for T
where &'a T: IntoIterator,
<&'a T as IntoIterator>::Item: Into<AutoName<'a>>,
{
type Item = <&'a T as IntoIterator>::Item;
type IntoIter = <&'a T as IntoIterator>::IntoIter;
fn into_name_iter(&'a self) -> Self::IntoIter {
self.into_iter()
}
}
impl Into<abstract_ns::Error> for Error {
fn into(self) -> abstract_ns::Error {
match self {
Error::Name(name, _) => {
abstract_ns::Error::InvalidName(name, "bad name")
}
Error::Port(name, _) => {
abstract_ns::Error::InvalidName(name, "bad port number")
}
}
}
}
#[cfg(test)]
mod test {
use abstract_ns::Name;
use super::AutoName as A;
use super::InternalName as I;
fn name(name: &str) -> Name {
name.parse().unwrap()
}
#[test]
fn auto() {
assert_eq!(A::Auto("localhost").parse(1234).unwrap(),
I::HostPort(name("localhost"), 1234));
assert_eq!(A::Auto("localhost:8080").parse(1234).unwrap(),
I::HostPort(name("localhost"), 8080));
assert_eq!(A::Auto("_my._svc.localhost").parse(1234).unwrap(),
I::Service(name("_my._svc.localhost")));
}
#[test]
#[should_panic(expected="InvalidChar")]
fn bad_names() {
A::Auto("_my._svc.localhost:8080").parse(1234).unwrap();
}
}