use alloc::borrow::Cow;
use alloc::vec::Vec;
use core::fmt;
#[derive(Clone, PartialEq, Eq)]
pub struct DecodeError {
description: Cow<'static, str>,
stack: Vec<(&'static str, &'static str)>,
}
impl DecodeError {
#[doc(hidden)]
#[cold]
pub fn new(description: impl Into<Cow<'static, str>>) -> DecodeError {
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::Debug for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DecodeError").field("description", &self.description).field("stack", &self.stack).finish()
}
}
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 std::error::Error for DecodeError {}
impl From<DecodeError> for std::io::Error {
fn from(error: DecodeError) -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::InvalidData, error)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct EncodeError {
required: usize,
remaining: usize,
}
impl EncodeError {
pub(crate) const fn new(required: usize, remaining: usize) -> EncodeError {
EncodeError { required, remaining }
}
pub const fn required_capacity(&self) -> usize {
self.required
}
pub const fn remaining(&self) -> usize {
self.remaining
}
}
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"failed to encode Protobuf message; insufficient buffer capacity (required: {}, remaining: {})",
self.required, self.remaining
)
}
}
impl core::error::Error for EncodeError {}
impl From<EncodeError> for std::io::Error {
fn from(error: EncodeError) -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::InvalidInput, error)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct UnknownEnumValue(pub i32);
impl fmt::Display for UnknownEnumValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "unknown enumeration value {}", self.0)
}
}
impl core::error::Error for UnknownEnumValue {}
#[cfg(test)]
mod test {
use super::*;
use crate::alloc::string::ToString;
#[test]
fn test_push() {
let mut decode_error = DecodeError::new("something failed");
decode_error.push("Foo bad", "bar.foo");
decode_error.push("Baz bad", "bar.baz");
assert_eq!(
decode_error.to_string(),
"failed to decode Protobuf message: Foo bad.bar.foo: Baz bad.bar.baz: something failed"
);
}
#[test]
fn test_into_std_io_error() {
let decode_error = DecodeError::new("something failed");
let std_io_error = std::io::Error::from(decode_error);
assert_eq!(std_io_error.kind(), std::io::ErrorKind::InvalidData);
assert_eq!(std_io_error.to_string(), "failed to decode Protobuf message: something failed");
}
}