oxnet 0.1.7

commonly used networking primitives with common traits implemented
Documentation
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
// Copyright 2026 Oxide Computer Company

use std::net::{AddrParseError, IpAddr, Ipv4Addr, Ipv6Addr};

/// An error during the parsing of a [UnicastLinkLocalIpAddr],
/// [UnicastLinkLocalIpv4Addr] or [UnicastLinkLocalIpv6Addr].
#[derive(Debug, Clone)]
pub enum UnicastLinkLocalIpAddrParseError {
    /// Failure to parse the input as an IP address.
    InvalidAddr(AddrParseError),
    /// The parsed address is not unicast link-local.
    NotUnicastLinkLocal(UnicastLinkLocalIpAddrError),
}

impl std::fmt::Display for UnicastLinkLocalIpAddrParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidAddr(error) => error.fmt(f),
            Self::NotUnicastLinkLocal(error) => error.fmt(f),
        }
    }
}

impl std::error::Error for UnicastLinkLocalIpAddrParseError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::InvalidAddr(error) => std::error::Error::source(error),
            Self::NotUnicastLinkLocal(error) => std::error::Error::source(error),
        }
    }
}

/// An error during the creation of a [UnicastLinkLocalIpAddr],
/// [UnicastLinkLocalIpv4Addr] or [UnicastLinkLocalIpv6Addr].
#[derive(Copy, Debug, Clone, PartialEq)]
pub struct UnicastLinkLocalIpAddrError(
    /// The address that failed validation.
    pub IpAddr,
);

impl std::fmt::Display for UnicastLinkLocalIpAddrError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "input is not unicast link-local: {}", self.0)
    }
}

impl std::error::Error for UnicastLinkLocalIpAddrError {}

/// An IP address, either IPv4 or IPv6, that falls within the Link-Local range.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub enum UnicastLinkLocalIpAddr {
    /// An IPv4 address within the Link-local range (169.254.0.0/16).
    V4(UnicastLinkLocalIpv4Addr),
    /// An IPv6 address within the Link-local range (fe80::/10).
    V6(UnicastLinkLocalIpv6Addr),
}

impl UnicastLinkLocalIpAddr {
    /// Create a new [UnicastLinkLocalIpAddr] from an [IpAddr].
    pub fn new(addr: IpAddr) -> Result<Self, UnicastLinkLocalIpAddrError> {
        match addr {
            IpAddr::V4(ip4) => UnicastLinkLocalIpv4Addr::new(ip4).map(Self::V4),
            IpAddr::V6(ip6) => UnicastLinkLocalIpv6Addr::new(ip6).map(Self::V6),
        }
    }

    /// Returns [`true`] if this address is an [`IPv4`](Self::V4) address, and
    /// [`false`] otherwise.
    pub fn is_ipv4(&self) -> bool {
        matches!(self, Self::V4(_))
    }

    /// Returns [`true`] if this address is an [`IPv6`](Self::V6) address, and
    /// [`false`] otherwise.
    pub fn is_ipv6(&self) -> bool {
        matches!(self, Self::V6(_))
    }
}

impl From<UnicastLinkLocalIpv4Addr> for UnicastLinkLocalIpAddr {
    fn from(value: UnicastLinkLocalIpv4Addr) -> Self {
        Self::V4(value)
    }
}

impl From<UnicastLinkLocalIpv6Addr> for UnicastLinkLocalIpAddr {
    fn from(value: UnicastLinkLocalIpv6Addr) -> Self {
        Self::V6(value)
    }
}

impl TryFrom<IpAddr> for UnicastLinkLocalIpAddr {
    type Error = UnicastLinkLocalIpAddrError;

    fn try_from(value: IpAddr) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl TryFrom<Ipv4Addr> for UnicastLinkLocalIpAddr {
    type Error = UnicastLinkLocalIpAddrError;

    fn try_from(value: Ipv4Addr) -> Result<Self, Self::Error> {
        Self::new(value.into())
    }
}

impl TryFrom<Ipv6Addr> for UnicastLinkLocalIpAddr {
    type Error = UnicastLinkLocalIpAddrError;

