use std::fmt;
use std::os::raw::c_int;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AvError(c_int);
impl AvError {
#[must_use]
pub const fn new(code: c_int) -> Self {
Self(code)
}
#[must_use]
pub const fn code(self) -> c_int {
self.0
}
#[must_use]
pub const fn is_eagain(self) -> bool {
self.0 == crate::error_codes::EAGAIN
}
#[must_use]
pub const fn is_eof(self) -> bool {
self.0 == crate::error_codes::EOF
}
}
impl fmt::Display for AvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} (code={})", crate::av_error_string(self.0), self.0)
}
}
impl std::error::Error for AvError {}
impl From<c_int> for AvError {
fn from(code: c_int) -> Self {
Self(code)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn av_error_should_preserve_the_raw_code() {
assert_eq!(AvError::new(-22).code(), -22);
}
#[test]
fn av_error_should_detect_eagain() {
assert!(AvError::new(crate::error_codes::EAGAIN).is_eagain());
assert!(!AvError::new(crate::error_codes::EOF).is_eagain());
}
#[test]
fn av_error_should_detect_eof() {
assert!(AvError::new(crate::error_codes::EOF).is_eof());
assert!(!AvError::new(crate::error_codes::EAGAIN).is_eof());
}
#[test]
fn av_error_display_should_include_the_message_and_code() {
let rendered = AvError::new(crate::error_codes::EOF).to_string();
assert!(
rendered.contains("code="),
"display should include the raw code: {rendered}"
);
assert!(!rendered.is_empty());
}
#[test]
fn av_error_should_convert_from_a_raw_code() {
let err = AvError::from(-22);
assert_eq!(err.code(), -22);
}
}