1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
use std::error::Error;
use std::fmt;
use std::net::AddrParseError;
use std::num::ParseIntError;

use super::Family;

/// Error returned when the network length was longer than the address
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct NetworkLengthTooLongError(usize, Family);

impl NetworkLengthTooLongError {
	#[doc(hidden)]
	pub fn new(len: usize, family: Family) -> Self {
		NetworkLengthTooLongError(len, family)
	}
}

impl fmt::Debug for NetworkLengthTooLongError {
	fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
		write!(
			w,
			"Network length {} is too long for {:?} (maximum: {})",
			self.0,
			self.1,
			self.1.len()
		)
	}
}
impl fmt::Display for NetworkLengthTooLongError {
	fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
		fmt::Debug::fmt(self, w)
	}
}

impl Error for NetworkLengthTooLongError {
	fn description(&self) -> &str {
		"network length too long"
	}
}

/// Error type returned when parsing IP networks
#[derive(Clone, PartialEq)]
pub enum NetworkParseError {
	/// The host part wasn't zero but should have been. The `Cidr` types
	/// require that you use the first address in the network (and the
	/// network length) to represent the address, but it wasn't the
	/// first address.
	InvalidHostPart,
	/// Failed to parse the address
	AddrParseError(AddrParseError),
	/// Failed to parse the network length
	NetworkLengthParseError(ParseIntError),
	/// The network length was not valid (but was successfully parsed)
	NetworkLengthTooLongError(NetworkLengthTooLongError),
}
impl fmt::Debug for NetworkParseError {
	fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
		match *self {
			NetworkParseError::InvalidHostPart => {
				write!(w, "host part of address was not zero")
			},
			NetworkParseError::AddrParseError(ref e) => {
				write!(w, "couldn't parse address in network: {}", e)
			},
			NetworkParseError::NetworkLengthParseError(ref e) => {
				write!(w, "couldn't parse length in network: {}", e)
			},
			NetworkParseError::NetworkLengthTooLongError(ref e) => {
				write!(w, "invalid length for network: {}", e)
			},
		}
	}
}
impl fmt::Display for NetworkParseError {
	fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
		fmt::Debug::fmt(self, w)
	}
}

impl Error for NetworkParseError {
	fn description(&self) -> &str {
		"network parse error"
	}
	fn cause(&self) -> Option<&Error> {
		match *self {
			NetworkParseError::InvalidHostPart => None,
			NetworkParseError::AddrParseError(ref e) => Some(e),
			NetworkParseError::NetworkLengthParseError(ref e) => Some(e),
			NetworkParseError::NetworkLengthTooLongError(ref e) => Some(e),
		}
	}
}

impl From<AddrParseError> for NetworkParseError {
	fn from(e: AddrParseError) -> Self {
		NetworkParseError::AddrParseError(e)
	}
}

impl From<ParseIntError> for NetworkParseError {
	fn from(e: ParseIntError) -> Self {
		NetworkParseError::NetworkLengthParseError(e)
	}
}

impl From<NetworkLengthTooLongError> for NetworkParseError {
	fn from(e: NetworkLengthTooLongError) -> Self {
		NetworkParseError::NetworkLengthTooLongError(e)
	}
}