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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::Value;
use std::fmt::{Display, Formatter, Result as FmtResult};

/// A convenient alias for `Result`.
pub type Result<T> = std::result::Result<T, Error>;

/// An error while deserializing.
#[derive(Debug)]
pub enum Error {
    /// A custom error, from the serde `custom` methods.
    Custom(String),

    /// An IO error from `from_reader`/`to_writer`.
    Io(std::io::Error),

    /// An invalid s-expression was found when trying to deserialize the given Serde type.
    Invalid(&'static str, Value),

    /// A string failed to parse as an s-expression.
    ParseFailed,

    /// An s-expression was successfully parsed, but there was trailing input.
    ParseTrailing,

    /// An error converting bytes to UTF-8.
    Utf8(std::str::Utf8Error),
}

impl Display for Error {
    fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
        match self {
            Error::Custom(s) => fmt.write_str(&s),
            Error::Io(err) => err.fmt(fmt),
            Error::Invalid(ty, val) => write!(fmt, "{} is not a {}", val, ty),
            Error::ParseFailed => fmt.write_str("parsing s-expression failed"),
            Error::ParseTrailing => fmt.write_str("parsing s-expression failed (trailing input)"),
            Error::Utf8(err) => err.fmt(fmt),
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Error {
        Error::Io(err)
    }
}

impl From<std::str::Utf8Error> for Error {
    fn from(err: std::str::Utf8Error) -> Error {
        Error::Utf8(err)
    }
}

impl serde::de::Error for Error {
    fn custom<T: Display>(msg: T) -> Error {
        Error::Custom(msg.to_string())
    }
}

impl serde::ser::Error for Error {
    fn custom<T: Display>(msg: T) -> Error {
        Error::Custom(msg.to_string())
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Io(err) => Some(err),
            Error::Utf8(err) => Some(err),
            _ => None,
        }
    }
}