use sublime_cli_tools::error::CliError;
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ErrorCode {
Config,
Validation,
Execution,
Git,
Package,
NotFound,
Io,
Network,
User,
Timeout,
}
impl ErrorCode {
#[must_use]
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::Config => "ECONFIG",
Self::Validation => "EVALIDATION",
Self::Execution => "EEXEC",
Self::Git => "EGIT",
Self::Package => "EPKG",
Self::NotFound => "ENOENT",
Self::Io => "EIO",
Self::Network => "ENETWORK",
Self::User => "EUSER",
Self::Timeout => "ETIMEOUT",
}
}
}
impl std::fmt::Display for ErrorCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub(crate) struct ErrorInfo {
pub(crate) code: String,
pub(crate) message: String,
pub(crate) context: Option<String>,
pub(crate) kind: String,
}
#[allow(dead_code)]
impl ErrorInfo {
#[must_use]
pub(crate) fn new(
code: impl Into<String>,
message: impl Into<String>,
context: Option<impl Into<String>>,
kind: impl Into<String>,
) -> Self {
Self {
code: code.into(),
message: message.into(),
context: context.map(Into::into),
kind: kind.into(),
}
}
#[must_use]
pub(crate) fn validation(message: impl Into<String>, field: Option<impl Into<String>>) -> Self {
Self::new(ErrorCode::Validation.as_str(), message, field, "Validation")
}
#[must_use]
pub(crate) fn configuration(message: impl Into<String>) -> Self {
Self::new(ErrorCode::Config.as_str(), message, None::<String>, "Configuration")
}
#[must_use]
pub(crate) fn not_found(message: impl Into<String>, path: Option<impl Into<String>>) -> Self {
Self::new(ErrorCode::NotFound.as_str(), message, path, "Io")
}
#[must_use]
pub(crate) fn git(message: impl Into<String>) -> Self {
Self::new(ErrorCode::Git.as_str(), message, None::<String>, "Git")
}
#[must_use]
pub(crate) fn execution(message: impl Into<String>) -> Self {
Self::new(ErrorCode::Execution.as_str(), message, None::<String>, "Execution")
}
#[must_use]
pub(crate) fn package(message: impl Into<String>) -> Self {
Self::new(ErrorCode::Package.as_str(), message, None::<String>, "Package")
}
#[must_use]
pub(crate) fn io(message: impl Into<String>, path: Option<impl Into<String>>) -> Self {
Self::new(ErrorCode::Io.as_str(), message, path, "Io")
}
#[must_use]
pub(crate) fn network(message: impl Into<String>) -> Self {
Self::new(ErrorCode::Network.as_str(), message, None::<String>, "Network")
}
#[must_use]
pub(crate) fn user(message: impl Into<String>) -> Self {
Self::new(ErrorCode::User.as_str(), message, None::<String>, "User")
}
#[must_use]
pub(crate) fn timeout(message: impl Into<String>) -> Self {
Self::new(ErrorCode::Timeout.as_str(), message, None::<String>, "Timeout")
}
}
impl From<&CliError> for ErrorInfo {
fn from(error: &CliError) -> Self {
let code = match error {
CliError::Configuration(_) => ErrorCode::Config,
CliError::Validation(_) => ErrorCode::Validation,
CliError::Execution(_) => ErrorCode::Execution,
CliError::Git(_) => ErrorCode::Git,
CliError::Package(_) => ErrorCode::Package,
CliError::Io(_) => ErrorCode::Io,
CliError::Network(_) => ErrorCode::Network,
CliError::User(_) => ErrorCode::User,
};
Self::new(code.as_str(), error.to_string(), None::<String>, error.kind())
}
}
impl From<CliError> for ErrorInfo {
fn from(error: CliError) -> Self {
Self::from(&error)
}
}