1use std::{
2 fmt::{self, Display, Formatter},
3 io,
4};
5
6pub type Result<T> = std::result::Result<T, Error>;
8
9#[derive(Debug)]
11pub enum Error {
12 Toml(String),
14 Io(io::Error),
16 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 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}