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

#[cfg(feature = "std")]
use super::PrefixSet;
use super::{Address, Bitmask, Hostmask, Interface, Netmask, Prefix, PrefixLength, PrefixRange};
use crate::{concrete, traits};

/// The class of address families consisting of `{ IPv4, IPv6 }`.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Any {}

impl traits::AfiClass for Any {
    type Address = Address;
    type Interface = Interface;
    type PrefixLength = PrefixLength;
    type Prefix = Prefix;
    type Netmask = Netmask;
    type Hostmask = Hostmask;
    type Bitmask = Bitmask;
    type PrefixRange = PrefixRange;

    #[cfg(feature = "std")]
    type PrefixSet = PrefixSet;

    fn as_afi_class() -> AfiClass {
        AfiClass::Any
    }
}

/// Enumeration of address family classes.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum AfiClass {
    /// Variant representing the class `{ IPv4 }`.
    Ipv4,
    /// Variant representing the class `{ IPv6 }`.
    Ipv6,
    /// Variant representing the [`Any`] class.
    Any,
}

impl From<concrete::Afi> for AfiClass {
    fn from(afi: concrete::Afi) -> Self {
        match afi {
            concrete::Afi::Ipv4 => Self::Ipv4,
            concrete::Afi::Ipv6 => Self::Ipv6,
        }
    }
}

impl PartialOrd for AfiClass {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match (self, other) {
            _ if self == other => Some(Ordering::Equal),
            (Self::Any, Self::Ipv4 | Self::Ipv6) => Some(Ordering::Greater),
            (Self::Ipv4 | Self::Ipv6, Self::Any) => Some(Ordering::Less),
            _ => None,
        }
    }
}

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

    #[test]
    fn any_contains_ipv4() {
        assert!(AfiClass::Any > AfiClass::Ipv4);
    }

    #[test]
    fn ipv6_contained_in_any() {
        assert!(AfiClass::Ipv6 < AfiClass::Any);
    }
}