migra/migra-cli/src/error.rs

44 lines
998 B
Rust
Raw Normal View History

2021-02-22 23:06:08 +03:00
use std::error;
2021-02-10 01:13:27 +03:00
use std::fmt;
2021-02-22 23:06:08 +03:00
use std::io;
2021-02-10 01:13:27 +03:00
use std::mem;
use std::result;
pub type StdResult<T> = result::Result<T, Box<dyn std::error::Error>>;
2021-02-22 23:06:08 +03:00
pub type MigraResult<T> = result::Result<T, Error>;
2021-02-10 01:13:27 +03:00
#[derive(Debug)]
2021-02-22 23:06:08 +03:00
pub enum Error {
RootNotFound,
2021-02-10 01:13:27 +03:00
MissedEnvVar(String),
2021-02-22 23:06:08 +03:00
2021-02-25 12:25:14 +03:00
Io(io::Error),
2021-02-10 01:13:27 +03:00
}
2021-02-22 23:06:08 +03:00
impl fmt::Display for Error {
2021-02-10 01:13:27 +03:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
2021-02-22 23:06:08 +03:00
Error::RootNotFound => fmt.write_str("Cannot find root directory"),
Error::MissedEnvVar(ref name) => {
2021-02-10 01:13:27 +03:00
write!(fmt, r#"Missed "{}" environment variable"#, name)
}
2021-02-25 12:25:14 +03:00
Error::Io(ref error) => write!(fmt, "{}", error),
2021-02-10 01:13:27 +03:00
}
}
}
2021-02-22 23:06:08 +03:00
impl error::Error for Error {}
impl PartialEq for Error {
2021-02-10 01:13:27 +03:00
fn eq(&self, other: &Self) -> bool {
mem::discriminant(self) == mem::discriminant(other)
}
}
2021-02-22 23:06:08 +03:00
impl From<io::Error> for Error {
2021-02-10 01:13:27 +03:00
#[inline]
2021-02-22 23:06:08 +03:00
fn from(err: io::Error) -> Error {
2021-02-25 12:25:14 +03:00
Error::Io(err)
2021-02-10 01:13:27 +03:00
}
}