From c50ccbbd1a9b3780a367e0259d04e6c143197e3b Mon Sep 17 00:00:00 2001 From: "Sunrin SHIMURA (keen)" <3han5chou7@gmail.com> Date: Thu, 23 Feb 2017 13:29:31 +0900 Subject: [PATCH] implement Error for IpNetworkError --- src/common.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/common.rs b/src/common.rs index 0465774..9df6f3c 100644 --- a/src/common.rs +++ b/src/common.rs @@ -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::>(); if parts.len() == 2 {