use std::error::Error;
use std::fmt;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct AppError {
message: String,
exit_code: i32,
}
impl AppError {
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
exit_code: 1,
}
}
pub fn invalid_input(message: impl Into<String>) -> Self {
Self {
message: message.into(),
exit_code: 2,
}
}
pub fn message(&self) -> &str {
&self.message
}
pub fn exit_code(&self) -> i32 {
self.exit_code
}
}
impl fmt::Display for AppError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.message)
}
}
impl Error for AppError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn invalid_input_uses_cli_error_exit_code() {
let error = AppError::invalid_input("bad value");
assert_eq!(error.message(), "bad value");
assert_eq!(error.exit_code(), 2);
assert_eq!(error.to_string(), "bad value");
}
}