Skip to main content

hadris_udf/
error.rs

1//! UDF-specific error types
2
3use hadris_io as io;
4
5/// Errors that can occur when reading or writing UDF filesystems
6#[derive(Debug)]
7pub enum Error {
8    /// I/O error
9    Io(io::Error),
10    /// Invalid or missing Volume Recognition Sequence
11    InvalidVrs,
12    /// Invalid or missing Volume Descriptor Sequence
13    InvalidVds(&'static str),
14    /// Invalid or missing File Set Descriptor
15    InvalidFsd,
16    /// No Anchor Volume Descriptor Pointer found
17    NoAnchor,
18    /// Invalid descriptor tag
19    InvalidTag {
20        /// Descriptor tag identifier required at this location.
21        expected: u16,
22        /// Descriptor tag identifier found on the medium.
23        found: u16,
24    },
25    /// Descriptor CRC mismatch
26    CrcMismatch {
27        /// CRC stored in the descriptor tag.
28        expected: u16,
29        /// CRC computed from the descriptor payload.
30        computed: u16,
31    },
32    /// Invalid partition reference
33    InvalidPartition(u16),
34    /// Invalid ICB (Information Control Block)
35    InvalidIcb,
36    /// File not found
37    NotFound,
38    /// Not a directory
39    NotADirectory,
40    /// Not a file
41    NotAFile,
42    /// Path too long
43    PathTooLong,
44    /// Invalid filename encoding
45    InvalidEncoding,
46    /// byte casting failed - the data buffer size doesn't match the target struct size.
47    PodCastError(bytemuck::PodCastError),
48}
49
50impl<E: io::IoError> From<io::Error<E>> for Error {
51    fn from(err: io::Error<E>) -> Self {
52        Self::Io(err.erase())
53    }
54}
55
56impl core::fmt::Display for Error {
57    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
58        match self {
59            Self::Io(e) => write!(f, "I/O error: {e}"),
60            Self::InvalidVrs => write!(f, "invalid or missing Volume Recognition Sequence"),
61            Self::InvalidVds(reason) => {
62                write!(f, "invalid or missing Volume Descriptor Sequence. {reason}")
63            }
64            Self::InvalidFsd => write!(f, "invalid or missing File Set Descriptor."),
65            Self::NoAnchor => write!(f, "no Anchor Volume Descriptor Pointer found"),
66            Self::InvalidTag { expected, found } => {
67                write!(
68                    f,
69                    "invalid descriptor tag: expected {expected}, found {found}"
70                )
71            }
72            Self::CrcMismatch { expected, computed } => {
73                write!(
74                    f,
75                    "CRC mismatch: expected {expected:04x}, computed {computed:04x}"
76                )
77            }
78            Self::InvalidPartition(num) => write!(f, "invalid partition reference: {num}"),
79            Self::InvalidIcb => write!(f, "invalid Information Control Block"),
80            Self::NotFound => write!(f, "file or directory not found"),
81            Self::NotADirectory => write!(f, "not a directory"),
82            Self::NotAFile => write!(f, "not a file"),
83            Self::PathTooLong => write!(f, "path too long"),
84            Self::InvalidEncoding => write!(f, "invalid filename encoding"),
85            Self::PodCastError(err) => write!(
86                f,
87                "byte casting failed - the data buffer size doesn't match the target struct size. {err}"
88            ),
89        }
90    }
91}
92
93#[cfg(feature = "std")]
94impl std::error::Error for Error {
95    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
96        match self {
97            Self::Io(e) => Some(e),
98            _ => None,
99        }
100    }
101}
102
103/// Result type for UDF operations
104pub type Result<T> = core::result::Result<T, Error>;