minicbor/encode/
error.rs
use core::fmt;
#[cfg(feature = "alloc")]
use alloc::string::ToString;
#[derive(Debug)]
pub struct Error<E> {
err: ErrorImpl<E>,
#[cfg(not(feature = "alloc"))]
msg: &'static str,
#[cfg(feature = "alloc")]
msg: alloc::string::String
}
impl<E> Error<E> {
#[cfg(not(feature = "alloc"))]
pub fn message(msg: &'static str) -> Self {
Error { err: ErrorImpl::Message, msg }
}
#[cfg(feature = "alloc")]
pub fn message<T: fmt::Display>(msg: T) -> Self {
Error { err: ErrorImpl::Message, msg: msg.to_string() }
}
pub fn write(e: E) -> Self {
Error { err: ErrorImpl::Write(e), msg: Default::default() }
}
#[cfg(feature = "std")]
pub fn custom<T: std::error::Error + Send + Sync + 'static>(err: T) -> Self {
Error { err: ErrorImpl::Custom(Box::new(err)), msg: Default::default() }
}
#[cfg(not(feature = "alloc"))]
pub fn with_message(mut self, msg: &'static str) -> Self {
self.msg = msg;
self
}
#[cfg(feature = "alloc")]
pub fn with_message<T: fmt::Display>(mut self, msg: T) -> Self {
self.msg = msg.to_string();
self
}
pub fn is_message(&self) -> bool {
matches!(self.err, ErrorImpl::Message)
}
pub fn is_write(&self) -> bool {
matches!(self.err, ErrorImpl::Write(_))
}
#[cfg(feature = "std")]
pub fn is_custom(&self) -> bool {
matches!(self.err, ErrorImpl::Custom(_))
}
}
#[derive(Debug)]
enum ErrorImpl<E> {
Write(E),
Message,
#[cfg(feature = "std")]
Custom(Box<dyn std::error::Error + Send + Sync>)
}
impl<E: fmt::Display> fmt::Display for Error<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.err {
ErrorImpl::Message => write!(f, "{}", self.msg),
#[cfg(not(feature = "std"))]
ErrorImpl::Write(e) =>
if self.msg.is_empty() {
write!(f, "write error: {e}")
} else {
write!(f, "write error: {e}, {}", self.msg)
}
#[cfg(feature = "std")]
ErrorImpl::Write(_) =>
if self.msg.is_empty() {
write!(f, "write error")
} else {
write!(f, "write error: {}", self.msg)
}
#[cfg(feature = "std")]
ErrorImpl::Custom(_) =>
if self.msg.is_empty() {
write!(f, "encode error")
} else {
write!(f, "encode error: {}", self.msg)
}
}
}
}
#[cfg(feature = "std")]
impl<E: std::error::Error + 'static> std::error::Error for Error<E> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.err {
ErrorImpl::Message => None,
ErrorImpl::Write(e) => Some(e),
ErrorImpl::Custom(e) => Some(&**e)
}
}
}