use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Operation {
Read,
Write,
Directory,
Lock,
PathResolution,
}
impl Operation {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Read => "read",
Self::Write => "write",
Self::Directory => "directory",
Self::Lock => "lock",
Self::PathResolution => "path resolution",
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum FsError {
#[error("file read error: {path}: {detail}")]
Read {
path: String,
detail: String,
},
#[error("file write error: {path}: {detail}")]
Write {
path: String,
detail: String,
},
#[error("directory error: {path}: {detail}")]
Directory {
path: String,
detail: String,
},
#[error("lock error: {path}: {detail}")]
Lock {
path: String,
detail: String,
},
#[error("path resolution error: {0}")]
PathResolution(String),
#[error("invalid filesystem request: {0}")]
InvalidRequest(String),
}
impl FsError {
#[must_use]
pub fn io(operation: Operation, path: &Path, detail: impl std::fmt::Display) -> Self {
let path = path.display().to_string();
let detail = detail.to_string();
match operation {
Operation::Read => Self::Read { path, detail },
Operation::Write => Self::Write { path, detail },
Operation::Directory => Self::Directory { path, detail },
Operation::Lock => Self::Lock { path, detail },
Operation::PathResolution => Self::PathResolution(detail),
}
}
}