    fn try_from(value: Ipv6Addr) -> Result<Self, Self::Error> {
        Self::new(value.into())
    }
}

impl From<UnicastLinkLocalIpAddr> for IpAddr {
    fn from(value: UnicastLinkLocalIpAddr) -> Self {
        match value {
            UnicastLinkLocalIpAddr::V4(ip4) => IpAddr::V4(ip4.into_addr()),
            UnicastLinkLocalIpAddr::V6(ip6) => IpAddr::V6(ip6.into_addr()),
        }
    }
}

impl std::fmt::Display for UnicastLinkLocalIpAddr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UnicastLinkLocalIpAddr::V4(inner) => write!(f, "{inner}"),
            UnicastLinkLocalIpAddr::V6(inner) => write!(f, "{inner}"),
        }
    }
}

impl std::str::FromStr for UnicastLinkLocalIpAddr {
    type Err = UnicastLinkLocalIpAddrParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let addr: IpAddr = s
            .parse()
            .map_err(UnicastLinkLocalIpAddrParseError::InvalidAddr)?;
        Self::try_from(addr).map_err(UnicastLinkLocalIpAddrParseError::NotUnicastLinkLocal)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for UnicastLinkLocalIpAddr {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let addr = <IpAddr as serde::Deserialize>::deserialize(deserializer)?;
        Self::new(addr).map_err(serde::de::Error::custom)
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for UnicastLinkLocalIpAddr {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serde::Serialize::serialize(&IpAddr::from(*self), serializer)
    }
}

#[cfg(feature = "schemars")]
impl schemars::JsonSchema for UnicastLinkLocalIpAddr {
    fn schema_name() -> String {
        "UnicastLinkLocalIpAddr".to_string()
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        use crate::schema_util::label_schema;

        schemars::schema::SchemaObject {
            subschemas: Some(Box::new(schemars::schema::SubschemaValidation {
                one_of: Some(vec![
                    label_schema("v4", gen.subschema_for::<UnicastLinkLocalIpv4Addr>()),
                    label_schema("v6", gen.subschema_for::<UnicastLinkLocalIpv6Addr>()),
                ]),
                ..Default::default()
            })),
            extensions: crate::schema_util::extension("UnicastLinkLocalIpAddr", "0.1.7"),
            ..Default::default()
        }
        .into()
    }
}

/// An IPv4 address guaranteed to exist within the link-local range.
///
/// Validation follows [`Ipv4Addr::is_link_local`] and accepts every address in
/// `169.254.0.0/16`, including `169.254.0.0/24` and `169.254.255.0/24`.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct UnicastLinkLocalIpv4Addr(Ipv4Addr);

impl UnicastLinkLocalIpv4Addr {
    /// Create a [UnicastLinkLocalIpv4Addr] from an [Ipv4Addr].
    pub fn new(addr: Ipv4Addr) -> Result<Self, UnicastLinkLocalIpAddrError> {
        if addr.is_link_local() {
            return Ok(Self(addr));
        }
        Err(UnicastLinkLocalIpAddrError(addr.into()))
    }

    /// Converts this address into the underlying [Ipv4Addr].
    pub fn into_addr(self) -> Ipv4Addr {
        self.0
    }
}

impl TryFrom<Ipv4Addr> for UnicastLinkLocalIpv4Addr {
    type Error = UnicastLinkLocalIpAddrError;

