use std::borrow::Cow;
use std::error;
use std::fmt;
use std::io;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DecodeError {
description: Cow<'static, str>,
stack: Vec<(&'static str, &'static str)>,
}
impl DecodeError {
#[doc(hidden)]
pub fn new<S>(description: S) -> DecodeError where S: Into<Cow<'static, str>> {
DecodeError {
description: description.into(),
stack: Vec::new(),
}
}
#[doc(hidden)]
pub fn push(&mut self, message: &'static str, field: &'static str) {
self.stack.push((message, field));
}
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("failed to decode Protobuf message: ")?;
for &(message, field) in &self.stack {
write!(f, "{}.{}: ", message, field)?;
}
f.write_str(&self.description)
}
}
impl error::Error for DecodeError {
fn description(&self) -> &str {
&self.description
}
}
impl From<DecodeError> for io::Error {
fn from(error: DecodeError) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, error)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct EncodeError {
required: usize,
remaining: usize,
}
impl EncodeError {
pub(crate) fn new(required: usize, remaining: usize) -> EncodeError {
EncodeError {
required,
remaining,
}
}
pub fn required_capacity(&self) -> usize {
self.required
}
pub fn remaining(&self) -> usize {
self.remaining
}
}
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(error::Error::description(self))?;
write!(f, " (required: {}, remaining: {})", self.required, self.remaining)
}
}
impl error::Error for EncodeError {
fn description(&self) -> &str {
"failed to encode Protobuf message: insufficient buffer capacity"
}
}
impl From<EncodeError> for io::Error {
fn from(error: EncodeError) -> io::Error {
io::Error::new(io::ErrorKind::InvalidInput, error)
}
}