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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use super::SetOpt;
use curl::easy::{Easy2, List};
use http::Uri;
use std::{
convert::TryFrom,
fmt,
net::SocketAddr,
str::FromStr,
};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DialerParseError(());
impl fmt::Display for DialerParseError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.write_str("invalid dial address syntax")
}
}
impl std::error::Error for DialerParseError {}
#[derive(Clone, Debug)]
pub struct Dialer(Inner);
#[derive(Clone, Debug, Eq, PartialEq)]
enum Inner {
Default,
IpSocket(String),
#[cfg(unix)]
UnixSocket(std::path::PathBuf),
}
impl Dialer {
pub fn ip_socket(addr: impl Into<SocketAddr>) -> Self {
Self(Inner::IpSocket(format!("::{}", addr.into())))
}
#[cfg(unix)]
pub fn unix_socket(path: impl Into<std::path::PathBuf>) -> Self {
Self(Inner::UnixSocket(path.into()))
}
}
impl Default for Dialer {
fn default() -> Self {
Self(Inner::Default)
}
}
impl From<SocketAddr> for Dialer {
fn from(socket_addr: SocketAddr) -> Self {
Self::ip_socket(socket_addr)
}
}
impl FromStr for Dialer {
type Err = DialerParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with("tcp:") {
let addr_str = s[4..].trim_start_matches("/");
return addr_str
.parse::<SocketAddr>()
.map(Self::ip_socket)
.map_err(|_| DialerParseError(()));
}
#[cfg(unix)]
if s.starts_with("unix:") {
let mut path = std::path::PathBuf::from("/");
path.push(&s[5..].trim_start_matches("/"));
return Ok(Self(Inner::UnixSocket(path)));
}
Err(DialerParseError(()))
}
}
impl TryFrom<&'_ str> for Dialer {
type Error = DialerParseError;
fn try_from(str: &str) -> Result<Self, Self::Error> {
str.parse()
}
}
impl TryFrom<String> for Dialer {
type Error = DialerParseError;
fn try_from(string: String) -> Result<Self, Self::Error> {
string.parse()
}
}
impl TryFrom<Uri> for Dialer {
type Error = DialerParseError;
fn try_from(uri: Uri) -> Result<Self, Self::Error> {
uri.to_string().parse()
}
}
impl SetOpt for Dialer {
fn set_opt<H>(&self, easy: &mut Easy2<H>) -> Result<(), curl::Error> {
let mut connect_to = List::new();
if let Inner::IpSocket(addr) = &self.0 {
connect_to.append(addr)?;
}
easy.connect_to(connect_to)?;
#[cfg(unix)]
easy.unix_socket_path(match &self.0 {
Inner::UnixSocket(path) => Some(path),
_ => None,
})?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_tcp_socket_and_port_uri() {
let dialer = "tcp:127.0.0.1:1200".parse::<Dialer>().unwrap();
assert_eq!(dialer.0, Inner::IpSocket("::127.0.0.1:1200".into()));
}
#[test]
fn parse_invalid_tcp_uri() {
let result = "tcp:127.0.0.1-1200".parse::<Dialer>();
assert!(result.is_err());
}
#[test]
#[cfg(unix)]
fn parse_unix_socket_uri() {
let dialer = "unix:/path/to/my.sock".parse::<Dialer>().unwrap();
assert_eq!(dialer.0, Inner::UnixSocket("/path/to/my.sock".into()));
}
#[test]
#[cfg(unix)]
fn from_unix_socket_uri() {
let uri = "unix://path/to/my.sock".parse::<http::Uri>().unwrap();
let dialer = Dialer::try_from(uri).unwrap();
assert_eq!(dialer.0, Inner::UnixSocket("/path/to/my.sock".into()));
}
}