summaryrefslogtreecommitdiffstats
path: root/third_party/rust/basic-toml/src/error.rs
blob: 9abfd53e851034ab7c3f5613c42bcc4766547ebc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::fmt::{self, Debug, Display};

/// Errors that can occur when serializing or deserializing TOML.
pub struct Error(Box<ErrorInner>);

pub(crate) enum ErrorInner {
    Ser(crate::ser::Error),
    De(crate::de::Error),
}

impl Error {
    /// Produces a (line, column) pair of the position of the error if
    /// available.
    ///
    /// All indexes are 0-based.
    pub fn line_col(&self) -> Option<(usize, usize)> {
        match &*self.0 {
            ErrorInner::Ser(_) => None,
            ErrorInner::De(error) => error.line_col(),
        }
    }
}

impl From<crate::ser::Error> for Error {
    fn from(error: crate::ser::Error) -> Self {
        Error(Box::new(ErrorInner::Ser(error)))
    }
}

impl From<crate::de::Error> for Error {
    fn from(error: crate::de::Error) -> Self {
        Error(Box::new(ErrorInner::De(error)))
    }
}

impl Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        match &*self.0 {
            ErrorInner::Ser(error) => Display::fmt(error, formatter),
            ErrorInner::De(error) => Display::fmt(error, formatter),
        }
    }
}

impl Debug for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        match &*self.0 {
            ErrorInner::Ser(error) => Debug::fmt(error, formatter),
            ErrorInner::De(error) => Debug::fmt(error, formatter),
        }
    }
}

impl std::error::Error for Error {}