use crate::sim::state::FileId;
use std::io;
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum StorageError {
#[error("file not found: {path}")]
NotFound {
path: String,
},
#[error("file already exists: {path}")]
AlreadyExists {
path: String,
},
#[error("invalid file handle: {file_id:?}")]
InvalidFileHandle {
file_id: FileId,
},
#[error("file is closed: {file_id:?}")]
FileClosed {
file_id: FileId,
},
#[error("I/O error on {file_id:?} ({kind:?}): {message}")]
Io {
file_id: FileId,
kind: io::ErrorKind,
message: String,
},
}
impl From<StorageError> for io::Error {
fn from(e: StorageError) -> Self {
let kind = match &e {
StorageError::NotFound { .. } => io::ErrorKind::NotFound,
StorageError::AlreadyExists { .. } => io::ErrorKind::AlreadyExists,
StorageError::InvalidFileHandle { .. } => io::ErrorKind::NotFound,
StorageError::FileClosed { .. } => io::ErrorKind::BrokenPipe,
StorageError::Io { kind, .. } => *kind,
};
io::Error::new(kind, e.to_string())
}
}