1use hadris_io as io;
4
5#[derive(Debug)]
7pub enum Error {
8 Io(io::Error),
10 InvalidVrs,
12 InvalidVds(&'static str),
14 InvalidFsd,
16 NoAnchor,
18 InvalidTag {
20 expected: u16,
22 found: u16,
24 },
25 CrcMismatch {
27 expected: u16,
29 computed: u16,
31 },
32 InvalidPartition(u16),
34 InvalidIcb,
36 NotFound,
38 NotADirectory,
40 NotAFile,
42 PathTooLong,
44 InvalidEncoding,
46 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
103pub type Result<T> = core::result::Result<T, Error>;