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
use core::fmt;

#[cfg(any(test, feature = "arbitrary"))]
use proptest::{
    arbitrary::{any, Arbitrary},
    prop_oneof,
    strategy::{BoxedStrategy, Strategy},
};

use super::delegate;
use crate::{
    concrete::{self, Ipv4, Ipv6},
    traits, Error,
};

/// The length of either an IPv4 or IPv6 prefix.
///
/// # Examples
///
/// ``` rust
/// use ip::{traits::Prefix as _, Any, Prefix, PrefixLength};
///
/// let prefix = "2001:db8::/32".parse::<Prefix<Any>>()?;
///
/// assert!(matches!(prefix.prefix_len(), PrefixLength::<Any>::Ipv6(_)));
/// # Ok::<(), ip::Error>(())
/// ```
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd)]
pub enum Length {
    /// IPv4 prefix length variant.
    Ipv4(concrete::PrefixLength<Ipv4>),
    /// IPv6 prefix length variant.
    Ipv6(concrete::PrefixLength<Ipv6>),
}

impl traits::PrefixLength for Length {
    fn increment(self) -> Result<Self, Error> {
        match self {
            Self::Ipv4(length) => length.increment().map(Self::from),
            Self::Ipv6(length) => length.increment().map(Self::from),
        }
    }

    fn decrement(self) -> Result<Self, Error> {
        match self {
            Self::Ipv4(length) => length.decrement().map(Self::from),
            Self::Ipv6(length) => length.decrement().map(Self::from),
        }
    }
}

impl From<concrete::PrefixLength<Ipv4>> for Length {
    fn from(length: concrete::PrefixLength<Ipv4>) -> Self {
        Self::Ipv4(length)
    }
}

impl From<concrete::PrefixLength<Ipv6>> for Length {
    fn from(length: concrete::PrefixLength<Ipv6>) -> Self {
        Self::Ipv6(length)
    }
}

impl AsRef<u8> for Length {
    delegate! {
        fn as_ref(&self) -> &u8;
    }
}

impl fmt::Display for Length {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Ipv4(len) => len.fmt(f),
            Self::Ipv6(len) => len.fmt(f),
        }
    }
}

#[cfg(any(test, feature = "arbitrary"))]
impl Arbitrary for Length {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
        prop_oneof![
            any::<concrete::PrefixLength<Ipv4>>().prop_map(Self::Ipv4),
            any::<concrete::PrefixLength<Ipv6>>().prop_map(Self::Ipv6),
        ]
        .boxed()
    }
}