use crate::error::IndexError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ExitCode {
Success = 0,
GeneralError = 1,
BlockingError = 2,
NotFound = 3,
ParseError = 4,
IoError = 5,
ConfigError = 6,
IndexCorrupted = 7,
UnsupportedOperation = 8,
}
impl From<ExitCode> for i32 {
fn from(code: ExitCode) -> i32 {
code as i32
}
}
impl ExitCode {
pub fn from_retrieve_result<T>(result: &Option<T>) -> Self {
match result {
Some(_) => ExitCode::Success,
None => ExitCode::NotFound,
}
}
pub fn from_error(error: &IndexError) -> Self {
match error {
IndexError::SymbolNotFound { .. } | IndexError::FileNotFound { .. } => {
ExitCode::NotFound
}
IndexError::IndexCorrupted { .. } => ExitCode::BlockingError,
IndexError::ParseError { .. } => ExitCode::ParseError,
IndexError::FileRead { .. } | IndexError::FileWrite { .. } => ExitCode::IoError,
IndexError::ConfigError { .. } => ExitCode::ConfigError,
IndexError::UnsupportedFileType { .. } => ExitCode::UnsupportedOperation,
IndexError::FileIdExhausted | IndexError::SymbolIdExhausted => ExitCode::BlockingError,
_ => ExitCode::GeneralError,
}
}
#[must_use]
pub fn is_blocking(&self) -> bool {
matches!(self, ExitCode::BlockingError)
}
#[must_use]
pub fn is_success(&self) -> bool {
matches!(self, ExitCode::Success)
}
pub fn description(&self) -> &str {
match self {
ExitCode::Success => "Success",
ExitCode::GeneralError => "General error",
ExitCode::BlockingError => "Blocking error - automation should halt",
ExitCode::NotFound => "Not found",
ExitCode::ParseError => "Parse error",
ExitCode::IoError => "I/O error",
ExitCode::ConfigError => "Configuration error",
ExitCode::IndexCorrupted => "Index corrupted",
ExitCode::UnsupportedOperation => "Unsupported operation",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exit_code_values() {
assert_eq!(ExitCode::Success as u8, 0);
assert_eq!(ExitCode::GeneralError as u8, 1);
assert_eq!(ExitCode::BlockingError as u8, 2);
assert_eq!(ExitCode::NotFound as u8, 3);
}
#[test]
fn test_from_retrieve_result() {
let some_result = Some("data");
assert_eq!(
ExitCode::from_retrieve_result(&some_result),
ExitCode::Success
);
let none_result: Option<&str> = None;
assert_eq!(
ExitCode::from_retrieve_result(&none_result),
ExitCode::NotFound
);
}
#[test]
fn test_is_success() {
assert!(ExitCode::Success.is_success());
assert!(!ExitCode::NotFound.is_success());
assert!(!ExitCode::GeneralError.is_success());
}
#[test]
fn test_is_blocking() {
assert!(ExitCode::BlockingError.is_blocking());
assert!(!ExitCode::Success.is_blocking());
assert!(!ExitCode::NotFound.is_blocking());
}
}