Skip to main content

tea_coding_tools/
file_error.rs

1use thiserror::Error;
2
3use crate::{WorkspacePathError, WorkspacePathErrorCode};
4
5/// Stable machine-readable native file-tool failure classification.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum FileToolErrorCode {
8    /// Invocation arguments were absent despite schema validation.
9    InvalidArguments,
10    /// A workspace path was invalid, changed, or escaped containment.
11    InvalidPath,
12    /// The requested target was not found.
13    NotFound,
14    /// A regular file was required.
15    NotAFile,
16    /// File contents were not supported UTF-8 text.
17    InvalidUtf8,
18    /// File contents appeared to be binary.
19    BinaryFile,
20    /// A source or resulting file exceeded deterministic bounds.
21    TooLarge,
22    /// Exact edit text was not present.
23    NoMatch,
24    /// Exact edit text had an unexpected number of matches.
25    MatchCountMismatch,
26    /// The file changed after it was read for editing.
27    PathChanged,
28    /// A safe atomic commit could not be completed.
29    AtomicCommitFailed,
30    /// A filesystem operation failed without exposing host diagnostics.
31    FilesystemFailure,
32    /// A configured process could not be spawned or awaited.
33    ProcessFailure,
34    /// Captured process output exceeded its spill bound.
35    OutputLimit,
36    /// An internal result-contract invariant failed.
37    Internal,
38}
39
40impl FileToolErrorCode {
41    /// Returns the canonical snake-case code used in structured details.
42    #[must_use]
43    pub const fn as_str(self) -> &'static str {
44        match self {
45            Self::InvalidArguments => "invalid_arguments",
46            Self::InvalidPath => "invalid_path",
47            Self::NotFound => "not_found",
48            Self::NotAFile => "not_a_file",
49            Self::InvalidUtf8 => "invalid_utf8",
50            Self::BinaryFile => "binary_file",
51            Self::TooLarge => "too_large",
52            Self::NoMatch => "no_match",
53            Self::MatchCountMismatch => "match_count_mismatch",
54            Self::PathChanged => "path_changed",
55            Self::AtomicCommitFailed => "atomic_commit_failed",
56            Self::FilesystemFailure => "filesystem_failure",
57            Self::ProcessFailure => "process_failure",
58            Self::OutputLimit => "output_limit",
59            Self::Internal => "internal",
60        }
61    }
62
63    const fn message(self) -> &'static str {
64        match self {
65            Self::InvalidArguments => "validated file tool arguments are invalid",
66            Self::InvalidPath => "workspace file path is invalid or changed",
67            Self::NotFound => "workspace file does not exist",
68            Self::NotAFile => "workspace target is not a regular file",
69            Self::InvalidUtf8 => "workspace file is not valid UTF-8 text",
70            Self::BinaryFile => "workspace file appears to be binary",
71            Self::TooLarge => "workspace file exceeds the supported byte limit",
72            Self::NoMatch => "edit text was not found",
73            Self::MatchCountMismatch => "edit text match count is not the expected value",
74            Self::PathChanged => "workspace file changed during the edit operation",
75            Self::AtomicCommitFailed => "workspace file could not be replaced atomically",
76            Self::FilesystemFailure => "workspace file operation failed",
77            Self::ProcessFailure => "configured process execution failed",
78            Self::OutputLimit => "process output exceeded the supported spill limit",
79            Self::Internal => "tool produced an invalid internal result",
80        }
81    }
82}
83
84/// Bounded path-independent file tool error.
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
86#[error("{code:?}: {message}")]
87pub struct FileToolError {
88    code: FileToolErrorCode,
89    message: &'static str,
90}
91
92impl FileToolError {
93    pub(crate) const fn new(code: FileToolErrorCode) -> Self {
94        Self {
95            code,
96            message: code.message(),
97        }
98    }
99
100    /// Returns the stable machine-readable classification.
101    #[must_use]
102    pub const fn code(self) -> FileToolErrorCode {
103        self.code
104    }
105
106    /// Returns a path-independent technical message.
107    #[must_use]
108    pub const fn message(self) -> &'static str {
109        self.message
110    }
111}
112
113impl From<WorkspacePathError> for FileToolError {
114    fn from(error: WorkspacePathError) -> Self {
115        let code = match error.code() {
116            WorkspacePathErrorCode::TargetNotFound => FileToolErrorCode::NotFound,
117            WorkspacePathErrorCode::FilesystemFailure => FileToolErrorCode::FilesystemFailure,
118            _ => FileToolErrorCode::InvalidPath,
119        };
120        Self::new(code)
121    }
122}