use std::fmt;
pub const OK: i32 = 0;
pub const ERROR: i32 = 1;
pub const TIMEOUT: i32 = 2;
pub const AUTH: i32 = 3;
pub const NOT_FOUND: i32 = 4;
#[derive(Debug)]
pub struct Exit {
pub code: i32,
pub message: String,
}
impl fmt::Display for Exit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for Exit {}
pub fn auth(message: impl Into<String>) -> anyhow::Error {
Exit {
code: AUTH,
message: message.into(),
}
.into()
}
pub fn not_found(message: impl Into<String>) -> anyhow::Error {
Exit {
code: NOT_FOUND,
message: message.into(),
}
.into()
}
pub fn timeout(message: impl Into<String>) -> anyhow::Error {
Exit {
code: TIMEOUT,
message: message.into(),
}
.into()
}
pub fn code_of(err: &anyhow::Error) -> i32 {
err.downcast_ref::<Exit>().map(|e| e.code).unwrap_or(ERROR)
}
pub fn is_not_found(err: &anyhow::Error) -> bool {
code_of(err) == NOT_FOUND
}