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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//! This crate can be used to create URL prefix strings by inputting a protocol, a domain, a port number and a path.
//!
//! ## Why You Need This?
//!
//! Sometimes your web application is run on different protocols(HTTP/HTTPS) and domains. And it is boring to write some code like below to format a URL:
//!
//! ```rust,ignore
//! let mut url_prefix = String::new();
//! if is_https {
//!     url_prefix.push_str("https://");
//! } else {
//!     url_prefix.push_str("http://");
//! }
//! url_prefix.push_str(domain);
//!
//! if is_https && port != 443 || !is_https && port != 80{
//!     url_prefix.push_str(":");
//!     url_prefix.push_str(&port.to_string());
//! }
//! ```
//!
//! Instead, you can easily use this crate to create URL prefix strings. For examples,
//!
//! ```rust
//! extern crate url_prefix;
//!
//! let prefix = url_prefix::create_prefix(url_prefix::Protocol::HTTPS, "magiclen.org", None, None);
//!
//! assert_eq!("https://magiclen.org", prefix);
//! ```
//!
//! ```rust
//! extern crate url_prefix;
//!
//! let prefix = url_prefix::create_prefix(url_prefix::Protocol::HTTPS, "magiclen.org", Some(8100), Some("url-prefix"));
//!
//! assert_eq!("https://magiclen.org:8100/url-prefix", prefix);
//! ```
//!
//! ## Validators Support
//!
//! `Validators` is a crate which can help you validate user input, in order to create a safe URL prefix.
//!
//! To use with `Validators` support, you have to enable the **validator** feature for this crate.
//!
//!
//! ```toml
//! [dependencies.url-prefix]
//! version = "*"
//! features = ["validator"]
//! ```
//! And the `create_prefix_with_validated_domain`, `create_prefix_with_validated_ipv4`, `create_prefix_with_validated_ipv6`, `create_prefix_with_validated_host` functions are available.
//!
//! For example,
//!
//! ```rust,ignore
//! extern crate url_prefix;
//!
//! let user_input = url_prefix::validators::domain::DomainLocalhostableWithPort::from_str("magiclen.org:443").unwrap();
//!
//! let prefix = url_prefix::create_prefix_with_validated_domain(url_prefix::Protocol::HTTPS, user_input.as_domain(), Some("url-prefix"));
//!
//! assert_eq!("https://magiclen.org/url-prefix", prefix);
//! ```

#[cfg(feature = "validator")]
pub extern crate validators;

#[cfg(feature = "validator")]
use validators::domain::Domain;

#[cfg(feature = "validator")]
use validators::host::Host;

#[cfg(feature = "validator")]
use validators::ipv4::IPv4;

#[cfg(feature = "validator")]
use validators::ipv6::IPv6;

#[cfg(feature = "validator")]
use validators::http_url::HttpUrlLocalableWithProtocol;

#[cfg(feature = "validator")]
use validators::http_ftp_url::HttpFtpUrlLocalableWithProtocol;

macro_rules! impl_protocol {
    ( $($protocol:ident, $name:expr, $port:expr), * ) => {
        /// A set of protocols for URLs.
        pub enum Protocol{
            $(
                $protocol,
            )+
            /// Your own custom protocol created by giving a name and a default port number.
            Custom(String, u16)
        }

        impl Protocol{
            pub fn get_default_from_string(s: String) -> Option<Self>{
                let lowered_case = s.to_lowercase();
                match lowered_case.as_str() {
                    $(
                        $name => Some(Protocol::$protocol),
                    )+
                    _ => None
                }
            }

            pub fn get_default_from_str(s: &str) -> Option<Self>{
                let lowered_case = s.to_lowercase();
                match lowered_case.as_str() {
                    $(
                        $name => Some(Protocol::$protocol),
                    )+
                    _ => None
                }
            }

            pub fn get_default_port(&self) -> u16 {
                match self {
                    $(
                        Protocol::$protocol => $port,
                    )+
                    Protocol::Custom(_, port) => *port
                }
            }

            pub fn get_name(&self) -> &str {
                match self {
                    $(
                        Protocol::$protocol => $name,
                    )+
                    Protocol::Custom(name, _) => &name
                }
            }
        }
    };
}

