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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//  ADDRESS.rs
//    by Lut99
//
//  Created:
//    26 Jan 2023, 09:41:51
//  Last edited:
//    12 Jan 2024, 11:51:07
//  Auto updated?
//    Yes
//
//  Description:
//!   Defines the Address struct, which does something similar to the Url
//!   struct in the `url` crate, except that it's much more lenient
//!   towards defining URL schemes or not. Moreover, it does not contain
//!   any paths.
//

use std::borrow::Cow;
use std::convert::TryFrom;
use std::error::Error;
use std::fmt::{Display, Formatter, Result as FResult};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::str::FromStr;

use enum_debug::EnumDebug;
use log::trace;
use serde::de::{self, Deserializer, Visitor};
use serde::ser::Serializer;
use serde::{Deserialize, Serialize};


/***** ERRORS *****/
/// Errors that relate to parsing Addresses.
#[derive(Debug)]
pub enum AddressError {
    /// Invalid port number.
    IllegalPortNumber { raw: String, err: std::num::ParseIntError },
    /// Missing the colon separator (':') in the address.
    MissingColon { raw: String },
    /// Port not found when translating an [`AddressOpt`] into an [`Address`].
    MissingPort { addr: AddressOpt },
}
impl Display for AddressError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        use AddressError::*;
        match self {
            IllegalPortNumber { raw, .. } => write!(f, "Illegal port number '{raw}'"),
            MissingColon { raw } => write!(f, "Missing address/port separator ':' in '{raw}' (did you forget to define a port?)"),
            MissingPort { addr } => write!(f, "Address '{addr}' does not have a port"),
        }
    }
}
impl Error for AddressError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        use AddressError::*;
        match self {
            IllegalPortNumber { err, .. } => Some(err),
            MissingColon { .. } => None,
            MissingPort { .. } => None,
        }
    }
}





/***** LIBRARY *****/
/// Defines a more lenient alternative to a SocketAddr that also accepts hostnames.
#[derive(Clone, Debug, EnumDebug)]
pub enum Address {
    /// It's an Ipv4 address.
    Ipv4(Ipv4Addr, u16),
    /// It's an Ipv6 address.
    Ipv6(Ipv6Addr, u16),
    /// It's a hostname.
    Hostname(String, u16),
}
impl Address {
    /// Constructor for the Address that initializes it for the given IPv4 address.
    ///
    /// # Arguments
    /// - `b1`: The first byte of the IPv4 address.
    /// - `b2`: The second byte of the IPv4 address.
    /// - `b3`: The third byte of the IPv4 address.
    /// - `b4`: The fourth byte of the IPv4 address.
    /// - `port`: The port for this address.
    ///
    /// # Returns
    /// A new Address instance.
    #[inline]
    pub fn ipv4(b1: u8, b2: u8, b3: u8, b4: u8, port: u16) -> Self { Self::Ipv4(Ipv4Addr::new(b1, b2, b3, b4), port) }

    /// Constructor for the Address that initializes it for the given IPv4 address.
    ///
    /// # Arguments
    /// - `ipv4`: The already constructed IPv4 address to use.
    /// - `port`: The port for this address.
    ///
    /// # Returns
    /// A new Address instance.
    #[inline]
    pub fn from_ipv4(ipv4: impl Into<Ipv4Addr>, port: u16) -> Self { Self::Ipv4(ipv4.into(), port) }

    /// Constructor for the Address that initializes it for the given IPv6 address.
    ///
    /// # Arguments
    /// - `b1`: The first pair of bytes of the IPv6 address.
    /// - `b2`: The second pair of bytes of the IPv6 address.
    /// - `b3`: The third pair of bytes of the IPv6 address.
    /// - `b4`: The fourth pair of bytes of the IPv6 address.
    /// - `b5`: The fifth pair of bytes of the IPv6 address.
    /// - `b6`: The sixth pair of bytes of the IPv6 address.
    /// - `b7`: The seventh pair of bytes of the IPv6 address.
    /// - `b8`: The eight pair of bytes of the IPv6 address.
    /// - `port`: The port for this address.
    ///
    /// # Returns
    /// A new Address instance.
    #[allow(clippy::too_many_arguments)]
    #[inline]
    pub fn ipv6(b1: u16, b2: u16, b3: u16, b4: u16, b5: u16, b6: u16, b7: u16, b8: u16, port: u16) -> Self {
        Self::Ipv6(Ipv6Addr::new(b1, b2, b3, b4, b5, b6, b7, b8), port)
    }

