cargo_deps/
error.rs

1use std::{
2    fmt::{self, Display, Formatter},
3    io,
4};
5
6/// Result type for the crate.
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// Error type for the crate.
10#[derive(Debug)]
11pub enum Error {
12    /// Errors originating from the toml crate.
13    Toml(String),
14    /// IO errors.
15    Io(io::Error),
16    /// All other errors.
17    Generic(String),
18}
19
20impl Display for Error {
21    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
22        match *self {
23            Self::Generic(ref e) => write!(f, "{}", e),
24            Self::Toml(ref e) => write!(f, "Could not parse toml file: {}", e),
25            Self::Io(ref e) => write!(f, "{}", e),
26        }
27    }
28}
29
30impl Error {
31    /// Print this error and immediately exit the program.
32    pub fn exit(&self) -> ! {
33        eprintln!("error: {}", self);
34        ::std::process::exit(1)
35    }
36}
37
38impl From<io::Error> for Error {
39    fn from(err: io::Error) -> Self {
40        Self::Io(err)
41    }
42}
43
44impl From<toml::de::Error> for Error {
45    fn from(err: toml::de::Error) -> Self {
46        Self::Toml(format!("Could not parse input as TOML: {}", err))
47    }
48}
49
50impl<T> From<std::io::IntoInnerError<T>> for Error {
51    fn from(err: std::io::IntoInnerError<T>) -> Self {
52        Self::Generic(format!("Error: {}", err))
53    }
54}