use super::CoreError;
use core::fmt;
#[cfg(not(feature = "std"))]
extern crate alloc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ErrorCategory {
Parsing,
Analysis,
Plugin,
Format,
Encoding,
Io,
Resource,
Configuration,
Validation,
Compatibility,
Security,
Internal,
}
impl fmt::Display for ErrorCategory {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Parsing => write!(f, "parsing"),
Self::Analysis => write!(f, "analysis"),
Self::Plugin => write!(f, "plugin"),
Self::Format => write!(f, "format"),
Self::Encoding => write!(f, "encoding"),
Self::Io => write!(f, "io"),
Self::Resource => write!(f, "resource"),
Self::Configuration => write!(f, "configuration"),
Self::Validation => write!(f, "validation"),
Self::Compatibility => write!(f, "compatibility"),
Self::Security => write!(f, "security"),
Self::Internal => write!(f, "internal"),
}
}
}
impl ErrorCategory {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Parsing => "Parsing",
Self::Analysis => "Analysis",
Self::Plugin => "Plugin",
Self::Format => "Format",
Self::Encoding => "Encoding",
Self::Io => "I/O",
Self::Resource => "Resource",
Self::Configuration => "Configuration",
Self::Validation => "Validation",
Self::Compatibility => "Compatibility",
Self::Security => "Security",
Self::Internal => "Internal",
}
}
#[must_use]
pub const fn is_user_fixable(self) -> bool {
match self {
Self::Parsing
| Self::Format
| Self::Encoding
| Self::Configuration
| Self::Validation
| Self::Analysis
| Self::Compatibility => true,
Self::Plugin | Self::Io | Self::Resource | Self::Security | Self::Internal => false,
}
}
#[must_use]
pub const fn severity_level(self) -> u8 {
match self {
Self::Internal | Self::Security => 5,
Self::Resource | Self::Io => 4,
Self::Plugin | Self::Compatibility => 3,
Self::Parsing | Self::Validation => 2,
Self::Analysis | Self::Configuration | Self::Format | Self::Encoding => 1,
}
}
}
impl CoreError {
#[must_use]
pub const fn category(&self) -> ErrorCategory {
match self {
Self::Parse(_) | Self::Tokenization(_) => ErrorCategory::Parsing,
Self::Analysis(_) => ErrorCategory::Analysis,
Self::Plugin(_) => ErrorCategory::Plugin,
Self::InvalidColor(_) | Self::InvalidNumeric(_) | Self::InvalidTime(_) => {
ErrorCategory::Format
}
Self::Utf8Error { .. } => ErrorCategory::Encoding,
Self::Io(_) => ErrorCategory::Io,
Self::OutOfMemory(_) | Self::ResourceLimitExceeded { .. } => ErrorCategory::Resource,
Self::Config(_) => ErrorCategory::Configuration,
Self::Validation(_) => ErrorCategory::Validation,
Self::FeatureNotSupported { .. } | Self::VersionIncompatible { .. } => {
ErrorCategory::Compatibility
}
Self::SecurityViolation(_) => ErrorCategory::Security,
Self::Internal(_) => ErrorCategory::Internal,
}
}
#[must_use]
pub const fn suggestion(&self) -> Option<&'static str> {
match self {
Self::InvalidColor(_) => Some("Use format like '&H00FF00FF&' for colors"),
Self::InvalidTime(_) => Some("Use format like '0:01:30.50' for times"),
Self::InvalidNumeric(_) => Some("Check numeric format and range"),
Self::FeatureNotSupported { .. } => Some("Enable required feature in Cargo.toml"),
Self::OutOfMemory(_) => Some("Reduce input size or enable 'arena' feature"),
Self::ResourceLimitExceeded { .. } => {
Some("Reduce input complexity or increase limits")
}
Self::SecurityViolation(_) => Some("Review script content for security issues"),
Self::Internal(_) => Some("Please report this bug to the maintainers"),
Self::Utf8Error { .. } => Some("Check file encoding - ASS files should be UTF-8"),
Self::Config(_) => Some("Review configuration settings and feature flags"),
_ => None,
}
}
}
#[cfg(test)]
mod tests {}