    /// Constructor for the Address that initializes it for the given IPv6 address.
    ///
    /// # Arguments
    /// - `ipv6`: The already constructed IPv6 address to use.
    /// - `port`: The port for this address.
    ///
    /// # Returns
    /// A new Address instance.
    #[inline]
    pub fn from_ipv6(ipv6: impl Into<Ipv6Addr>, port: u16) -> Self { Self::Ipv6(ipv6.into(), port) }

    /// Constructor for the Address that initializes it for the given hostname.
    ///
    /// # Arguments
    /// - `hostname`: The hostname for this Address.
    /// - `port`: The port for this address.
    ///
    /// # Returns
    /// A new Address instance.
    #[inline]
    pub fn hostname(hostname: impl Into<String>, port: u16) -> Self { Self::Hostname(hostname.into(), port) }

    /// Returns the domain-part, as a (serialized) string version.
    ///
    /// # Returns
    /// A `Cow<str>` that either contains a reference to the already String hostname, or else a newly created string that is the serialized version of an IP.
    #[inline]
    pub fn domain(&self) -> Cow<'_, str> {
        use Address::*;
        match self {
            Ipv4(addr, _) => format!("{addr}").into(),
            Ipv6(addr, _) => format!("{addr}").into(),
            Hostname(addr, _) => addr.into(),
        }
    }

    /// Returns the port-part, as a number.
    ///
    /// # Returns
    /// A `u16` that is the port.
    #[inline]
    pub fn port(&self) -> u16 {
        use Address::*;
        match self {
            Ipv4(_, port) => *port,
            Ipv6(_, port) => *port,
            Hostname(_, port) => *port,
        }
    }

    /// Returns the port-part as a mutable number.
    ///
    /// # Returns
    /// A mutable reference to the `u16` that is the port.
    #[inline]
    pub fn port_mut(&mut self) -> &mut u16 {
        use Address::*;
        match self {
            Ipv4(_, port) => port,
            Ipv6(_, port) => port,
            Hostname(_, port) => port,
        }
    }

    /// Returns if this Address is an `Address::Hostname`.
    ///
    /// # Returns
    /// True if it is, false if it isn't.
    #[inline]
    pub fn is_hostname(&self) -> bool { matches!(self, Self::Hostname(_, _)) }

    /// Returns if this Address is an `Address::Ipv4` or `Address::Ipv6`.
    ///
    /// # Returns
    /// True if it is, false if it isn't.
    #[inline]
    pub fn is_ip(&self) -> bool { self.is_ipv4() || self.is_ipv6() }

    /// Returns if this Address is an `Address::Ipv4`.
    ///
    /// # Returns
    /// True if it is, false if it isn't.
    #[inline]
    pub fn is_ipv4(&self) -> bool { matches!(self, Self::Ipv4(_, _)) }

    /// Returns if this Address is an `Address::Ipv6`.
    ///
    /// # Returns
    /// True if it is, false if it isn't.
    #[inline]
    pub fn is_ipv6(&self) -> bool { matches!(self, Self::Ipv6(_, _)) }

    /// Returns a formatter that deterministically and parseably serializes the Address.
    #[inline]
    pub fn serialize(&self) -> impl '_ + Display { self }
}
impl Display for Address {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        use Address::*;
        match self {
            Ipv4(addr, port) => write!(f, "{addr}:{port}"),
            Ipv6(addr, port) => write!(f, "{addr}:{port}"),
            Hostname(addr, port) => write!(f, "{addr}:{port}"),
        }
    }
}
impl Serialize for Address {
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&format!("{}", self.serialize()))
    }
}
impl<'de> Deserialize<'de> for Address {
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        /// Defines the visitor for the Address
        struct AddressVisitor;
        impl<'de> Visitor<'de> for AddressVisitor {
            type Value = Address;

