tea_coding_tools/
file_error.rs1use thiserror::Error;
2
3use crate::{WorkspacePathError, WorkspacePathErrorCode};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum FileToolErrorCode {
8 InvalidArguments,
10 InvalidPath,
12 NotFound,
14 NotAFile,
16 InvalidUtf8,
18 BinaryFile,
20 TooLarge,
22 NoMatch,
24 MatchCountMismatch,
26 PathChanged,
28 AtomicCommitFailed,
30 FilesystemFailure,
32 ProcessFailure,
34 OutputLimit,
36 Internal,
38}
39
40impl FileToolErrorCode {
41 #[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#[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 #[must_use]
102 pub const fn code(self) -> FileToolErrorCode {
103 self.code
104 }
105
106 #[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}