use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorCode {
ZipInvalid,
ZipEncrypted,
PathInvalid,
PathEmpty,
PathLeadingSlash,
PathTraversal,
PathReservedChar,
PathControlChar,
DuplicatePath,
PathConflict,
ManifestInvalid,
EntryPointInvalid,
EntryPointMissing,
EntryPointUnresolved,
VersionUnsupported,
ModeUnsupported,
LimitExceeded,
EntryPointChanged,
NotFound,
TextInvalid,
Io,
}
impl ErrorCode {
pub const fn as_str(self) -> &'static str {
match self {
ErrorCode::ZipInvalid => "ERR_ZIP_INVALID",
ErrorCode::ZipEncrypted => "ERR_ZIP_ENCRYPTED",
ErrorCode::PathInvalid => "ERR_PATH_INVALID",
ErrorCode::PathEmpty => "ERR_PATH_EMPTY",
ErrorCode::PathLeadingSlash => "ERR_PATH_LEADING_SLASH",
ErrorCode::PathTraversal => "ERR_PATH_TRAVERSAL",
ErrorCode::PathReservedChar => "ERR_PATH_RESERVED_CHAR",
ErrorCode::PathControlChar => "ERR_PATH_CONTROL_CHAR",
ErrorCode::DuplicatePath => "ERR_DUPLICATE_PATH",
ErrorCode::PathConflict => "ERR_PATH_CONFLICT",
ErrorCode::ManifestInvalid => "ERR_MANIFEST_INVALID",
ErrorCode::EntryPointInvalid => "ERR_ENTRYPOINT_INVALID",
ErrorCode::EntryPointMissing => "ERR_ENTRYPOINT_MISSING",
ErrorCode::EntryPointUnresolved => "ERR_ENTRYPOINT_UNRESOLVED",
ErrorCode::VersionUnsupported => "ERR_VERSION_UNSUPPORTED",
ErrorCode::ModeUnsupported => "ERR_MODE_UNSUPPORTED",
ErrorCode::LimitExceeded => "ERR_LIMIT_EXCEEDED",
ErrorCode::EntryPointChanged => "ERR_ENTRYPOINT_CHANGED",
ErrorCode::NotFound => "ERR_NOT_FOUND",
ErrorCode::TextInvalid => "ERR_TEXT_INVALID",
ErrorCode::Io => "ERR_IO",
}
}
}
impl fmt::Display for ErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MdzError {
code: ErrorCode,
message: String,
}
impl MdzError {
pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
MdzError {
code,
message: message.into(),
}
}
pub fn code(&self) -> ErrorCode {
self.code
}
pub fn message(&self) -> &str {
&self.message
}
}
impl fmt::Display for MdzError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.code, self.message)
}
}
impl std::error::Error for MdzError {}
pub type Result<T, E = MdzError> = std::result::Result<T, E>;
pub(crate) fn err<T>(code: ErrorCode, message: impl Into<String>) -> Result<T> {
Err(MdzError::new(code, message))
}