ip/any/
af.rs

1use core::cmp::Ordering;
2
3#[cfg(feature = "std")]
4use super::PrefixSet;
5use super::{Address, Bitmask, Hostmask, Interface, Netmask, Prefix, PrefixLength, PrefixRange};
6use crate::{concrete, traits};
7
8/// The class of address families consisting of `{ IPv4, IPv6 }`.
9#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
10pub enum Any {}
11
12impl traits::AfiClass for Any {
13    type Address = Address;
14    type Interface = Interface;
15    type PrefixLength = PrefixLength;
16    type Prefix = Prefix;
17    type Netmask = Netmask;
18    type Hostmask = Hostmask;
19    type Bitmask = Bitmask;
20    type PrefixRange = PrefixRange;
21
22    #[cfg(feature = "std")]
23    type PrefixSet = PrefixSet;
24
25    fn as_afi_class() -> AfiClass {
26        AfiClass::Any
27    }
28}
29
30/// Enumeration of address family classes.
31#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
32pub enum AfiClass {
33    /// Variant representing the class `{ IPv4 }`.
34    Ipv4,
35    /// Variant representing the class `{ IPv6 }`.
36    Ipv6,
37    /// Variant representing the [`Any`] class.
38    Any,
39}
40
41impl From<concrete::Afi> for AfiClass {
42    fn from(afi: concrete::Afi) -> Self {
43        match afi {
44            concrete::Afi::Ipv4 => Self::Ipv4,
45            concrete::Afi::Ipv6 => Self::Ipv6,
46        }
47    }
48}
49
50impl PartialOrd for AfiClass {
51    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
52        match (self, other) {
53            _ if self == other => Some(Ordering::Equal),
54            (Self::Any, Self::Ipv4 | Self::Ipv6) => Some(Ordering::Greater),
55            (Self::Ipv4 | Self::Ipv6, Self::Any) => Some(Ordering::Less),
56            _ => None,
57        }
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn any_contains_ipv4() {
67        assert!(AfiClass::Any > AfiClass::Ipv4);
68    }
69
70    #[test]
71    fn ipv6_contained_in_any() {
72        assert!(AfiClass::Ipv6 < AfiClass::Any);
73    }
74}