hadris-udf 2.0.0-rc.2

Pure Rust UDF filesystem library for optical media and disk images with no-std support
Documentation
//! UDF-specific error types

use hadris_io as io;

/// Errors that can occur when reading or writing UDF filesystems
#[derive(Debug)]
pub enum UdfError {
    /// I/O error
    Io(io::Error),
    /// Invalid or missing Volume Recognition Sequence
    InvalidVrs,
    /// Invalid or missing Volume Descriptor Sequence
    InvalidVds(&'static str),
    /// Invalid or missing File Set Descriptor
    InvalidFsd,
    /// No Anchor Volume Descriptor Pointer found
    NoAnchor,
    /// Invalid descriptor tag
    InvalidTag {
        /// Descriptor tag identifier required at this location.
        expected: u16,
        /// Descriptor tag identifier found on the medium.
        found: u16,
    },
    /// Descriptor CRC mismatch
    CrcMismatch {
        /// CRC stored in the descriptor tag.
        expected: u16,
        /// CRC computed from the descriptor payload.
        computed: u16,
    },
    /// Invalid partition reference
    InvalidPartition(u16),
    /// Invalid ICB (Information Control Block)
    InvalidIcb,
    /// File not found
    NotFound,
    /// Not a directory
    NotADirectory,
    /// Not a file
    NotAFile,
    /// Path too long
    PathTooLong,
    /// Invalid filename encoding
    InvalidEncoding,
    /// byte casting failed - the data buffer size doesn't match the target struct size.
    PodCastError(bytemuck::PodCastError),
}

impl<E: io::IoError> From<io::Error<E>> for UdfError {
    fn from(err: io::Error<E>) -> Self {
        Self::Io(err.erase())
    }
}

impl core::fmt::Display for UdfError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Io(e) => write!(f, "I/O error: {e}"),
            Self::InvalidVrs => write!(f, "invalid or missing Volume Recognition Sequence"),
            Self::InvalidVds(reason) => {
                write!(f, "invalid or missing Volume Descriptor Sequence. {reason}")
            }
            Self::InvalidFsd => write!(f, "invalid or missing File Set Descriptor."),
            Self::NoAnchor => write!(f, "no Anchor Volume Descriptor Pointer found"),
            Self::InvalidTag { expected, found } => {
                write!(
                    f,
                    "invalid descriptor tag: expected {expected}, found {found}"
                )
            }
            Self::CrcMismatch { expected, computed } => {
                write!(
                    f,
                    "CRC mismatch: expected {expected:04x}, computed {computed:04x}"
                )
            }
            Self::InvalidPartition(num) => write!(f, "invalid partition reference: {num}"),
            Self::InvalidIcb => write!(f, "invalid Information Control Block"),
            Self::NotFound => write!(f, "file or directory not found"),
            Self::NotADirectory => write!(f, "not a directory"),
            Self::NotAFile => write!(f, "not a file"),
            Self::PathTooLong => write!(f, "path too long"),
            Self::InvalidEncoding => write!(f, "invalid filename encoding"),
            Self::PodCastError(err) => write!(
                f,
                "byte casting failed - the data buffer size doesn't match the target struct size. {err}"
            ),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for UdfError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(e) => Some(e),
            _ => None,
        }
    }
}

/// Result type for UDF operations
pub type UdfResult<T> = Result<T, UdfError>;