            #[inline]
            fn expecting(&self, f: &mut Formatter<'_>) -> FResult { write!(f, "an address:port pair") }

            #[inline]
            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                // Attempt to serialize the incoming string
                match Address::from_str(v) {
                    Ok(address) => Ok(address),
                    Err(err) => Err(E::custom(err)),
                }
            }
        }

        // Call the visitor
        deserializer.deserialize_str(AddressVisitor)
    }
}
impl FromStr for Address {
    type Err = AddressError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // Attempt to find the colon first
        let colon_pos: usize = match s.rfind(':') {
            Some(pos) => pos,
            None => {
                return Err(AddressError::MissingColon { raw: s.into() });
            },
        };

        // Split it on that
        let (address, port): (&str, &str) = (&s[..colon_pos], &s[colon_pos + 1..]);

        // Parse the port
        let port: u16 = match u16::from_str(port) {
            Ok(port) => port,
            Err(err) => {
                return Err(AddressError::IllegalPortNumber { raw: port.into(), err });
            },
        };

        // Resolve the address to a new instance of ourselves
        match IpAddr::from_str(address) {
            Ok(address) => match address {
                IpAddr::V4(ip) => Ok(Self::Ipv4(ip, port)),
                IpAddr::V6(ip) => Ok(Self::Ipv6(ip, port)),
            },
            Err(err) => {
                trace!("Parsing '{}' as a hostname, but might be an invalid IP address (parser feedback: {})", address, err);
                Ok(Self::Hostname(address.into(), port))
            },
        }
    }
}
impl AsRef<Address> for Address {
    #[inline]
    fn as_ref(&self) -> &Self { self }
}
impl From<&Address> for Address {
    #[inline]
    fn from(value: &Address) -> Self { value.clone() }
}
impl From<&mut Address> for Address {
    #[inline]
    fn from(value: &mut Address) -> Self { value.clone() }
}
impl TryFrom<AddressOpt> for Address {
    type Error = AddressError;

    #[inline]
    fn try_from(value: AddressOpt) -> Result<Self, Self::Error> {
        match value {
            AddressOpt::Ipv4(host, port_opt) => {
                if let Some(port) = port_opt {
                    Ok(Self::Ipv4(host, port))
                } else {
                    Err(AddressError::MissingPort { addr: AddressOpt::Ipv4(host, None) })
                }
            },

            AddressOpt::Ipv6(host, port_opt) => {
                if let Some(port) = port_opt {
                    Ok(Self::Ipv6(host, port))
                } else {
                    Err(AddressError::MissingPort { addr: AddressOpt::Ipv6(host, None) })
                }
            },

            AddressOpt::Hostname(host, port_opt) => {
                if let Some(port) = port_opt {
                    Ok(Self::Hostname(host, port))
                } else {
                    Err(AddressError::MissingPort { addr: AddressOpt::Hostname(host, None) })
                }
            },
        }
    }
}



/// Alternative to an [`Address`] that has an optional port part.
#[derive(Clone, Debug, EnumDebug)]
pub enum AddressOpt {
    /// It's an Ipv4 address.
    Ipv4(Ipv4Addr, Option<u16>),
    /// It's an Ipv6 address.
    Ipv6(Ipv6Addr, Option<u16>),
    /// It's a hostname.
    Hostname(String, Option<u16>),
}
impl AddressOpt {
    /// Constructor for the AddressOpt that initializes it for the given IPv4 address.
    ///
    /// # Arguments
    /// - `b1`: The first byte of the IPv4 address.
    /// - `b2`: The second byte of the IPv4 address.
    /// - `b3`: The third byte of the IPv4 address.
    /// - `b4`: The fourth byte of the IPv4 address.
    /// - `port`: The port for this address, if any.
    ///
    /// # Returns
    /// A new AddressOpt instance.
    #[inline]
    pub fn ipv4(b1: u8, b2: u8, b3: u8, b4: u8, port: Option<u16>) -> Self { Self::Ipv4(Ipv4Addr::new(b1, b2, b3, b4), port) }

