Skip to main content

tea_coding_tools/
error.rs

1use thiserror::Error;
2
3/// Maximum UTF-8 bytes exposed by a workspace-path diagnostic.
4pub const MAX_WORKSPACE_ERROR_MESSAGE_BYTES: usize = 256;
5
6/// Stable machine-readable classification for workspace capability failures.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum WorkspacePathErrorCode {
9    /// The requested workspace root does not exist.
10    WorkspaceNotFound,
11    /// The requested workspace root is not a directory.
12    WorkspaceNotDirectory,
13    /// An absolute model-supplied path was rejected.
14    AbsolutePath,
15    /// A model-supplied path contained a parent traversal component.
16    ParentTraversal,
17    /// A model-supplied path was empty, ambiguous, or platform-specific.
18    InvalidPath,
19    /// A model-supplied path exceeded its UTF-8 byte bound.
20    PathTooLong,
21    /// A model-supplied path exceeded its component-count bound.
22    TooManyComponents,
23    /// An existing target was required but was not found.
24    TargetNotFound,
25    /// A mutation attempted to target the workspace root itself.
26    WorkspaceRootMutation,
27    /// A resolved target or ancestor escaped the workspace.
28    OutsideWorkspace,
29    /// The nearest existing mutation ancestor was not a directory.
30    ParentNotDirectory,
31    /// A symlink or filesystem object could not be resolved safely.
32    UnresolvableTarget,
33    /// A mutation target changed after it was resolved.
34    PathChanged,
35    /// A resolved mutation target belongs to another workspace capability.
36    CapabilityMismatch,
37    /// A filesystem operation failed without a safe path-specific diagnostic.
38    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/// Bounded path-independent error safe for model, terminal, and JSON output.
64#[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    /// Returns the stable machine-readable failure classification.
79    #[must_use]
80    pub const fn code(&self) -> WorkspacePathErrorCode {
81        self.code
82    }
83
84    /// Returns a bounded diagnostic that never embeds caller or host paths.
85    #[must_use]
86    pub const fn message(&self) -> &'static str {
87        self.message
88    }
89}