    fn try_from(value: Ipv4Addr) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl From<UnicastLinkLocalIpv4Addr> for Ipv4Addr {
    fn from(value: UnicastLinkLocalIpv4Addr) -> Self {
        value.into_addr()
    }
}

impl From<UnicastLinkLocalIpv4Addr> for IpAddr {
    fn from(value: UnicastLinkLocalIpv4Addr) -> Self {
        IpAddr::V4(value.into_addr())
    }
}

impl std::ops::Deref for UnicastLinkLocalIpv4Addr {
    type Target = Ipv4Addr;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl std::fmt::Display for UnicastLinkLocalIpv4Addr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl std::str::FromStr for UnicastLinkLocalIpv4Addr {
    type Err = UnicastLinkLocalIpAddrParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let addr: Ipv4Addr = s
            .parse()
            .map_err(UnicastLinkLocalIpAddrParseError::InvalidAddr)?;
        Self::new(addr).map_err(UnicastLinkLocalIpAddrParseError::NotUnicastLinkLocal)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for UnicastLinkLocalIpv4Addr {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let addr = <Ipv4Addr as serde::Deserialize>::deserialize(deserializer)?;
        Self::new(addr).map_err(serde::de::Error::custom)
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for UnicastLinkLocalIpv4Addr {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serde::Serialize::serialize(&self.0, serializer)
    }
}

#[cfg(feature = "schemars")]
const UNICAST_LINK_LOCAL_IPV4_ADDR_REGEX: &str = concat!(
    r"^169\.254\.",
    r"([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.",
    r"([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$",
);

#[cfg(feature = "schemars")]
impl schemars::JsonSchema for UnicastLinkLocalIpv4Addr {
    fn schema_name() -> String {
        "UnicastLinkLocalIpv4Addr".to_string()
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        let schema = gen.subschema_for::<Ipv4Addr>();
        let mut schema_object = schema.into_object();
        schema_object.metadata = Some(Box::new(schemars::schema::Metadata {
            title: Some("A unicast link-local IPv4 address".to_string()),
            description: Some("An IPv4 address in 169.254.0.0/16".to_string()),
            examples: vec!["169.254.1.1".into()],
            ..Default::default()
        }));
        schema_object.string = Some(Box::new(schemars::schema::StringValidation {
            pattern: Some(UNICAST_LINK_LOCAL_IPV4_ADDR_REGEX.to_string()),
            ..Default::default()
        }));
        schema_object.extensions =
            crate::schema_util::extension("UnicastLinkLocalIpv4Addr", "0.1.7");
        schema_object.into()
    }
}

/// An IPv6 address guaranteed to exist within the Link-local range.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct UnicastLinkLocalIpv6Addr(Ipv6Addr);

impl UnicastLinkLocalIpv6Addr {
    /// Create a [UnicastLinkLocalIpv6Addr] from an [Ipv6Addr].
    pub fn new(addr: Ipv6Addr) -> Result<Self, UnicastLinkLocalIpAddrError> {
        if addr.is_unicast_link_local() {
            return Ok(Self(addr));
        }
        Err(UnicastLinkLocalIpAddrError(addr.into()))
    }

    /// Converts this address into the underlying [Ipv6Addr].
    pub fn into_addr(self) -> Ipv6Addr {
        self.0
    }
}

impl TryFrom<Ipv6Addr> for UnicastLinkLocalIpv6Addr {
    type Error = UnicastLinkLocalIpAddrError;

    fn try_from(value: Ipv6Addr) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl From<UnicastLinkLocalIpv6Addr> for Ipv6Addr {
    fn from(value: UnicastLinkLocalIpv6Addr) -> Self {
        value.into_addr()
    }
}

impl From<UnicastLinkLocalIpv6Addr> for IpAddr {
    fn from(value: UnicastLinkLocalIpv6Addr) -> Self {
        IpAddr::V6(value.into_addr())
    }
}

impl std::ops::Deref for UnicastLinkLocalIpv6Addr {
    type Target = Ipv6Addr;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl std::fmt::Display for UnicastLinkLocalIpv6Addr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl std::str::FromStr for UnicastLinkLocalIpv6Addr {
    type Err = UnicastLinkLocalIpAddrParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let addr: Ipv6Addr = s
            .parse()
            .map_err(UnicastLinkLocalIpAddrParseError::InvalidAddr)?;
        Self::new(addr).map_err(UnicastLinkLocalIpAddrParseError::NotUnicastLinkLocal)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for UnicastLinkLocalIpv6Addr {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let addr = <Ipv6Addr as serde::Deserialize>::deserialize(deserializer)?;
        Self::new(addr).map_err(serde::de::Error::custom)
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for UnicastLinkLocalIpv6Addr {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serde::Serialize::serialize(&self.0, serializer)
    }
}

#[cfg(feature = "schemars")]
// The inherited `ipv6` format describes the full address syntax; this pattern
// only narrows the first segment to fe80..=febf.
const UNICAST_LINK_LOCAL_IPV6_ADDR_REGEX: &str = r"^[fF][eE][89aAbB][0-9a-fA-F]:";

#[cfg(feature = "schemars")]
impl schemars::JsonSchema for UnicastLinkLocalIpv6Addr {
    fn schema_name() -> String {
        "UnicastLinkLocalIpv6Addr".to_string()
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        let schema = gen.subschema_for::<Ipv6Addr>();
        let mut schema_object = schema.into_object();
        schema_object.metadata = Some(Box::new(schemars::schema::Metadata {
            title: Some("A unicast link-local IPv6 address".to_string()),
            description: Some("An IPv6 address in fe80::/10".to_string()),
            examples: vec!["fe80::1".into()],
            ..Default::default()
        }));
        schema_object.string = Some(Box::new(schemars::schema::StringValidation {
            pattern: Some(UNICAST_LINK_LOCAL_IPV6_ADDR_REGEX.to_string()),
            ..Default::default()
        }));
        schema_object.extensions =
            crate::schema_util::extension("UnicastLinkLocalIpv6Addr", "0.1.7");
        schema_object.into()
    }
}

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

    #[test]
    fn from_str_parses_ipv4_link_local_address() {
        let addr: UnicastLinkLocalIpv4Addr = "169.254.1.2".parse().unwrap();
        assert_eq!(addr.into_addr(), Ipv4Addr::new(169, 254, 1, 2));
    }

    #[test]
    fn from_str_parses_ipv6_link_local_address() {
        let addr: UnicastLinkLocalIpv6Addr = "fe80::1".parse().unwrap();
        assert_eq!(addr.into_addr(), "fe80::1".parse::<Ipv6Addr>().unwrap());
    }

    #[test]
    fn from_str_parses_either_address_family() {
        let ipv4: UnicastLinkLocalIpAddr = "169.254.1.2".parse().unwrap();
        let ipv6: UnicastLinkLocalIpAddr = "febf::1".parse().unwrap();

        assert!(ipv4.is_ipv4());
        assert!(ipv6.is_ipv6());
    }

    #[test]
    fn from_str_rejects_malformed_address() {
        let error = "not-an-address"
            .parse::<UnicastLinkLocalIpAddr>()
            .unwrap_err();
        assert!(matches!(
            error,
            UnicastLinkLocalIpAddrParseError::InvalidAddr(_)
        ));
    }

    #[test]
    fn from_str_rejects_non_link_local_addresses() {
        for addr in ["192.0.2.1", "2001:db8::1"] {
            let error = addr.parse::<UnicastLinkLocalIpAddr>().unwrap_err();
            assert!(matches!(
                error,
                UnicastLinkLocalIpAddrParseError::NotUnicastLinkLocal(_)
            ));
        }
    }

    #[test]
    fn parse_errors_transparently_report_the_inner_error() {
        for (input, expected) in [
            ("not-an-address", "invalid IP address syntax"),
            ("192.0.2.1", "input is not unicast link-local: 192.0.2.1"),
        ] {
            let error = input.parse::<UnicastLinkLocalIpAddr>().unwrap_err();

            assert_eq!(error.to_string(), expected);
            assert!(std::error::Error::source(&error).is_none());
        }
    }

    #[test]
    fn ipv4_constructors_accept_entire_link_local_range() {
        for octets in [[169, 254, 0, 0], [169, 254, 255, 255]] {
            let expected = Ipv4Addr::from(octets);
            let validated = UnicastLinkLocalIpv4Addr::new(expected).unwrap();
            let generic = UnicastLinkLocalIpAddr::from(validated);

            assert_eq!(validated.into_addr(), expected);
            assert_eq!(validated.octets(), octets);
            assert_eq!(validated.to_bits(), expected.to_bits());
            assert_eq!(
                UnicastLinkLocalIpAddr::new(expected.into()).unwrap(),
                generic
            );
            assert_eq!(UnicastLinkLocalIpv4Addr::try_from(expected), Ok(validated));
            assert_eq!(UnicastLinkLocalIpAddr::try_from(expected), Ok(generic));
            assert_eq!(
                UnicastLinkLocalIpAddr::try_from(IpAddr::V4(expected)),
                Ok(generic)
            );
            assert_eq!(Ipv4Addr::from(validated), expected);
            assert_eq!(IpAddr::from(validated), IpAddr::V4(expected));
            assert_eq!(IpAddr::from(generic), IpAddr::V4(expected));
        }
    }

    #[test]
    fn ipv4_constructors_reject_addresses_outside_link_local_range() {
        for octets in [[169, 253, 255, 255], [169, 255, 0, 0]] {
            let addr = Ipv4Addr::from(octets);
            let expected = IpAddr::V4(addr);
            let errors = [
                UnicastLinkLocalIpv4Addr::new(addr).unwrap_err(),
                UnicastLinkLocalIpv4Addr::try_from(addr).unwrap_err(),
                UnicastLinkLocalIpAddr::new(expected).unwrap_err(),
                UnicastLinkLocalIpAddr::try_from(addr).unwrap_err(),
                UnicastLinkLocalIpAddr::try_from(expected).unwrap_err(),
            ];

            for error in errors {
                assert_eq!(error.0, expected);
            }
        }
    }

    #[test]
    fn ipv6_constructors_accept_link_local_range_boundaries() {
        for segments in [
            [0xfe80, 0, 0, 0, 0, 0, 0, 0],
            [
                0xfebf, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
            ],
        ] {
            let expected = Ipv6Addr::from(segments);
            let validated = UnicastLinkLocalIpv6Addr::new(expected).unwrap();
            let generic = UnicastLinkLocalIpAddr::from(validated);

            assert_eq!(validated.into_addr(), expected);
            assert_eq!(validated.octets(), expected.octets());
            assert_eq!(validated.segments(), segments);
            assert_eq!(validated.to_bits(), expected.to_bits());
            assert_eq!(
                UnicastLinkLocalIpAddr::new(expected.into()).unwrap(),
                generic
            );
            assert_eq!(UnicastLinkLocalIpv6Addr::try_from(expected), Ok(validated));
            assert_eq!(UnicastLinkLocalIpAddr::try_from(expected), Ok(generic));
            assert_eq!(
                UnicastLinkLocalIpAddr::try_from(IpAddr::V6(expected)),
                Ok(generic)
            );
            assert_eq!(Ipv6Addr::from(validated), expected);
            assert_eq!(IpAddr::from(validated), IpAddr::V6(expected));
            assert_eq!(IpAddr::from(generic), IpAddr::V6(expected));
        }
    }

    #[test]
    fn ipv6_constructors_reject_addresses_outside_link_local_range() {
        for segments in [
            [
                0xfe7f, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
            ],
            [0xfec0, 0, 0, 0, 0, 0, 0, 0],
            [0xff02, 0, 0, 0, 0, 0, 0, 1],
        ] {
            let addr = Ipv6Addr::from(segments);
            let expected = IpAddr::V6(addr);
            let errors = [
                UnicastLinkLocalIpv6Addr::new(addr).unwrap_err(),
                UnicastLinkLocalIpv6Addr::try_from(addr).unwrap_err(),
                UnicastLinkLocalIpAddr::new(expected).unwrap_err(),
                UnicastLinkLocalIpAddr::try_from(addr).unwrap_err(),
                UnicastLinkLocalIpAddr::try_from(expected).unwrap_err(),
            ];

            for error in errors {
                assert_eq!(error.0, expected);
            }
        }
    }

    #[cfg(all(feature = "serde", feature = "schemars"))]
    #[test]
    fn serde_serializes_all_address_types_as_canonical_strings() {
        let generic_v4: UnicastLinkLocalIpAddr = "169.254.1.2".parse().unwrap();
        let generic_v6: UnicastLinkLocalIpAddr = "FE80:0:0:0:0:0:0:1".parse().unwrap();
        let ipv4: UnicastLinkLocalIpv4Addr = "169.254.1.2".parse().unwrap();
        let ipv6: UnicastLinkLocalIpv6Addr = "FE80:0:0:0:0:0:0:1".parse().unwrap();

        assert_eq!(
            serde_json::to_string(&generic_v4).unwrap(),
            r#""169.254.1.2""#
        );
        assert_eq!(serde_json::to_string(&generic_v6).unwrap(), r#""fe80::1""#);
        assert_eq!(serde_json::to_string(&ipv4).unwrap(), r#""169.254.1.2""#);
        assert_eq!(serde_json::to_string(&ipv6).unwrap(), r#""fe80::1""#);
    }

    #[cfg(all(feature = "serde", feature = "schemars"))]
    #[test]
    fn serde_round_trips_all_address_types() {
        let generic_v4: UnicastLinkLocalIpAddr = "169.254.1.2".parse().unwrap();
        let generic_v6: UnicastLinkLocalIpAddr = "fe80::1".parse().unwrap();
        let ipv4: UnicastLinkLocalIpv4Addr = "169.254.1.2".parse().unwrap();
        let ipv6: UnicastLinkLocalIpv6Addr = "fe80::1".parse().unwrap();

        let generic_v4_json = serde_json::to_string(&generic_v4).unwrap();
        let generic_v6_json = serde_json::to_string(&generic_v6).unwrap();
        let ipv4_json = serde_json::to_string(&ipv4).unwrap();
        let ipv6_json = serde_json::to_string(&ipv6).unwrap();

        assert_eq!(
            serde_json::from_str::<UnicastLinkLocalIpAddr>(&generic_v4_json).unwrap(),
            generic_v4
        );
        assert_eq!(
            serde_json::from_str::<UnicastLinkLocalIpAddr>(&generic_v6_json).unwrap(),
            generic_v6
        );
        assert_eq!(
            serde_json::from_str::<UnicastLinkLocalIpv4Addr>(&ipv4_json).unwrap(),
            ipv4
        );
        assert_eq!(
            serde_json::from_str::<UnicastLinkLocalIpv6Addr>(&ipv6_json).unwrap(),
            ipv6
        );
    }

    #[cfg(all(feature = "serde", feature = "schemars"))]
    #[test]
    fn serde_accepts_link_local_range_boundaries() {
        for addr in [r#""169.254.0.0""#, r#""169.254.255.255""#] {
            assert!(serde_json::from_str::<UnicastLinkLocalIpAddr>(addr).is_ok());
            assert!(serde_json::from_str::<UnicastLinkLocalIpv4Addr>(addr).is_ok());
        }

        for addr in [
            r#""fe80::""#,
            r#""febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff""#,
        ] {
            assert!(serde_json::from_str::<UnicastLinkLocalIpAddr>(addr).is_ok());
            assert!(serde_json::from_str::<UnicastLinkLocalIpv6Addr>(addr).is_ok());
        }
    }

    #[cfg(all(feature = "serde", feature = "schemars"))]
    #[test]
    fn serde_rejects_non_link_local_addresses() {
        for addr in [r#""169.253.255.255""#, r#""169.255.0.0""#, r#""192.0.2.1""#] {
            assert!(serde_json::from_str::<UnicastLinkLocalIpAddr>(addr).is_err());
            assert!(serde_json::from_str::<UnicastLinkLocalIpv4Addr>(addr).is_err());
        }

        for addr in [
            r#""fe7f:ffff:ffff:ffff:ffff:ffff:ffff:ffff""#,
            r#""fec0::""#,
            r#""2001:db8::1""#,
        ] {
            assert!(serde_json::from_str::<UnicastLinkLocalIpAddr>(addr).is_err());
            assert!(serde_json::from_str::<UnicastLinkLocalIpv6Addr>(addr).is_err());
        }
    }

    #[cfg(feature = "schemars")]
    #[test]
    fn schema_patterns_match_link_local_boundaries() {
        let ipv4 = regress::Regex::new(UNICAST_LINK_LOCAL_IPV4_ADDR_REGEX).unwrap();
        let ipv6 = regress::Regex::new(UNICAST_LINK_LOCAL_IPV6_ADDR_REGEX).unwrap();

        for addr in ["169.254.0.0", "169.254.255.255"] {
            assert!(ipv4.find(addr).is_some(), "expected {addr} to match");
        }
        for addr in ["169.253.255.255", "169.255.0.0"] {
            assert!(ipv4.find(addr).is_none(), "expected {addr} not to match");
        }
        for addr in ["fe80::", "FE9a::1", "feaf::1", "febf:ffff::1"] {
            assert!(ipv6.find(addr).is_some(), "expected {addr} to match");
        }
        for addr in ["fe7f::", "fec0::", "ff02::1"] {
            assert!(ipv6.find(addr).is_none(), "expected {addr} not to match");
        }
    }
}