use std::process::ExitCode;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommandExitCode {
Success = 0,
GeneralError = 1,
InvalidArguments = 2,
ConfigError = 3,
GitError = 10,
RegistryError = 11,
VersionError = 12,
CheckFailed = 13,
SecurityError = 14,
NetworkError = 15,
PrePublishChecksFailed = 16,
PartialSuccess = 20,
RollbackFailed = 21,
CheckpointCreated = 30,
NoChanges = 31,
DriftDetected = 32,
}
impl From<CommandExitCode> for ExitCode {
fn from(code: CommandExitCode) -> Self {
Self::from(code as u8)
}
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("CLI error: {0}")]
Cli(String),
#[error("Config error: {0}")]
Config(String),
#[error("Invalid arguments: {0}")]
InvalidArguments(String),
#[error("Git error: {0}")]
Git(String),
#[error("Registry error: {0}")]
Registry(String),
#[error("Core error: {0}")]
Core(#[from] governor_core::Error),
#[error("IO error: {0}")]
StdIo(#[from] std::io::Error),
#[error("Serialization error: {0}")]
Serialization(String),
}
impl From<String> for Error {
fn from(s: String) -> Self {
Self::Cli(s)
}
}
impl From<governor_application::ApplicationError> for Error {
fn from(error: governor_application::ApplicationError) -> Self {
match error {
governor_application::ApplicationError::InvalidArguments(message) => {
Self::InvalidArguments(message)
}
governor_application::ApplicationError::Workspace(message) => Self::Config(message),
governor_application::ApplicationError::SourceControl(error) => {
Self::Git(error.to_string())
}
governor_application::ApplicationError::Registry(error) => {
Self::Registry(error.to_string())
}
governor_application::ApplicationError::Checkpoint(error) => {
Self::Config(error.to_string())
}
governor_application::ApplicationError::Io(error) => Self::StdIo(error),
}
}
}
impl Error {
#[must_use]
pub const fn exit_code(&self) -> CommandExitCode {
match self {
Self::Cli(_) | Self::InvalidArguments(_) => CommandExitCode::InvalidArguments,
Self::Config(_) => CommandExitCode::ConfigError,
Self::Git(_) => CommandExitCode::GitError,
Self::Registry(_) => CommandExitCode::RegistryError,
Self::Core(_) | Self::StdIo(_) | Self::Serialization(_) => {
CommandExitCode::GeneralError
}
}
}
}