    /// Constructor for the AddressOpt that initializes it for the given IPv4 address.
    ///
    /// # Arguments
    /// - `ipv4`: The already constructed IPv4 address to use.
    /// - `port`: The port for this address, if any.
    ///
    /// # Returns
    /// A new AddressOpt instance.
    #[inline]
    pub fn from_ipv4(ipv4: impl Into<Ipv4Addr>, port: Option<u16>) -> Self { Self::Ipv4(ipv4.into(), port) }

    /// Constructor for the AddressOpt that initializes it for the given IPv6 address.
    ///
    /// # Arguments
    /// - `b1`: The first pair of bytes of the IPv6 address.
    /// - `b2`: The second pair of bytes of the IPv6 address.
    /// - `b3`: The third pair of bytes of the IPv6 address.
    /// - `b4`: The fourth pair of bytes of the IPv6 address.
    /// - `b5`: The fifth pair of bytes of the IPv6 address.
    /// - `b6`: The sixth pair of bytes of the IPv6 address.
    /// - `b7`: The seventh pair of bytes of the IPv6 address.
    /// - `b8`: The eight pair of bytes of the IPv6 address.
    /// - `port`: The port for this address, if any.
    ///
    /// # Returns
    /// A new AddressOpt instance.
    #[allow(clippy::too_many_arguments)]
    #[inline]
    pub fn ipv6(b1: u16, b2: u16, b3: u16, b4: u16, b5: u16, b6: u16, b7: u16, b8: u16, port: Option<u16>) -> Self {
        Self::Ipv6(Ipv6Addr::new(b1, b2, b3, b4, b5, b6, b7, b8), port)
    }

    /// Constructor for the AddressOpt that initializes it for the given IPv6 address.
    ///
    /// # Arguments
    /// - `ipv6`: The already constructed IPv6 address to use.
    /// - `port`: The port for this address, if any.
    ///
    /// # Returns
    /// A new AddressOpt instance.
    #[inline]
    pub fn from_ipv6(ipv6: impl Into<Ipv6Addr>, port: Option<u16>) -> Self { Self::Ipv6(ipv6.into(), port) }

    /// Constructor for the AddressOpt that initializes it for the given hostname.
    ///
    /// # Arguments
    /// - `hostname`: The hostname for this AddressOpt.
    /// - `port`: The port for this address, if any.
    ///
    /// # Returns
    /// A new AddressOpt instance.
    #[inline]
    pub fn hostname(hostname: impl Into<String>, port: Option<u16>) -> Self { Self::Hostname(hostname.into(), port) }

