iptrie/prefix/mod.rs
1//! IP Prefixes types and utilities
2//!
3
4mod cover;
5mod ipstd;
6mod slot;
7
8mod network;
9mod private;
10mod shorten;
11#[cfg(test)]
12mod tests;
13
14pub use cover::*;
15pub use ipstd::*;
16pub use network::Ipv6NetPrefix;
17pub use shorten::*;
18pub use slot::*;
19use std::error::Error;
20
21use std::fmt;
22use std::fmt::{Debug, Display};
23use std::hash::Hash;
24use std::net::{Ipv4Addr, Ipv6Addr};
25use std::str::FromStr;
26
27pub use crate::prefix::private::IpPrivatePrefix;
28use ipnet::{Ipv4Net, Ipv6Net};
29
30pub trait IpRootPrefix: IpPrefix {
31 /// Root prefix has a length of 0
32 fn root() -> Self; // root prefix, of len =0
33}
34
35/// Ip prefix (as bit prefix)
36#[allow(clippy::len_without_is_empty)]
37pub trait IpPrefix: IpPrivatePrefix + Debug + Clone + Copy {
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 {
68 <Self::Slot as BitSlot>::bitmask(self.len())
69 }
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/// 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 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
107 match self {
108 IpPrefixError::PrefixLenError => fmt.write_str("invalid IP prefix length"),
109 IpPrefixError::AddrParseError => fmt.write_str("invalid IP address syntax"),
110 }
111 }
112}
113
114impl Error for IpPrefixError {}
115
116impl From<ipnet::AddrParseError> for IpPrefixError {
117 fn from(_: ipnet::AddrParseError) -> Self {
118 IpPrefixError::AddrParseError
119 }
120}
121
122impl From<std::net::AddrParseError> for IpPrefixError {
123 fn from(_: std::net::AddrParseError) -> Self {
124 IpPrefixError::AddrParseError
125 }
126}
127
128impl From<ipnet::PrefixLenError> for IpPrefixError {
129 fn from(_: ipnet::PrefixLenError) -> Self {
130 IpPrefixError::PrefixLenError
131 }
132}