cidr/
errors.rs

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
//! Various error types returned by function in this crate

use core::{
	fmt,
	num::ParseIntError,
};
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
use std::error::Error;
use std::net::AddrParseError;

use crate::Family;

/// Error returned when the network length was longer than the address
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct NetworkLengthTooLongError(usize, Family);

impl NetworkLengthTooLongError {
	pub(crate) const fn new(len: usize, family: Family) -> Self {
		NetworkLengthTooLongError(len, family)
	}
}

impl fmt::Debug for NetworkLengthTooLongError {
	fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(
			w,
			"Network length {} is too long for {:?} (maximum: {})",
			self.0,
			self.1,
			self.1.len()
		)
	}
}
impl fmt::Display for NetworkLengthTooLongError {
	fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
		fmt::Debug::fmt(self, w)
	}
}

#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl Error for NetworkLengthTooLongError {
	fn description(&self) -> &str {
		"network length too long"
	}
}

/// Error type returned when parsing IP networks
#[derive(Clone, PartialEq)]
pub enum NetworkParseError {
	/// The host part wasn't zero but should have been. The [`Cidr`] types
	/// require that you use the first address in the network (and the
	/// network length) to represent the address, but it wasn't the
	/// first address.
	///
	/// [`Cidr`]: crate::Cidr
	InvalidHostPart,
	/// Failed to parse the address
	AddrParseError(AddrParseError),
	/// Failed to parse the network length
	NetworkLengthParseError(ParseIntError),
	/// The network length was not valid (but was successfully parsed)
	NetworkLengthTooLongError(NetworkLengthTooLongError),
}
impl fmt::Debug for NetworkParseError {
	fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			Self::InvalidHostPart => write!(w, "host part of address was not zero"),
			Self::AddrParseError(e) => {
				write!(w, "couldn't parse address in network: {}", e)
			},
			Self::NetworkLengthParseError(e) => {
				write!(w, "couldn't parse length in network: {}", e)
			},
			Self::NetworkLengthTooLongError(e) => {
				write!(w, "invalid length for network: {}", e)
			},
		}
	}
}
impl fmt::Display for NetworkParseError {
	fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
		fmt::Debug::fmt(self, w)
	}
}

#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl Error for NetworkParseError {
	fn description(&self) -> &str {
		"network parse error"
	}

	fn source(&self) -> Option<&(dyn Error + 'static)> {
		match self {
			Self::InvalidHostPart => None,
			Self::AddrParseError(e) => Some(e),
			Self::NetworkLengthParseError(e) => Some(e),
			Self::NetworkLengthTooLongError(e) => Some(e),
		}
	}
}

impl From<AddrParseError> for NetworkParseError {
	fn from(e: AddrParseError) -> Self {
		NetworkParseError::AddrParseError(e)
	}
}

impl From<ParseIntError> for NetworkParseError {
	fn from(e: ParseIntError) -> Self {
		NetworkParseError::NetworkLengthParseError(e)
	}
}

impl From<NetworkLengthTooLongError> for NetworkParseError {
	fn from(e: NetworkLengthTooLongError) -> Self {
		NetworkParseError::NetworkLengthTooLongError(e)
	}
}

/// Error type returned when creating [`Inet`] pair
///
/// [`Inet`]: crate::Inet
#[derive(Clone, PartialEq)]
pub enum InetTupleError {
	/// The given addresses are not in the same network
	NotInSharedNetwork,
	/// The network length was not valid (but was successfully parsed)
	NetworkLengthTooLongError(NetworkLengthTooLongError),
}

impl fmt::Debug for InetTupleError {
	fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			Self::NotInSharedNetwork => write!(w, "addresses not in shared network"),
			Self::NetworkLengthTooLongError(e) => {
				write!(w, "invalid length for network: {}", e)
			},
		}
	}
}

impl fmt::Display for InetTupleError {
	fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
		fmt::Debug::fmt(self, w)
	}
}

#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl Error for InetTupleError {
	fn description(&self) -> &str {
		"inet tuple error"
	}

	fn source(&self) -> Option<&(dyn Error + 'static)> {
		match self {
			Self::NotInSharedNetwork => None,
			Self::NetworkLengthTooLongError(e) => Some(e),
		}
	}
}

impl From<NetworkLengthTooLongError> for InetTupleError {
	fn from(e: NetworkLengthTooLongError) -> Self {
		InetTupleError::NetworkLengthTooLongError(e)
	}
}