    /// Returns the domain-part, as a (serialized) string version.
    ///
    /// # Returns
    /// A `Cow<str>` that either contains a reference to the already String hostname, or else a newly created string that is the serialized version of an IP.
    #[inline]
    pub fn domain(&self) -> Cow<'_, str> {
        use AddressOpt::*;
        match self {
            Ipv4(addr, _) => format!("{addr}").into(),
            Ipv6(addr, _) => format!("{addr}").into(),
            Hostname(addr, _) => addr.into(),
        }
    }

    /// Returns the port-part, as a number.
    ///
    /// # Returns
    /// A `u16` that is the port.
    #[inline]
    pub fn port(&self) -> Option<u16> {
        use AddressOpt::*;
        match self {
            Ipv4(_, port) => *port,
            Ipv6(_, port) => *port,
            Hostname(_, port) => *port,
        }
    }

    /// Returns the port-part as a mutable number.
    ///
    /// # Returns
    /// A mutable reference to the `u16` that is the port.
    #[inline]
    pub fn port_mut(&mut self) -> &mut Option<u16> {
        use AddressOpt::*;
        match self {
            Ipv4(_, port) => port,
            Ipv6(_, port) => port,
            Hostname(_, port) => port,
        }
    }

    /// Returns if this AddressOpt is an `AddressOpt::Hostname`.
    ///
    /// # Returns
    /// True if it is, false if it isn't.
    #[inline]
    pub fn is_hostname(&self) -> bool { matches!(self, Self::Hostname(_, _)) }

    /// Returns if this AddressOpt is an `AddressOpt::Ipv4` or `AddressOpt::Ipv6`.
    ///
    /// # Returns
    /// True if it is, false if it isn't.
    #[inline]
    pub fn is_ip(&self) -> bool { self.is_ipv4() || self.is_ipv6() }

    /// Returns if this AddressOpt is an `AddressOpt::Ipv4`.
    ///
    /// # Returns
    /// True if it is, false if it isn't.
    #[inline]
    pub fn is_ipv4(&self) -> bool { matches!(self, Self::Ipv4(_, _)) }

    /// Returns if this AddressOpt is an `AddressOpt::Ipv6`.
    ///
    /// # Returns
    /// True if it is, false if it isn't.
    #[inline]
    pub fn is_ipv6(&self) -> bool { matches!(self, Self::Ipv6(_, _)) }

    /// Returns a formatter that deterministically and parseably serializes the AddressOpt.
    #[inline]
    pub fn serialize(&self) -> impl '_ + Display { self }
}
impl Display for AddressOpt {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        use AddressOpt::*;
        match self {
            Ipv4(addr, port) => {
                write!(f, "{addr}")?;
                if let Some(port) = port {
                    write!(f, ":{port}")?;
                };
                Ok(())
            },
            Ipv6(addr, port) => {
                write!(f, "{addr}")?;
                if let Some(port) = port {
                    write!(f, ":{port}")?;
                };
                Ok(())
            },
            Hostname(addr, port) => {
                write!(f, "{addr}")?;
                if let Some(port) = port {
                    write!(f, ":{port}")?;
                };
                Ok(())
            },
        }
    }
}
impl Serialize for AddressOpt {
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&format!("{}", self.serialize()))
    }
}
impl<'de> Deserialize<'de> for AddressOpt {
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        /// Defines the visitor for the AddressOpt
        struct AddressOptVisitor;
        impl<'de> Visitor<'de> for AddressOptVisitor {
            type Value = AddressOpt;

            #[inline]
            fn expecting(&self, f: &mut Formatter<'_>) -> FResult { write!(f, "an address:port pair") }

            #[inline]
            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                // Attempt to serialize the incoming string
                match AddressOpt::from_str(v) {
                    Ok(address) => Ok(address),
                    Err(err) => Err(E::custom(err)),
                }
            }
        }

        // Call the visitor
        deserializer.deserialize_str(AddressOptVisitor)
    }
}
impl FromStr for AddressOpt {
    type Err = AddressError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // Attempt to find the colon first and split the string accordingly
        let (address, port): (&str, Option<&str>) = match s.rfind(':') {
            Some(pos) => (&s[..pos], Some(&s[pos + 1..])),
            None => (s, None),
        };

        // Parse the port, if any
        let port: Option<u16> = port.map(|p| u16::from_str(p).map_err(|err| AddressError::IllegalPortNumber { raw: p.into(), err })).transpose()?;

        // Resolve the address to a new instance of ourselves
        match IpAddr::from_str(address) {
            Ok(address) => match address {
                IpAddr::V4(ip) => Ok(Self::Ipv4(ip, port)),
                IpAddr::V6(ip) => Ok(Self::Ipv6(ip, port)),
            },
            Err(err) => {
                trace!("Parsing '{}' as a hostname, but might be an invalid IP address (parser feedback: {})", address, err);
                Ok(Self::Hostname(address.into(), port))
            },
        }
    }
}
impl AsRef<AddressOpt> for AddressOpt {
    #[inline]
    fn as_ref(&self) -> &Self { self }
}
impl From<&AddressOpt> for AddressOpt {
    #[inline]
    fn from(value: &AddressOpt) -> Self { value.clone() }
}
impl From<&mut AddressOpt> for AddressOpt {
    #[inline]
    fn from(value: &mut AddressOpt) -> Self { value.clone() }
}
impl From<Address> for AddressOpt {
    #[inline]
    fn from(value: Address) -> Self {
        match value {
            Address::Ipv4(host, port) => Self::Ipv4(host, Some(port)),
            Address::Ipv6(host, port) => Self::Ipv6(host, Some(port)),
            Address::Hostname(host, port) => Self::Hostname(host, Some(port)),
        }
    }
}