cidr/
errors.rs

1//! Various error types returned by function in this crate
2
3use core::{
4	fmt,
5	num::ParseIntError,
6};
7#[cfg(feature = "std")]
8#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
9use std::error::Error;
10use std::net::AddrParseError;
11
12use crate::Family;
13
14/// Error returned when the network length was longer than the address
15#[derive(Clone, PartialEq, Eq, Hash)]
16pub struct NetworkLengthTooLongError(usize, Family);
17
18impl NetworkLengthTooLongError {
19	pub(crate) const fn new(len: usize, family: Family) -> Self {
20		NetworkLengthTooLongError(len, family)
21	}
22}
23
24impl fmt::Debug for NetworkLengthTooLongError {
25	fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
26		write!(
27			w,
28			"Network length {} is too long for {:?} (maximum: {})",
29			self.0,
30			self.1,
31			self.1.len()
32		)
33	}
34}
35impl fmt::Display for NetworkLengthTooLongError {
36	fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
37		fmt::Debug::fmt(self, w)
38	}
39}
40
41#[cfg(feature = "std")]
42#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
43impl Error for NetworkLengthTooLongError {
44	fn description(&self) -> &str {
45		"network length too long"
46	}
47}
48
49/// Error type returned when parsing IP networks
50#[derive(Clone, PartialEq)]
51pub enum NetworkParseError {
52	/// The host part wasn't zero but should have been. The [`Cidr`] types
53	/// require that you use the first address in the network (and the
54	/// network length) to represent the address, but it wasn't the
55	/// first address.
56	///
57	/// [`Cidr`]: crate::Cidr
58	InvalidHostPart,
59	/// Failed to parse the address
60	AddrParseError(AddrParseError),
61	/// Failed to parse the network length
62	NetworkLengthParseError(ParseIntError),
63	/// The network length was not valid (but was successfully parsed)
64	NetworkLengthTooLongError(NetworkLengthTooLongError),
65}
66impl fmt::Debug for NetworkParseError {
67	fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
68		match self {
69			Self::InvalidHostPart => write!(w, "host part of address was not zero"),
70			Self::AddrParseError(e) => {
71				write!(w, "couldn't parse address in network: {}", e)
72			},
73			Self::NetworkLengthParseError(e) => {
74				write!(w, "couldn't parse length in network: {}", e)
75			},
76			Self::NetworkLengthTooLongError(e) => {
77				write!(w, "invalid length for network: {}", e)
78			},
79		}
80	}
81}
82impl fmt::Display for NetworkParseError {
83	fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
84		fmt::Debug::fmt(self, w)
85	}
86}
87
88#[cfg(feature = "std")]
89#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
90impl Error for NetworkParseError {
91	fn description(&self) -> &str {
92		"network parse error"
93	}
94
95	fn source(&self) -> Option<&(dyn Error + 'static)> {
96		match self {
97			Self::InvalidHostPart => None,
98			Self::AddrParseError(e) => Some(e),
99			Self::NetworkLengthParseError(e) => Some(e),
100			Self::NetworkLengthTooLongError(e) => Some(e),
101		}
102	}
103}
104
105impl From<AddrParseError> for NetworkParseError {
106	fn from(e: AddrParseError) -> Self {
107		NetworkParseError::AddrParseError(e)
108	}
109}
110
111impl From<ParseIntError> for NetworkParseError {
112	fn from(e: ParseIntError) -> Self {
113		NetworkParseError::NetworkLengthParseError(e)
114	}
115}
116
117impl From<NetworkLengthTooLongError> for NetworkParseError {
118	fn from(e: NetworkLengthTooLongError) -> Self {
119		NetworkParseError::NetworkLengthTooLongError(e)
120	}
121}
122
123/// Error type returned when creating [`Inet`] pair
124///
125/// [`Inet`]: crate::Inet
126#[derive(Clone, PartialEq)]
127pub enum InetTupleError {
128	/// The given addresses are not in the same network
129	NotInSharedNetwork,
130	/// The network length was not valid (but was successfully parsed)
131	NetworkLengthTooLongError(NetworkLengthTooLongError),
132}
133
134impl fmt::Debug for InetTupleError {
135	fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
136		match self {
137			Self::NotInSharedNetwork => write!(w, "addresses not in shared network"),
138			Self::NetworkLengthTooLongError(e) => {
139				write!(w, "invalid length for network: {}", e)
140			},
141		}
142	}
143}
144
145impl fmt::Display for InetTupleError {
146	fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
147		fmt::Debug::fmt(self, w)
148	}
149}
150
151#[cfg(feature = "std")]
152#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
153impl Error for InetTupleError {
154	fn description(&self) -> &str {
155		"inet tuple error"
156	}
157
158	fn source(&self) -> Option<&(dyn Error + 'static)> {
159		match self {
160			Self::NotInSharedNetwork => None,
161			Self::NetworkLengthTooLongError(e) => Some(e),
162		}
163	}
164}
165
166impl From<NetworkLengthTooLongError> for InetTupleError {
167	fn from(e: NetworkLengthTooLongError) -> Self {
168		InetTupleError::NetworkLengthTooLongError(e)
169	}
170}