use std::fmt;
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ExitCodeKind {
Success = 0,
Generic = 1,
InvalidArgs = 2,
SdkError = 3,
IceSimulationBlocked = 4,
OperatorPolicyRejected = 5,
ConnectionFailure = 6,
Timeout = 7,
ConfirmationRefused = 8,
DaemonFactoryNotFound = 10,
DbQueryParseFailed = 11,
DbPredicateParseFailed = 12,
IceSignatureInvalid = 13,
TypegenBreakingChanges = 14,
}
#[derive(Debug)]
pub struct CliError {
kind: ExitCodeKind,
message: String,
}
impl CliError {
pub fn new(kind: ExitCodeKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
}
}
pub fn code(&self) -> u8 {
self.kind as u8
}
#[allow(dead_code)]
pub fn kind(&self) -> ExitCodeKind {
self.kind
}
}
impl fmt::Display for CliError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for CliError {}
#[allow(dead_code)]
pub fn generic(msg: impl Into<String>) -> CliError {
CliError::new(ExitCodeKind::Generic, msg)
}
#[allow(dead_code)]
pub fn invalid_args(msg: impl Into<String>) -> CliError {
CliError::new(ExitCodeKind::InvalidArgs, msg)
}
#[allow(dead_code)]
pub fn sdk(msg: impl Into<String>) -> CliError {
CliError::new(ExitCodeKind::SdkError, msg)
}
#[allow(dead_code)]
pub fn timeout(msg: impl Into<String>) -> CliError {
CliError::new(ExitCodeKind::Timeout, msg)
}
#[allow(dead_code)]
pub fn connection_failure(msg: impl Into<String>) -> CliError {
CliError::new(ExitCodeKind::ConnectionFailure, msg)
}
#[allow(dead_code)]
pub fn confirmation_refused() -> CliError {
CliError::new(
ExitCodeKind::ConfirmationRefused,
"operator declined the confirmation prompt",
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exit_code_table_matches_documented_values() {
assert_eq!(ExitCodeKind::Success as u8, 0);
assert_eq!(ExitCodeKind::Generic as u8, 1);
assert_eq!(ExitCodeKind::InvalidArgs as u8, 2);
assert_eq!(ExitCodeKind::SdkError as u8, 3);
assert_eq!(ExitCodeKind::IceSimulationBlocked as u8, 4);
assert_eq!(ExitCodeKind::OperatorPolicyRejected as u8, 5);
assert_eq!(ExitCodeKind::ConnectionFailure as u8, 6);
assert_eq!(ExitCodeKind::Timeout as u8, 7);
assert_eq!(ExitCodeKind::ConfirmationRefused as u8, 8);
assert_eq!(ExitCodeKind::DaemonFactoryNotFound as u8, 10);
assert_eq!(ExitCodeKind::DbQueryParseFailed as u8, 11);
assert_eq!(ExitCodeKind::DbPredicateParseFailed as u8, 12);
assert_eq!(ExitCodeKind::IceSignatureInvalid as u8, 13);
assert_eq!(ExitCodeKind::TypegenBreakingChanges as u8, 14);
}
}