Skip to main content

hadris_optical/
error.rs

1use core::fmt;
2
3/// Filesystem requested from an optical image.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum OpticalFormat {
7    /// An ISO 9660 filesystem.
8    Iso9660,
9    /// A Universal Disk Format filesystem.
10    Udf,
11}
12
13/// Error returned by category-level optical operations.
14#[derive(Debug)]
15#[non_exhaustive]
16pub enum Error {
17    /// The source could not be read or repositioned during detection.
18    Io(hadris_io::Error),
19    /// No supported optical filesystem was recognized.
20    UnknownFormat,
21    /// The requested filesystem is not present in the image.
22    RequestedFormatUnavailable(OpticalFormat),
23    /// ISO 9660 validation or opening failed.
24    Iso(hadris_io::Error),
25    /// UDF validation or opening failed.
26    Udf(hadris_udf::Error),
27}
28
29/// Result type for category-level optical operations.
30pub type Result<T> = core::result::Result<T, Error>;
31
32impl fmt::Display for Error {
33    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Self::Io(error) => write!(formatter, "optical image I/O error: {error}"),
36            Self::UnknownFormat => formatter.write_str("unknown optical image format"),
37            Self::RequestedFormatUnavailable(format) => {
38                write!(
39                    formatter,
40                    "requested optical format is unavailable: {format:?}"
41                )
42            }
43            Self::Iso(error) => write!(formatter, "ISO 9660 open failed: {error}"),
44            Self::Udf(error) => write!(formatter, "UDF open failed: {error}"),
45        }
46    }
47}
48
49#[cfg(feature = "std")]
50impl std::error::Error for Error {}
51
52impl From<hadris_io::Error> for Error {
53    fn from(error: hadris_io::Error) -> Self {
54        Self::Io(error)
55    }
56}