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