toml/
edit.rs

1#[cfg(feature = "parse")]
2pub(crate) mod de {
3    pub(crate) use toml_edit::de::Error;
4}
5
6#[cfg(not(feature = "parse"))]
7pub(crate) mod de {
8    /// Errors that can occur when deserializing a type.
9    #[derive(Debug, Clone, PartialEq, Eq)]
10    pub struct Error {
11        inner: String,
12    }
13
14    impl Error {
15        /// Add key while unwinding
16        pub fn add_key(&mut self, _key: String) {}
17
18        /// What went wrong
19        pub fn message(&self) -> &str {
20            self.inner.as_str()
21        }
22    }
23
24    impl serde::de::Error for Error {
25        fn custom<T>(msg: T) -> Self
26        where
27            T: std::fmt::Display,
28        {
29            Error {
30                inner: msg.to_string(),
31            }
32        }
33    }
34
35    impl std::fmt::Display for Error {
36        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37            self.inner.fmt(f)
38        }
39    }
40
41    impl std::error::Error for Error {}
42}
43
44#[cfg(feature = "display")]
45pub(crate) mod ser {
46    pub(crate) use toml_edit::ser::Error;
47}
48
49#[cfg(not(feature = "display"))]
50pub(crate) mod ser {
51    #[derive(Debug, Clone, PartialEq, Eq)]
52    #[non_exhaustive]
53    pub(crate) enum Error {
54        UnsupportedType(Option<&'static str>),
55        UnsupportedNone,
56        KeyNotString,
57        Custom(String),
58    }
59
60    impl Error {
61        pub(crate) fn custom<T>(msg: T) -> Self
62        where
63            T: std::fmt::Display,
64        {
65            Error::Custom(msg.to_string())
66        }
67    }
68
69    impl serde::ser::Error for Error {
70        fn custom<T>(msg: T) -> Self
71        where
72            T: std::fmt::Display,
73        {
74            Self::custom(msg)
75        }
76    }
77
78    impl std::fmt::Display for Error {
79        fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80            match self {
81                Self::UnsupportedType(Some(t)) => write!(formatter, "unsupported {t} type"),
82                Self::UnsupportedType(None) => write!(formatter, "unsupported rust type"),
83                Self::UnsupportedNone => "unsupported None value".fmt(formatter),
84                Self::KeyNotString => "map key was not a string".fmt(formatter),
85                Self::Custom(s) => s.fmt(formatter),
86            }
87        }
88    }
89
90    impl std::error::Error for Error {}
91}