use hadris_io as io;
#[derive(Debug)]
pub enum UdfError {
Io(io::Error),
InvalidVrs,
InvalidVds(&'static str),
InvalidFsd,
NoAnchor,
InvalidTag {
expected: u16,
found: u16,
},
CrcMismatch {
expected: u16,
computed: u16,
},
InvalidPartition(u16),
InvalidIcb,
NotFound,
NotADirectory,
NotAFile,
PathTooLong,
InvalidEncoding,
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,
}
}
}
pub type UdfResult<T> = Result<T, UdfError>;