iptrie/prefix/mod.rs
1//! IP Prefixes types and utilities
2//!
3
4mod slot;
5mod ipv6trunc;
6mod ipv6netaddr;
7mod ipstd;
8mod cover;
9
10#[cfg(test)] mod tests;
11mod private;
12mod shorten;
13
14use std::error::Error;
15pub use slot::*;
16pub use ipv6trunc::*;
17pub use ipv6netaddr::Ipv6NetAddr;
18pub use ipstd::*;
19pub use cover::*;
20pub use shorten::*;
21
22use std::fmt;
23use std::fmt::{Debug, Display};
24use std::hash::Hash;
25use std::net::{Ipv4Addr, Ipv6Addr};
26use std::str::FromStr;
27
28use ipnet::{Ipv4Net, Ipv6Net};
29pub use crate::prefix::private::IpPrivatePrefix;
30
31pub trait IpRootPrefix: IpPrefix {
32 /// Root prefix has a length of 0
33 fn root() -> Self; // root prefix, of len =0
34}
35
36/// Ip prefix (as bit prefix)
37#[allow(clippy::len_without_is_empty)]
38pub trait IpPrefix: IpPrivatePrefix+Debug+Clone+Copy
39{
40 /// The slot manipulated inside this prefix
41 type Slot: BitSlot;
42
43 /// The inner slot _as is_
44 ///
45 /// Depending of the implementation, there is no warranty that
46 /// the masked bits (i.e. with position greater than the length)
47 /// are set to 0.
48 ///
49 /// For a version of a slot with masked bits set to 0,
50 /// see [`Self::bitslot_trunc`].
51 fn bitslot(&self) -> Self::Slot;
52
53 /// The inner slot with all the masked bits set to 0.
54 ///
55 /// The result always equals `self.bitslot() & self.bitmask()`
56 /// but it is usually faster to compute (depending on the prefix structure)
57 fn bitslot_trunc(&self) -> Self::Slot;
58
59 /// Length of the prefix.
60 ///
61 /// This is the number of significant first bits.
62 /// Others should be considered as 0 (event if not)
63 fn len(&self) -> u8;
64
65 /// Mask of the prefix.
66 ///
67 /// The n (prefix length) first bits are set to 1 and the last ones are set to 0.
68 #[inline]
69 fn bitmask(&self) -> Self::Slot { <Self::Slot as BitSlot>::bitmask(self.len()) }
70
71 /// The maximum allowed length for this prefix
72 const MAX_LEN: u8;
73
74 /// The underlying ip address (usually Ipv4Addr or Ipv6Addr)
75 type Addr: Display+Clone+Copy+Eq+Hash;
76
77 /// The address of the network defined by the prefixv
78 ///
79 /// All the bits greater than the prefix length are set to `0`
80 fn network(&self) -> Self::Addr;
81}
82
83
84
85/// Error generated when building an Ip prefix
86#[derive(Debug,PartialEq,Eq,Copy, Clone)]
87pub enum IpPrefixError {
88 /// The specified length of the prefix is not valid.
89 ///
90 /// For Ipv4, this error is generated if the specified length
91 /// is greater than 32 for an [`Ipv4Prefix`] or [`Ipv4Net`].
92 ///
93 /// For Ipv6, this error is generated if the specified length
94 /// is greater than 128 for an [`Ipv6Prefix`] or [`Ipv6Net`]
95 /// or greater than 120 for an [`Ipv6Prefix120`]
96 /// or greater than 56 for an [`Ipv6Prefix56`]
97 /// or not equal to 64 for an [`Ipv6NetAddr`]
98 PrefixLenError,
99
100 /// The parsed string does not contains a valid Ip address.
101 ///
102 /// It occurs also if when parsing an Ipv4 (resp. Ipv6) address on a string
103 /// which contains an Ipv6 (resp. Ipv4) syntax.
104 AddrParseError,
105}
106
107impl Display for IpPrefixError
108{
109 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
110 match self {
111 IpPrefixError::PrefixLenError => {
112 fmt.write_str("invalid IP prefix length")
113 }
114 IpPrefixError::AddrParseError => {
115 fmt.write_str("invalid IP address syntax")
116 }
117 }
118 }
119}
120
121impl Error for IpPrefixError {}
122
123impl From<ipnet::AddrParseError> for IpPrefixError {
124 fn from(_: ipnet::AddrParseError) -> Self {
125 IpPrefixError::AddrParseError
126 }
127}
128
129impl From<std::net::AddrParseError> for IpPrefixError {
130 fn from(_: std::net::AddrParseError) -> Self {
131 IpPrefixError::AddrParseError
132 }
133}
134
135impl From<ipnet::PrefixLenError> for IpPrefixError {
136 fn from(_: ipnet::PrefixLenError) -> Self {
137 IpPrefixError::PrefixLenError
138 }
139}
140