use thiserror::Error;
pub const MAX_WORKSPACE_ERROR_MESSAGE_BYTES: usize = 256;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WorkspacePathErrorCode {
WorkspaceNotFound,
WorkspaceNotDirectory,
AbsolutePath,
ParentTraversal,
InvalidPath,
PathTooLong,
TooManyComponents,
TargetNotFound,
WorkspaceRootMutation,
OutsideWorkspace,
ParentNotDirectory,
UnresolvableTarget,
PathChanged,
CapabilityMismatch,
FilesystemFailure,
}
impl WorkspacePathErrorCode {
pub(crate) const fn message(self) -> &'static str {
match self {
Self::WorkspaceNotFound => "workspace root does not exist",
Self::WorkspaceNotDirectory => "workspace root is not a directory",
Self::AbsolutePath => "absolute workspace paths are not allowed",
Self::ParentTraversal => "parent path traversal is not allowed",
Self::InvalidPath => "workspace path is invalid",
Self::PathTooLong => "workspace path exceeds the byte limit",
Self::TooManyComponents => "workspace path has too many components",
Self::TargetNotFound => "workspace target does not exist",
Self::WorkspaceRootMutation => "the workspace root cannot be mutated as a target",
Self::OutsideWorkspace => "workspace path resolves outside the workspace",
Self::ParentNotDirectory => "the nearest existing parent is not a directory",
Self::UnresolvableTarget => "workspace target cannot be resolved safely",
Self::PathChanged => "workspace target changed after resolution",
Self::CapabilityMismatch => "workspace target belongs to another capability",
Self::FilesystemFailure => "workspace filesystem operation failed",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[error("{code:?}: {message}")]
pub struct WorkspacePathError {
code: WorkspacePathErrorCode,
message: &'static str,
}
impl WorkspacePathError {
pub(crate) const fn new(code: WorkspacePathErrorCode) -> Self {
let message = code.message();
debug_assert!(message.len() <= MAX_WORKSPACE_ERROR_MESSAGE_BYTES);
Self { code, message }
}
#[must_use]
pub const fn code(&self) -> WorkspacePathErrorCode {
self.code
}
#[must_use]
pub const fn message(&self) -> &'static str {
self.message
}
}