impl_protocol!(
    HTTP, "http", 80,
    HTTPS, "https", 443,
    FTP, "ftp", 21
);

/// Create a URL prefix string.
pub fn create_prefix(protocol: Protocol, domain: &str, port: Option<u16>, path: Option<&str>) -> String {
    let protocol_name = protocol.get_name();

    let mut prefix = format!("{}://{}", protocol_name, domain);

    if let Some(port) = port {
        let protocol_port = protocol.get_default_port();
        if port != protocol_port {
            prefix.push(':');
            prefix.push_str(&port.to_string());
        }
    }

    if let Some(path) = path {
        if !path.starts_with("/") {
            prefix.push('/');
        }
        prefix.push_str(&path);
    }

    prefix
}

#[cfg(feature = "validator")]
/// Create a safe URL prefix string.
pub fn create_prefix_with_validated_domain(protocol: Protocol, domain: &Domain, path: Option<&str>) -> String {
    let port = domain.get_port();

    let domain = domain.get_full_domain_without_port();

    create_prefix(protocol, domain, port, path)
}

#[cfg(feature = "validator")]
/// Create a safe URL prefix string.
pub fn create_prefix_with_validated_ipv4(protocol: Protocol, ipv4: &IPv4, path: Option<&str>) -> String {
    let port = ipv4.get_port();

    let ipv4 = ipv4.get_full_ipv4_without_port();

    create_prefix(protocol, ipv4, port, path)
}

#[cfg(feature = "validator")]
/// Create a safe URL prefix string.
pub fn create_prefix_with_validated_ipv6(protocol: Protocol, ipv6: &IPv6, path: Option<&str>) -> String {
    let port = ipv6.get_port();

    let ipv6 = ipv6.get_full_ipv6_without_port();

    let ipv6 = format!("[{}]", ipv6);

    create_prefix(protocol, &ipv6, port, path)
}

#[cfg(feature = "validator")]
/// Create a safe URL prefix string.
pub fn create_prefix_with_validated_host(protocol: Protocol, host: &Host, path: Option<&str>) -> String {
    match host {
        Host::Domain(domain) => {
            create_prefix_with_validated_domain(protocol, domain, path)
        }
        Host::IPv4(ipv4) => {
            create_prefix_with_validated_ipv4(protocol, ipv4, path)
        }
        Host::IPv6(ipv6) => {
            create_prefix_with_validated_ipv6(protocol, ipv6, path)
        }
    }
}

#[cfg(feature = "validator")]
/// Create a safe URL prefix string.
pub fn create_prefix_with_validated_http_url(http_url: &HttpUrlLocalableWithProtocol) -> String {
    let protocol = if http_url.is_https() {
        Protocol::HTTPS
    } else {
        Protocol::HTTP
    };

    create_prefix_with_validated_host(protocol, http_url.get_host(), http_url.get_path())
}

