use std::fmt;
use std::io;
use std::path::PathBuf;
pub type DiskResult<T> = Result<T, DiskError>;
#[derive(Debug)]
pub enum DiskError {
Io(io::Error),
InvalidFormat { path: PathBuf, reason: String },
PartitionError { reason: String },
FilesystemError { partition: u32, reason: String },
Qcow2Error { reason: String },
OutOfBounds { requested: u64, size: u64 },
Unsupported { feature: String },
}
impl fmt::Display for DiskError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DiskError::Io(e) => write!(f, "I/O error: {}", e),
DiskError::InvalidFormat { path, reason } => {
write!(f, "Invalid image format for {:?}: {}", path, reason)
}
DiskError::PartitionError { reason } => {
write!(f, "Partition table error: {}", reason)
}
DiskError::FilesystemError { partition, reason } => {
write!(f, "Filesystem error on partition {}: {}", partition, reason)
}
DiskError::Qcow2Error { reason } => {
write!(f, "QCOW2 error: {}", reason)
}
DiskError::OutOfBounds { requested, size } => {
write!(
f,
"Offset {} is out of bounds (image size: {})",
requested, size
)
}
DiskError::Unsupported { feature } => {
write!(f, "Unsupported feature: {}", feature)
}
}
}
}
impl std::error::Error for DiskError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
DiskError::Io(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for DiskError {
fn from(err: io::Error) -> Self {
DiskError::Io(err)
}
}