tea_coding_tools/
error.rs1use thiserror::Error;
2
3pub const MAX_WORKSPACE_ERROR_MESSAGE_BYTES: usize = 256;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum WorkspacePathErrorCode {
9 WorkspaceNotFound,
11 WorkspaceNotDirectory,
13 AbsolutePath,
15 ParentTraversal,
17 InvalidPath,
19 PathTooLong,
21 TooManyComponents,
23 TargetNotFound,
25 WorkspaceRootMutation,
27 OutsideWorkspace,
29 ParentNotDirectory,
31 UnresolvableTarget,
33 PathChanged,
35 CapabilityMismatch,
37 FilesystemFailure,
39}
40
41impl WorkspacePathErrorCode {
42 pub(crate) const fn message(self) -> &'static str {
43 match self {
44 Self::WorkspaceNotFound => "workspace root does not exist",
45 Self::WorkspaceNotDirectory => "workspace root is not a directory",
46 Self::AbsolutePath => "absolute workspace paths are not allowed",
47 Self::ParentTraversal => "parent path traversal is not allowed",
48 Self::InvalidPath => "workspace path is invalid",
49 Self::PathTooLong => "workspace path exceeds the byte limit",
50 Self::TooManyComponents => "workspace path has too many components",
51 Self::TargetNotFound => "workspace target does not exist",
52 Self::WorkspaceRootMutation => "the workspace root cannot be mutated as a target",
53 Self::OutsideWorkspace => "workspace path resolves outside the workspace",
54 Self::ParentNotDirectory => "the nearest existing parent is not a directory",
55 Self::UnresolvableTarget => "workspace target cannot be resolved safely",
56 Self::PathChanged => "workspace target changed after resolution",
57 Self::CapabilityMismatch => "workspace target belongs to another capability",
58 Self::FilesystemFailure => "workspace filesystem operation failed",
59 }
60 }
61}
62
63#[derive(Debug, Clone, PartialEq, Eq, Error)]
65#[error("{code:?}: {message}")]
66pub struct WorkspacePathError {
67 code: WorkspacePathErrorCode,
68 message: &'static str,
69}
70
71impl WorkspacePathError {
72 pub(crate) const fn new(code: WorkspacePathErrorCode) -> Self {
73 let message = code.message();
74 debug_assert!(message.len() <= MAX_WORKSPACE_ERROR_MESSAGE_BYTES);
75 Self { code, message }
76 }
77
78 #[must_use]
80 pub const fn code(&self) -> WorkspacePathErrorCode {
81 self.code
82 }
83
84 #[must_use]
86 pub const fn message(&self) -> &'static str {
87 self.message
88 }
89}