ipgen/
error.rs

1use ipnetwork::IpNetworkError;
2use std::{error, fmt};
3
4/// Errors returned by this crate
5#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
6#[non_exhaustive]
7pub enum Error {
8    /// The IP network address provided is invalid
9    ///
10    /// *NB:* for some unseen reason, the network and ip addresses
11    /// generated internally by this crate might fail to parse.
12    /// This is is obviously a bug and as such, are labelled `[BUG]`.
13    /// If you run into these kindly report them on the repo.
14    InvalidIpNetwork(String),
15    /// Network address provided is already a full IP address
16    PrefixTooBig(crate::IpNetwork),
17}
18
19impl error::Error for Error {}
20
21impl fmt::Display for Error {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        match self {
24            Error::InvalidIpNetwork(error) => write!(f, "{}", error),
25            Error::PrefixTooBig(crate::IpNetwork(net)) => write!(
26                f,
27                "{}/{} is already a full IP address",
28                net.ip(),
29                net.prefix()
30            ),
31        }
32    }
33}
34
35impl From<IpNetworkError> for Error {
36    fn from(error: IpNetworkError) -> Self {
37        Self::InvalidIpNetwork(error.to_string())
38    }
39}