ipnetwork/
error.rs
1use std::{error::Error, fmt, net::AddrParseError};
2
3use crate::error::IpNetworkError::*;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum IpNetworkError {
9 InvalidAddr(String),
10 InvalidPrefix,
11 InvalidCidrFormat(String),
12 NetworkSizeError(NetworkSizeError),
13}
14
15impl fmt::Display for IpNetworkError {
16 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17 match *self {
18 InvalidAddr(ref s) => write!(f, "invalid address: {s}"),
19 InvalidPrefix => write!(f, "invalid prefix"),
20 InvalidCidrFormat(ref s) => write!(f, "invalid cidr format: {s}"),
21 NetworkSizeError(ref e) => write!(f, "network size error: {e}"),
22 }
23 }
24}
25
26impl Error for IpNetworkError {
27 fn description(&self) -> &str {
28 match *self {
29 InvalidAddr(_) => "address is invalid",
30 InvalidPrefix => "prefix is invalid",
31 InvalidCidrFormat(_) => "cidr is invalid",
32 NetworkSizeError(_) => "network size error",
33 }
34 }
35}
36
37impl From<AddrParseError> for IpNetworkError {
38 fn from(e: AddrParseError) -> Self {
39 InvalidAddr(e.to_string())
40 }
41}
42
43#[derive(Copy, Clone, Debug, PartialEq, Eq)]
45#[non_exhaustive]
46pub enum NetworkSizeError {
47 NetworkIsTooLarge,
48}
49
50impl fmt::Display for NetworkSizeError {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
52 f.write_str("Network is too large to fit into an unsigned 32-bit integer!")
53 }
54}
55
56impl Error for NetworkSizeError {}