use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq)]
pub enum StorageError {
DirectoryNotFound(PathBuf),
DirectoryAlreadyExists(PathBuf),
Io { path: PathBuf, reason: String },
PageOutOfBounds { page_id: u32, num_pages: u32 },
BufferPoolFull,
TupleError(String),
DuplicateKey,
IndexNotInitialized,
}
impl StorageError {
pub(crate) fn io(path: impl Into<PathBuf>, e: std::io::Error) -> Self {
StorageError::Io {
path: path.into(),
reason: e.to_string(),
}
}
}
impl std::fmt::Display for StorageError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
StorageError::DirectoryNotFound(p) => {
write!(f, "directory not found: {}", p.display())
}
StorageError::DirectoryAlreadyExists(p) => {
write!(f, "directory already exists: {}", p.display())
}
StorageError::Io { path, reason } => {
write!(f, "I/O error at {}: {}", path.display(), reason)
}
StorageError::PageOutOfBounds { page_id, num_pages } => {
write!(
f,
"page {} out of bounds (file has {} pages)",
page_id, num_pages
)
}
StorageError::BufferPoolFull => {
write!(f, "buffer pool is full: all frames are pinned")
}
StorageError::TupleError(msg) => {
write!(f, "tuple error: {}", msg)
}
StorageError::DuplicateKey => {
write!(f, "duplicate key violates unique constraint")
}
StorageError::IndexNotInitialized => {
write!(f, "index has no pages yet")
}
}
}
}
impl std::error::Error for StorageError {}