// SPDX-License-Identifier: Apache-2.0 use alloc::string::{String, ToString}; use core::fmt::{Debug, Display, Formatter, Result}; use serde::de::{Error as DeError, StdError}; /// An error occurred during deserialization #[derive(Debug)] pub enum Error { /// An error occurred while reading bytes /// /// Contains the underlying error returned while reading. Io(T), /// An error occurred while parsing bytes /// /// Contains the offset into the stream where the syntax error occurred. Syntax(usize), /// An error occurred while processing a parsed value /// /// Contains a description of the error that occurred and (optionally) /// the offset into the stream indicating the start of the item being /// processed when the error occurred. Semantic(Option, String), /// The input caused serde to recurse too much /// /// This error prevents a stack overflow. RecursionLimitExceeded, } impl Error { /// A helper method for composing a semantic error #[inline] pub fn semantic(offset: impl Into>, msg: impl Into) -> Self { Self::Semantic(offset.into(), msg.into()) } } impl From for Error { #[inline] fn from(value: T) -> Self { Error::Io(value) } } impl From> for Error { #[inline] fn from(value: ciborium_ll::Error) -> Self { match value { ciborium_ll::Error::Io(x) => Self::Io(x), ciborium_ll::Error::Syntax(x) => Self::Syntax(x), } } } impl Display for Error { #[inline] fn fmt(&self, f: &mut Formatter<'_>) -> Result { write!(f, "{:?}", self) } } impl StdError for Error {} impl DeError for Error { #[inline] fn custom(msg: U) -> Self { Self::Semantic(None, msg.to_string()) } }