#[derive(Debug, Clone)]
pub struct CliError {
exit_code: i32,
message: String,
}
impl CliError {
pub fn runtime(message: impl Into<String>) -> Self {
Self {
exit_code: 1,
message: message.into(),
}
}
pub fn io(message: impl Into<String>) -> Self {
Self {
exit_code: 2,
message: message.into(),
}
}
pub fn usage(message: impl Into<String>) -> Self {
Self {
exit_code: 3,
message: message.into(),
}
}
pub fn internal(message: impl Into<String>) -> Self {
Self {
exit_code: 4,
message: message.into(),
}
}
pub fn exit_code(&self) -> i32 {
self.exit_code
}
pub fn message(&self) -> &str {
&self.message
}
}
impl std::fmt::Display for CliError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for CliError {}
impl From<std::io::Error> for CliError {
fn from(err: std::io::Error) -> Self {
CliError::io(format!("io error: {err}"))
}
}
impl From<serde_json::Error> for CliError {
fn from(err: serde_json::Error) -> Self {
CliError::runtime(format!("failed to serialize output: {err}"))
}
}