summaryrefslogtreecommitdiff
path: root/dhall/src/error.rs
blob: b98716538dcb828a6fe838ca1e1db07b29114511 (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
pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    IO(std::io::Error),
    Parse(dhall_core::ParseError),
    Decode(crate::binary::DecodeError),
    Resolve(crate::imports::ImportError),
    Typecheck(crate::typecheck::TypeError),
    Deserialize(String),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Error::IO(err) => write!(f, "{}", err),
            Error::Parse(err) => write!(f, "{}", err),
            Error::Decode(err) => write!(f, "{:?}", err),
            Error::Resolve(err) => write!(f, "{:?}", err),
            Error::Typecheck(err) => write!(f, "{:?}", err),
            Error::Deserialize(err) => write!(f, "{}", err),
        }
    }
}

impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Error {
        Error::IO(err)
    }
}
impl From<dhall_core::ParseError> for Error {
    fn from(err: dhall_core::ParseError) -> Error {
        Error::Parse(err)
    }
}
impl From<crate::binary::DecodeError> for Error {
    fn from(err: crate::binary::DecodeError) -> Error {
        Error::Decode(err)
    }
}
impl From<crate::imports::ImportError> for Error {
    fn from(err: crate::imports::ImportError) -> Error {
        Error::Resolve(err)
    }
}
impl From<crate::typecheck::TypeError> for Error {
    fn from(err: crate::typecheck::TypeError) -> Error {
        Error::Typecheck(err)
    }
}