#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ErrorSeverity {
Fatal,
Recoverable,
Other,
}
pub trait MediaError {
fn severity(&self) -> ErrorSeverity;
fn is_recoverable(&self) -> bool {
matches!(self.severity(), ErrorSeverity::Recoverable)
}
fn is_fatal(&self) -> bool {
matches!(self.severity(), ErrorSeverity::Fatal)
}
}
impl MediaError for crate::FormatError {
fn severity(&self) -> ErrorSeverity {
ErrorSeverity::Other
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn severity_should_drive_is_recoverable_and_is_fatal() {
struct Fatal;
struct Recoverable;
struct Other;
impl MediaError for Fatal {
fn severity(&self) -> ErrorSeverity {
ErrorSeverity::Fatal
}
}
impl MediaError for Recoverable {
fn severity(&self) -> ErrorSeverity {
ErrorSeverity::Recoverable
}
}
impl MediaError for Other {
fn severity(&self) -> ErrorSeverity {
ErrorSeverity::Other
}
}
assert!(Fatal.is_fatal() && !Fatal.is_recoverable());
assert!(Recoverable.is_recoverable() && !Recoverable.is_fatal());
assert!(!Other.is_fatal() && !Other.is_recoverable());
}
#[test]
fn format_error_severity_should_be_other() {
let err = crate::FormatError::invalid_pixel_format("x");
assert_eq!(err.severity(), ErrorSeverity::Other);
}
}