1use alloc::string::{String, ToString};
4use core::fmt::{Debug, Display, Formatter, Result};
5
6use serde::de::{Error as DeError, StdError};
7
8#[derive(Debug)]
10pub enum Error<T> {
11    Io(T),
15
16    Syntax(usize),
20
21    Semantic(Option<usize>, String),
27
28    RecursionLimitExceeded,
32}
33
34impl<T> Error<T> {
35    #[inline]
37    pub fn semantic(offset: impl Into<Option<usize>>, msg: impl Into<String>) -> Self {
38        Self::Semantic(offset.into(), msg.into())
39    }
40}
41
42impl<T> From<T> for Error<T> {
43    #[inline]
44    fn from(value: T) -> Self {
45        Error::Io(value)
46    }
47}
48
49impl<T> From<ciborium_ll::Error<T>> for Error<T> {
50    #[inline]
51    fn from(value: ciborium_ll::Error<T>) -> Self {
52        match value {
53            ciborium_ll::Error::Io(x) => Self::Io(x),
54            ciborium_ll::Error::Syntax(x) => Self::Syntax(x),
55        }
56    }
57}
58
59impl<T: Debug> Display for Error<T> {
60    #[inline]
61    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
62        write!(f, "{:?}", self)
63    }
64}
65
66impl<T: Debug> StdError for Error<T> {}
67
68impl<T: Debug> DeError for Error<T> {
69    #[inline]
70    fn custom<U: Display>(msg: U) -> Self {
71        Self::Semantic(None, msg.to_string())
72    }
73}