-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3bc252d
commit 5c91bd0
Showing
6 changed files
with
81 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use std::{error::Error, fmt}; | ||
|
||
use crate::error::IpNetworkError::*; | ||
|
||
/// Represents a bunch of errors that can occur while working with a `IpNetwork` | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
#[non_exhaustive] | ||
pub enum IpNetworkError { | ||
InvalidAddr(String), | ||
InvalidPrefix, | ||
InvalidCidrFormat(String), | ||
NetworkSizeError(NetworkSizeError) | ||
} | ||
|
||
impl fmt::Display for IpNetworkError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match *self { | ||
InvalidAddr(ref s) => write!(f, "invalid address: {s}"), | ||
InvalidPrefix => write!(f, "invalid prefix"), | ||
InvalidCidrFormat(ref s) => write!(f, "invalid cidr format: {s}"), | ||
NetworkSizeError(ref e) => write!(f, "network size error: {e}") | ||
} | ||
} | ||
} | ||
|
||
impl Error for IpNetworkError { | ||
fn description(&self) -> &str { | ||
match *self { | ||
InvalidAddr(_) => "address is invalid", | ||
InvalidPrefix => "prefix is invalid", | ||
InvalidCidrFormat(_) => "cidr is invalid", | ||
NetworkSizeError(_) => "network size error" | ||
} | ||
} | ||
} | ||
|
||
/// Cannot convert an IPv6 network size to a u32 as it is a 128-bit value. | ||
#[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
#[non_exhaustive] | ||
pub enum NetworkSizeError { | ||
NetworkIsTooLarge | ||
} | ||
|
||
impl fmt::Display for NetworkSizeError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.write_str("Network is too large to fit into an unsigned 32-bit integer!") | ||
} | ||
} | ||
|
||
impl Error for NetworkSizeError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters