use std::fmt;
#[derive(Debug)]
pub enum MfsError {
Io(std::io::Error),
BadSignature { found: u16 },
UnsupportedHfs,
UnknownImageFormat,
Dc42(String),
CorruptVolume(String),
FileNotFound(String),
FileExists(String),
InvalidName(String),
VolumeFull { needed_blocks: u32, free_blocks: u32 },
DirectoryFull,
FileLocked(String),
VolumeLocked,
InvalidGeometry(String),
}
impl fmt::Display for MfsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MfsError::Io(e) => write!(f, "I/O error: {e}"),
MfsError::BadSignature { found } => {
write!(f, "not an MFS volume: signature {found:#06x} (expected 0xd2d7)")
}
MfsError::UnsupportedHfs => {
write!(f, "HFS volume detected (signature 0x4244): HFS is not supported")
}
MfsError::UnknownImageFormat => {
write!(f, "unrecognized image: neither raw MFS nor DiskCopy 4.2")
}
MfsError::Dc42(msg) => write!(f, "DiskCopy 4.2 container: {msg}"),
MfsError::CorruptVolume(msg) => write!(f, "corrupt MFS volume: {msg}"),
MfsError::FileNotFound(name) => write!(f, "file not found: {name}"),
MfsError::FileExists(name) => write!(f, "file already exists: {name}"),
MfsError::InvalidName(msg) => write!(f, "invalid name: {msg}"),
MfsError::VolumeFull { needed_blocks, free_blocks } => write!(
f,
"volume full: need {needed_blocks} allocation blocks, {free_blocks} free"
),
MfsError::DirectoryFull => write!(f, "file directory is full"),
MfsError::FileLocked(name) => write!(f, "file is locked: {name}"),
MfsError::VolumeLocked => write!(f, "volume is locked"),
MfsError::InvalidGeometry(msg) => write!(f, "invalid geometry: {msg}"),
}
}
}
impl std::error::Error for MfsError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
MfsError::Io(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for MfsError {
fn from(e: std::io::Error) -> Self {
MfsError::Io(e)
}
}
pub type Result<T> = std::result::Result<T, MfsError>;