mirror of
https://github.com/achanda/ipnetwork.git
synced 2025-06-15 16:43:00 +00:00
Merge pull request #38 from KeenS/impl_error
implement Error for IpNetworkError
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
use std::net::Ipv4Addr;
|
||||
use std::fmt;
|
||||
use std::error::Error;
|
||||
|
||||
/// Represents a bunch of errors that can occur while working with a `IpNetwork`
|
||||
#[derive(Debug,Clone,PartialEq,Eq)]
|
||||
@ -8,6 +10,29 @@ pub enum IpNetworkError {
|
||||
InvalidCidrFormat(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for IpNetworkError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use IpNetworkError::*;
|
||||
match *self {
|
||||
InvalidAddr(ref s) => write!(f, "invalid address: {}", s),
|
||||
InvalidPrefix => write!(f, "invalid prifex"),
|
||||
InvalidCidrFormat(ref s) => write!(f, "invalid cidr format: {}", s),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for IpNetworkError {
|
||||
fn description(&self) -> &str {
|
||||
use IpNetworkError::*;
|
||||
match *self {
|
||||
InvalidAddr(_) => "address is invalid",
|
||||
InvalidPrefix => "prefix is invalid",
|
||||
InvalidCidrFormat(_) => "cidr is invalid",
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cidr_parts(cidr: &str) -> Result<(&str, &str), IpNetworkError> {
|
||||
let parts = cidr.split('/').collect::<Vec<&str>>();
|
||||
if parts.len() == 2 {
|
||||
|
Reference in New Issue
Block a user