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
//! Standard Ip prefixes.
//!
//! This module implements [`IpPrefix`] trait for:
//! * [`Ipv4Prefix`], generic Ipv4 prefix, similar to [`Ipv4Net`] but with trailing bits
//!   guaranteed to equal `0`
//! * [`Ipv6Prefix`], generic Ipv6 prefix, similar to [`Ipv6Net`] but with trailing bits
//!   guaranteed to equal `0`
//! * [`Ipv4Addr`], Ipv4 address viewed as a prefix with length of 32 bits
//! * [`Ipv6Addr`], Ipv6 address viewed as a prefix with length of 128 bits
//! * [`Ipv4Net`] with a small extra cost to deal with non null trailing bits
//! * [`Ipv6Net`] with a small extra cost to deal with non null trailing bits

use super::*;

/// An Ipv4 prefix similar to [`Ipv4Net`] but with trailing bits
///  guaranteed to equal `0`
#[derive(Copy, Clone, Default, Eq, PartialEq, Hash)]
pub struct Ipv4Prefix {
    pub(super) addr: u32,
    pub(super) len: u8
}

impl Ipv4Prefix {
    /// Build a new prefix.
    ///
    /// All the bits greater than the prefix length are set to `0``.
    /// If the specified length is greater than the maximum allowed,
    /// an error is returned.
    ///
    /// # Example
    /// ```
    /// use std::net::{Ipv4Addr, Ipv6Addr};
    ///     use ipnet::PrefixLenError;
    /// #     use iptrie::*;
    ///     let ipv4 = "1.2.3.4".parse::<Ipv4Addr>().unwrap();
    ///     let ipv6 = "1:2:3:4::".parse::<Ipv6Addr>().unwrap();
    ///
    ///     assert!( Ipv4Prefix::new(ipv4, 12).is_ok());
    ///
    ///     assert_eq!( Ipv4Prefix::new(ipv4, 64), Err(IpPrefixError::PrefixLenError));
    /// ```
    #[inline]
    pub fn new(addr: Ipv4Addr, len: u8) -> Result<Self, IpPrefixError>
    {
        if len > Self::MAX_LEN {
            Err(IpPrefixError::PrefixLenError)
        } else {
            Ok( Self { addr: u32::from(addr) & u32::bitmask(len), len })
        }
    }
}

/// An Ipv4 prefix similar to [`Ipv6Net`] but with trailing bits
///  guaranteed to equal `0`
#[derive(Copy, Clone, Default, Eq, PartialEq, Hash)]
pub struct Ipv6Prefix {
    pub(super) addr: u128,
    pub(super) len: u8
}


impl Ipv6Prefix {
    /// Build a new prefix.
    ///
    /// All the bits greater than the prefix length are set to `0``.
    /// If the specified length is greater than the maximum allowed,
    /// an error is returned.
    ///
    /// # Example
    /// ```
    /// use std::net::{Ipv4Addr, Ipv6Addr};
    ///     use ipnet::PrefixLenError;
    /// #     use iptrie::*;
    ///     let ipv4 = "1.2.3.4".parse::<Ipv4Addr>().unwrap();
    ///     let ipv6 = "1:2:3:4::".parse::<Ipv6Addr>().unwrap();
    ///
    ///     assert!( Ipv6Prefix::new(ipv6, 78).is_ok());
    ///
    ///     assert_eq!( Ipv6Prefix::new(ipv6, 133), Err(IpPrefixError::PrefixLenError));
    /// ```
    #[inline]
    pub fn new(addr: Ipv6Addr, len: u8) -> Result<Self, IpPrefixError>
    {
        if len > Self::MAX_LEN {
            Err(IpPrefixError::PrefixLenError)
        } else {
            Ok( Self { addr: u128::from(addr) & u128::bitmask(len), len })
        }
    }
}



macro_rules! ipprefix {

    ($ipaddr:ty, $ipnet:ty, $prefix:ident, $slot:ty) => {

        impl IpPrefix for $prefix
        {
            type Slot = $slot;
            #[inline] fn root() -> Self { Self { addr: 0, len: 0 } }
            #[inline] fn bitslot(&self) -> Self::Slot { self.addr }
            #[inline] fn bitslot_trunc(&self) -> Self::Slot { self.addr }
            #[inline] fn len(&self) -> u8 { self.len }

            const MAX_LEN: u8 = <Self as IpPrefix>::Slot::LEN;
            type Addr = $ipaddr;
            #[inline] fn network(&self) -> Self::Addr { self.addr.into() }
        }

        impl From<$ipnet> for $prefix
        {
            #[inline] fn from(value: $ipnet) -> Self {
                Self { addr: value.trunc().addr().into(), len: value.prefix_len() }
            }
        }
        impl From<$prefix> for $ipnet
        {
            #[inline] fn from(value: $prefix) -> Self {
                <$ipnet>::new(value.addr.into(), value.len()).unwrap()
            }
        }
        impl Display for $prefix {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                let ip = (*self).into();
                <$ipnet as fmt::Display>::fmt(&ip, f)
            }
        }
        impl Debug for $prefix {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                let ip = (*self).into();
                <$ipnet as fmt::Display>::fmt(&ip, f)
            }
        }
        impl FromStr for $prefix {
            type Err = IpPrefixError;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                Ok(<$prefix>::from(<$ipnet>::from_str(s)?))
            }
        }
        impl From<$ipaddr> for $prefix
        {
            #[inline] fn from(value: $ipaddr) -> Self {
                Self { addr: value.into(), len: Self::MAX_LEN }
            }
        }

        impl IpPrefix for $ipnet
        {
            type Slot = $slot;
            #[inline] fn root() -> Self { Self::default() }
            #[inline] fn bitslot(&self) -> Self::Slot { <$slot>::from(self.addr()) }
            #[inline] fn bitslot_trunc(&self) -> Self::Slot { <$slot>::from(self.network()) }
            #[inline] fn len(&self) -> u8 { self.prefix_len() }

            const MAX_LEN: u8 = <Self as IpPrefix>::Slot::LEN;
            type Addr = $ipaddr;
            #[inline] fn network(&self) -> Self::Addr { self.network() }
        }

        impl IpPrefix for $ipaddr
        {
            type Slot = $slot;
            #[inline] fn root() -> Self { 0.into() }
            #[inline] fn bitslot(&self) -> Self::Slot { Self::Slot::from(*self) }
            #[inline] fn bitslot_trunc(&self) -> Self::Slot { self.bitslot() }
            #[inline] fn len(&self) -> u8 { Self::Slot::LEN }

            const MAX_LEN: u8 = <Self as IpPrefix>::Slot::LEN;
            type Addr = $ipaddr;
            #[inline] fn network(&self) -> Self::Addr { *self }
        }
    }
}

ipprefix!(Ipv4Addr, Ipv4Net, Ipv4Prefix, u32);
ipprefix!(Ipv6Addr, Ipv6Net, Ipv6Prefix, u128);