use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u8)]
pub enum ErrorCategory {
SyntaxQuoteMismatch = 0,
SyntaxBracketMismatch = 1,
SyntaxUnexpectedToken = 2,
SyntaxMissingOperand = 3,
CommandNotFound = 10,
CommandPermissionDenied = 11,
CommandInvalidOption = 12,
CommandMissingArgument = 13,
FileNotFound = 20,
FilePermissionDenied = 21,
FileIsDirectory = 22,
FileNotDirectory = 23,
FileTooManyOpen = 24,
VariableUnbound = 30,
VariableReadonly = 31,
VariableBadSubstitution = 32,
ProcessSignaled = 40,
ProcessExitNonZero = 41,
ProcessTimeout = 42,
PipeBroken = 50,
RedirectFailed = 51,
HereDocUnterminated = 52,
Unknown = 255,
}
impl ErrorCategory {
pub const COUNT: usize = 24;
#[must_use]
pub fn from_label(label: u8) -> Self {
match label {
0 => Self::SyntaxQuoteMismatch,
1 => Self::SyntaxBracketMismatch,
2 => Self::SyntaxUnexpectedToken,
3 => Self::SyntaxMissingOperand,
10 => Self::CommandNotFound,
11 => Self::CommandPermissionDenied,
12 => Self::CommandInvalidOption,
13 => Self::CommandMissingArgument,
20 => Self::FileNotFound,
21 => Self::FilePermissionDenied,
22 => Self::FileIsDirectory,
23 => Self::FileNotDirectory,
24 => Self::FileTooManyOpen,
30 => Self::VariableUnbound,
31 => Self::VariableReadonly,
32 => Self::VariableBadSubstitution,
40 => Self::ProcessSignaled,
41 => Self::ProcessExitNonZero,
42 => Self::ProcessTimeout,
50 => Self::PipeBroken,
51 => Self::RedirectFailed,
52 => Self::HereDocUnterminated,
_ => Self::Unknown,
}
}
#[must_use]
pub fn to_label_index(&self) -> usize {
match self {
Self::SyntaxQuoteMismatch => 0,
Self::SyntaxBracketMismatch => 1,
Self::SyntaxUnexpectedToken => 2,
Self::SyntaxMissingOperand => 3,
Self::CommandNotFound => 4,
Self::CommandPermissionDenied => 5,
Self::CommandInvalidOption => 6,
Self::CommandMissingArgument => 7,
Self::FileNotFound => 8,
Self::FilePermissionDenied => 9,
Self::FileIsDirectory => 10,
Self::FileNotDirectory => 11,
Self::FileTooManyOpen => 12,
Self::VariableUnbound => 13,
Self::VariableReadonly => 14,
Self::VariableBadSubstitution => 15,
Self::ProcessSignaled => 16,
Self::ProcessExitNonZero => 17,
Self::ProcessTimeout => 18,
Self::PipeBroken => 19,
Self::RedirectFailed => 20,
Self::HereDocUnterminated => 21,
Self::Unknown => 22,
}
}
#[must_use]
pub fn from_label_index(idx: usize) -> Self {
match idx {
0 => Self::SyntaxQuoteMismatch,
1 => Self::SyntaxBracketMismatch,
2 => Self::SyntaxUnexpectedToken,
3 => Self::SyntaxMissingOperand,
4 => Self::CommandNotFound,
5 => Self::CommandPermissionDenied,
6 => Self::CommandInvalidOption,
7 => Self::CommandMissingArgument,
8 => Self::FileNotFound,
9 => Self::FilePermissionDenied,
10 => Self::FileIsDirectory,
11 => Self::FileNotDirectory,
12 => Self::FileTooManyOpen,
13 => Self::VariableUnbound,
14 => Self::VariableReadonly,
15 => Self::VariableBadSubstitution,
16 => Self::ProcessSignaled,
17 => Self::ProcessExitNonZero,
18 => Self::ProcessTimeout,
19 => Self::PipeBroken,
20 => Self::RedirectFailed,
21 => Self::HereDocUnterminated,
_ => Self::Unknown,
}
}
#[must_use]
pub fn fix_suggestion(&self) -> &'static str {
match self {
Self::SyntaxQuoteMismatch => "Check for unmatched quotes (' or \")",
Self::SyntaxBracketMismatch => "Check for unmatched brackets ([], {}, ())",
Self::SyntaxUnexpectedToken => "Review syntax near the reported token",
Self::SyntaxMissingOperand => "Add missing operand to the expression",
Self::CommandNotFound => "Check PATH or install the missing command",
Self::CommandPermissionDenied => "Use chmod +x or run with sudo",
Self::CommandInvalidOption => "Check command documentation for valid options",
Self::CommandMissingArgument => "Provide required argument to the command",
Self::FileNotFound => "Verify the file path exists",
Self::FilePermissionDenied => "Check file permissions with ls -la",
Self::FileIsDirectory => "Use a file path, not a directory",
Self::FileNotDirectory => "Use a directory path, not a file",
Self::FileTooManyOpen => "Close unused file descriptors or increase ulimit",
Self::VariableUnbound => "Initialize variable or use ${VAR:-default}",
Self::VariableReadonly => "Cannot modify readonly variable",
Self::VariableBadSubstitution => "Fix parameter expansion syntax",
Self::ProcessSignaled => "Process was killed by signal",
Self::ProcessExitNonZero => "Check command exit status",
Self::ProcessTimeout => "Increase timeout or optimize command",
Self::PipeBroken => "Check if downstream process exited early",
Self::RedirectFailed => "Verify target path is writable",
Self::HereDocUnterminated => "Add terminating delimiter for here-doc",
Self::Unknown => "Review the error message for details",
}
}
#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::SyntaxQuoteMismatch => "Syntax: Quote Mismatch",
Self::SyntaxBracketMismatch => "Syntax: Bracket Mismatch",
Self::SyntaxUnexpectedToken => "Syntax: Unexpected Token",
Self::SyntaxMissingOperand => "Syntax: Missing Operand",
Self::CommandNotFound => "Command: Not Found",
Self::CommandPermissionDenied => "Command: Permission Denied",
Self::CommandInvalidOption => "Command: Invalid Option",
Self::CommandMissingArgument => "Command: Missing Argument",
Self::FileNotFound => "File: Not Found",
Self::FilePermissionDenied => "File: Permission Denied",
Self::FileIsDirectory => "File: Is Directory",
Self::FileNotDirectory => "File: Not Directory",
Self::FileTooManyOpen => "File: Too Many Open",
Self::VariableUnbound => "Variable: Unbound",
Self::VariableReadonly => "Variable: Readonly",
Self::VariableBadSubstitution => "Variable: Bad Substitution",
Self::ProcessSignaled => "Process: Signaled",
Self::ProcessExitNonZero => "Process: Non-Zero Exit",
Self::ProcessTimeout => "Process: Timeout",
Self::PipeBroken => "Pipe: Broken",
Self::RedirectFailed => "Redirect: Failed",
Self::HereDocUnterminated => "HereDoc: Unterminated",
Self::Unknown => "Unknown",
}
}
#[must_use]
pub fn all() -> &'static [ErrorCategory] {
&[
Self::SyntaxQuoteMismatch,
Self::SyntaxBracketMismatch,
Self::SyntaxUnexpectedToken,
Self::SyntaxMissingOperand,
Self::CommandNotFound,
Self::CommandPermissionDenied,
Self::CommandInvalidOption,
Self::CommandMissingArgument,
Self::FileNotFound,
Self::FilePermissionDenied,
Self::FileIsDirectory,
Self::FileNotDirectory,
Self::FileTooManyOpen,
Self::VariableUnbound,
Self::VariableReadonly,
Self::VariableBadSubstitution,
Self::ProcessSignaled,
Self::ProcessExitNonZero,
Self::ProcessTimeout,
Self::PipeBroken,
Self::RedirectFailed,
Self::HereDocUnterminated,
Self::Unknown,
]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_label_roundtrip() {
for cat in ErrorCategory::all() {
let idx = cat.to_label_index();
let restored = ErrorCategory::from_label_index(idx);
assert_eq!(*cat, restored, "Roundtrip failed for {cat:?}");
}
}
#[test]
fn test_all_categories_count() {
assert_eq!(ErrorCategory::all().len(), 23);
}
#[test]
fn test_fix_suggestions_not_empty() {
for cat in ErrorCategory::all() {
assert!(
!cat.fix_suggestion().is_empty(),
"Empty fix suggestion for {cat:?}"
);
}
}
#[test]
fn test_names_not_empty() {
for cat in ErrorCategory::all() {
assert!(!cat.name().is_empty(), "Empty name for {cat:?}");
}
}
#[test]
fn test_from_label_unknown() {
assert_eq!(ErrorCategory::from_label(200), ErrorCategory::Unknown);
assert_eq!(ErrorCategory::from_label(100), ErrorCategory::Unknown);
}
}