use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CliError {
CommandNotFound,
InvalidPath,
InvalidArgumentCount {
expected_min: usize,
expected_max: usize,
received: usize,
},
InvalidArgumentFormat {
arg_index: usize,
expected: heapless::String<32>,
},
BufferFull,
PathTooDeep,
#[cfg(feature = "authentication")]
AuthenticationFailed,
#[cfg(feature = "authentication")]
NotAuthenticated,
IoError,
#[cfg(feature = "async")]
AsyncInSyncContext,
Timeout,
CommandFailed(heapless::String<128>),
Other(heapless::String<128>),
}
impl fmt::Display for CliError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CliError::CommandNotFound => write!(f, "Command not found"),
CliError::InvalidPath => write!(f, "Invalid path"),
CliError::InvalidArgumentCount {
expected_min,
expected_max,
received,
} => {
if expected_min == expected_max {
write!(f, "Expected {} arguments, got {}", expected_min, received)
} else {
write!(
f,
"Expected {}-{} arguments, got {}",
expected_min, expected_max, received
)
}
}
CliError::InvalidArgumentFormat {
arg_index,
expected,
} => {
write!(f, "Argument {}: expected {}", arg_index + 1, expected)
}
CliError::BufferFull => write!(f, "Buffer full"),
CliError::PathTooDeep => write!(f, "Path too deep"),
#[cfg(feature = "authentication")]
CliError::AuthenticationFailed => write!(f, "Authentication failed"),
#[cfg(feature = "authentication")]
CliError::NotAuthenticated => write!(f, "Not authenticated"),
CliError::IoError => write!(f, "I/O error"),
#[cfg(feature = "async")]
CliError::AsyncInSyncContext => write!(f, "Async command requires async context"),
CliError::Timeout => write!(f, "Timeout"),
CliError::CommandFailed(msg) => write!(f, "{}", msg),
CliError::Other(msg) => write!(f, "{}", msg),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
extern crate std;
use std::format;
#[test]
fn test_error_display() {
assert_eq!(
format!("{}", CliError::CommandNotFound),
"Command not found"
);
assert_eq!(format!("{}", CliError::InvalidPath), "Invalid path");
let err = CliError::InvalidArgumentCount {
expected_min: 2,
expected_max: 2,
received: 1,
};
assert_eq!(format!("{}", err), "Expected 2 arguments, got 1");
let err = CliError::InvalidArgumentCount {
expected_min: 1,
expected_max: 3,
received: 4,
};
assert_eq!(format!("{}", err), "Expected 1-3 arguments, got 4");
let mut expected = heapless::String::new();
expected.push_str("integer").unwrap();
let err = CliError::InvalidArgumentFormat {
arg_index: 0,
expected,
};
assert_eq!(format!("{}", err), "Argument 1: expected integer");
let mut expected = heapless::String::new();
expected.push_str("IP address").unwrap();
let err = CliError::InvalidArgumentFormat {
arg_index: 2,
expected,
};
assert_eq!(format!("{}", err), "Argument 3: expected IP address");
}
}