use std::error::Error as StdError;
use std::fmt;
use std::borrow::Cow;
use super::Offset;
#[derive(Debug)]
pub enum Error {
UnexpectedlyShortPayload {
actual_size: Offset,
minimum_size: Offset,
},
IncorrectBoolean {
position: Offset,
value: u8,
},
UnsupportedFloat {
position: Offset,
value: f64,
},
IncorrectSegmentReference {
position: Offset,
value: Offset,
},
IncorrectSegmentSize {
position: Offset,
value: Offset,
},
UnexpectedlyShortRawMessage {
position: Offset,
size: Offset,
},
IncorrectSizeOfRawMessage {
position: Offset,
actual_size: Offset,
declared_size: Offset,
},
IncorrectMessageType {
message_type: u16,
},
IncorrectServiceId {
service_id: u16,
},
IncorrectNetworkId {
network_id: u8,
},
UnsupportedProtocolVersion {
version: u8,
},
OverlappingSegment {
last_end: Offset,
start: Offset,
},
SpaceBetweenSegments {
last_end: Offset,
start: Offset,
},
Utf8 {
position: Offset,
error: ::std::str::Utf8Error,
},
OffsetOverflow,
Basic(Cow<'static, str>),
Other(Box<StdError>),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} = {:?}", self.description(), self)
}
}
impl StdError for Error {
fn description(&self) -> &str {
match *self {
Error::UnexpectedlyShortPayload { .. } => "Unexpectedly short payload",
Error::IncorrectBoolean { .. } => "Incorrect boolean value",
Error::UnsupportedFloat { .. } => "Unsupported float value",
Error::IncorrectSegmentReference { .. } => "Incorrect segment reference",
Error::IncorrectSegmentSize { .. } => "Incorrect segment size",
Error::UnexpectedlyShortRawMessage { .. } => "Unexpectedly short RawMessage",
Error::IncorrectSizeOfRawMessage { .. } => "Incorrect size of RawMessage",
Error::IncorrectMessageType { .. } => "Incorrect message type",
Error::IncorrectServiceId { .. } => "Incorrect service id",
Error::IncorrectNetworkId { .. } => "Incorrect network id",
Error::UnsupportedProtocolVersion { .. } => "Unsupported protocol version",
Error::OverlappingSegment { .. } => "Overlapping segments",
Error::SpaceBetweenSegments { .. } => "Space between segments",
Error::Utf8 { .. } => "Utf8 error in parsing string",
Error::OffsetOverflow => "Offset pointers overflow",
Error::Basic(ref x) => x.as_ref(),
Error::Other(_) => "Other error",
}
}
fn cause(&self) -> Option<&StdError> {
use std::ops::Deref;
if let Error::Other(ref error) = *self {
Some(error.deref())
} else {
None
}
}
}
impl From<Box<StdError>> for Error {
fn from(t: Box<StdError>) -> Error {
Error::Other(t)
}
}
impl From<Cow<'static, str>> for Error {
fn from(t: Cow<'static, str>) -> Error {
Error::Basic(t)
}
}
impl From<&'static str> for Error {
fn from(t: &'static str) -> Error {
Error::Basic(t.into())
}
}