#[cfg(feature = "validator")]
/// Create a safe URL prefix string.
pub fn create_prefix_with_validated_http_ftp_url(http_ftp_url: &HttpFtpUrlLocalableWithProtocol) -> String {
    let protocol = if http_ftp_url.is_https() {
        Protocol::HTTPS
    } else if http_ftp_url.is_http() {
        Protocol::HTTP
    } else {
        Protocol::FTP
    };

    create_prefix_with_validated_host(protocol, http_ftp_url.get_host(), http_ftp_url.get_path())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn create_prefix_lv1_1() {
        let prefix = create_prefix(Protocol::HTTP, "magiclen.org", None, None);

        assert_eq!("http://magiclen.org", prefix);
    }

    #[test]
    fn create_prefix_lv1_2() {
        let prefix = create_prefix(Protocol::HTTPS, "magiclen.org", None, None);

        assert_eq!("https://magiclen.org", prefix);
    }

    #[test]
    fn create_prefix_lv1_3() {
        let prefix = create_prefix(Protocol::FTP, "magiclen.org", None, None);

        assert_eq!("ftp://magiclen.org", prefix);
    }

    #[test]
    fn create_prefix_lv2_1() {
        let prefix = create_prefix(Protocol::HTTP, "magiclen.org", Some(8000), None);

        assert_eq!("http://magiclen.org:8000", prefix);
    }

    #[test]
    fn create_prefix_lv2_2() {
        let prefix = create_prefix(Protocol::HTTPS, "magiclen.org", Some(8100), None);

        assert_eq!("https://magiclen.org:8100", prefix);
    }

    #[test]
    fn create_prefix_lv2_3() {
        let prefix = create_prefix(Protocol::FTP, "magiclen.org", Some(8200), None);

        assert_eq!("ftp://magiclen.org:8200", prefix);
    }

    #[test]
    fn create_prefix_lv3_1() {
        let prefix = create_prefix(Protocol::HTTP, "magiclen.org", Some(80), None);

        assert_eq!("http://magiclen.org", prefix);
    }

    #[test]
    fn create_prefix_lv3_2() {
        let prefix = create_prefix(Protocol::HTTPS, "magiclen.org", Some(443), None);

        assert_eq!("https://magiclen.org", prefix);
    }

    #[test]
    fn create_prefix_lv3_3() {
        let prefix = create_prefix(Protocol::FTP, "magiclen.org", Some(21), None);

        assert_eq!("ftp://magiclen.org", prefix);
    }

    #[test]
    fn create_prefix_lv4_1() {
        let prefix = create_prefix(Protocol::HTTP, "magiclen.org", Some(80), Some("url-prefix"));

        assert_eq!("http://magiclen.org/url-prefix", prefix);
    }

    #[test]
    fn create_prefix_lv4_2() {
        let prefix = create_prefix(Protocol::HTTPS, "magiclen.org", Some(8100), Some("url-prefix"));

        assert_eq!("https://magiclen.org:8100/url-prefix", prefix);
    }

    #[cfg(feature = "validator")]
    #[test]
    fn create_prefix_with_validated_domain_lv4() {
        let user_input = validators::domain::DomainLocalhostableWithPort::from_str("magiclen.org:443").unwrap();

        let prefix = create_prefix_with_validated_domain(Protocol::HTTPS, user_input.as_domain(), Some("url-prefix"));

        assert_eq!("https://magiclen.org/url-prefix", prefix);
    }

    #[cfg(feature = "validator")]
    #[test]
    fn create_prefix_with_validated_ipv4_lv4() {
        let user_input = validators::ipv4::IPv4LocalableWithPort::from_str("127.0.0.1:443").unwrap();

        let prefix = create_prefix_with_validated_ipv4(Protocol::HTTPS, user_input.as_ipv4(), Some("url-prefix"));

        assert_eq!("https://127.0.0.1/url-prefix", prefix);
    }

    #[cfg(feature = "validator")]
    #[test]
    fn create_prefix_with_validated_ipv6_lv4() {
        let user_input = validators::ipv6::IPv6LocalableWithPort::from_str("[0000:0000:0000:0000:0000:0000:370:7348]:443").unwrap();

        let prefix = create_prefix_with_validated_ipv6(Protocol::HTTPS, user_input.as_ipv6(), Some("url-prefix"));

        assert_eq!("https://[0000:0000:0000:0000:0000:0000:370:7348]/url-prefix", prefix);
    }

    #[cfg(feature = "validator")]
    #[test]
    fn create_prefix_with_validated_host_lv4() {
        let user_input = validators::host::HostLocalable::from_str("127.0.0.1:443").unwrap();

        let prefix = create_prefix_with_validated_host(Protocol::HTTPS, user_input.as_host(), Some("url-prefix"));

        assert_eq!("https://127.0.0.1/url-prefix", prefix);
    }

    #[cfg(feature = "validator")]
    #[test]
    fn create_prefix_with_validated_http_url_lv4() {
        let user_input = HttpUrlLocalableWithProtocol::from_str("https://127.0.0.1:443/url-prefix").unwrap();

        let prefix = create_prefix_with_validated_http_url(&user_input);

        assert_eq!("https://127.0.0.1/url-prefix", prefix);
    }

    #[cfg(feature = "validator")]
    #[test]
    fn create_prefix_with_validated_http_ftp_url_lv4() {
        let user_input = HttpFtpUrlLocalableWithProtocol::from_str("ftp://127.0.0.1:21/url-prefix").unwrap();

        let prefix = create_prefix_with_validated_http_ftp_url(&user_input);

        assert_eq!("ftp://127.0.0.1/url-prefix", prefix);
    }
}