Skip to main content

toml_edit/de/
error.rs

1/// Errors that can occur when deserializing a type.
2#[derive(Clone, PartialEq, Eq, Hash)]
3pub struct Error {
4    inner: crate::TomlError,
5}
6
7impl Error {
8    pub(crate) fn custom<T>(msg: T, span: Option<std::ops::Range<usize>>) -> Self
9    where
10        T: std::fmt::Display,
11    {
12        Self {
13            inner: crate::TomlError::custom(msg.to_string(), span),
14        }
15    }
16
17    /// Add key while unwinding
18    pub fn add_key(&mut self, key: String) {
19        self.inner.add_key(key);
20    }
21
22    /// What went wrong
23    pub fn message(&self) -> &str {
24        self.inner.message()
25    }
26
27    /// The start/end index into the original document where the error occurred
28    pub fn span(&self) -> Option<std::ops::Range<usize>> {
29        self.inner.span()
30    }
31
32    pub(crate) fn set_span(&mut self, span: Option<std::ops::Range<usize>>) {
33        self.inner.set_span(span);
34    }
35
36    /// Provide the encoded TOML the error applies to
37    pub fn set_input(&mut self, input: Option<&str>) {
38        self.inner.set_input(input);
39    }
40}
41
42impl serde_core::de::Error for Error {
43    fn custom<T>(msg: T) -> Self
44    where
45        T: std::fmt::Display,
46    {
47        Self::custom(msg, None)
48    }
49}
50
51impl std::fmt::Display for Error {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        self.inner.fmt(f)
54    }
55}
56
57impl std::fmt::Debug for Error {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        self.inner.fmt(f)
60    }
61}
62
63impl From<crate::TomlError> for Error {
64    fn from(e: crate::TomlError) -> Self {
65        Self { inner: e }
66    }
67}
68
69impl From<Error> for crate::TomlError {
70    fn from(e: Error) -> Self {
71        e.inner
72    }
73}
74
75impl std::error::Error for Error {}