1use std::fmt::{self, Debug, Display};
2
3/// Errors that can occur when serializing or deserializing TOML.
4pub struct Error(Box<ErrorInner>);
5
6pub(crate) enum ErrorInner {
7 Ser(crate::ser::Error),
8 De(crate::de::Error),
9}
10
11impl Error {
12 /// Produces a (line, column) pair of the position of the error if
13 /// available.
14 ///
15 /// All indexes are 0-based.
16 pub fn line_col(&self) -> Option<(usize, usize)> {
17 match &*self.0 {
18 ErrorInner::Ser(_) => None,
19 ErrorInner::De(error: &Error) => error.line_col(),
20 }
21 }
22}
23
24impl From<crate::ser::Error> for Error {
25 fn from(error: crate::ser::Error) -> Self {
26 Error(Box::new(ErrorInner::Ser(error)))
27 }
28}
29
30impl From<crate::de::Error> for Error {
31 fn from(error: crate::de::Error) -> Self {
32 Error(Box::new(ErrorInner::De(error)))
33 }
34}
35
36impl Display for Error {
37 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
38 match &*self.0 {
39 ErrorInner::Ser(error: &Error) => Display::fmt(self:error, f:formatter),
40 ErrorInner::De(error: &Error) => Display::fmt(self:error, f:formatter),
41 }
42 }
43}
44
45impl Debug for Error {
46 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
47 match &*self.0 {
48 ErrorInner::Ser(error: &Error) => Debug::fmt(self:error, f:formatter),
49 ErrorInner::De(error: &Error) => Debug::fmt(self:error, f:formatter),
50 }
51 }
52}
53
54impl std::error::Error for Error {}
55