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