use thiserror::Error;
use crate::{WorkspacePathError, WorkspacePathErrorCode};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileToolErrorCode {
InvalidArguments,
InvalidPath,
NotFound,
NotAFile,
InvalidUtf8,
BinaryFile,
TooLarge,
NoMatch,
MatchCountMismatch,
PathChanged,
AtomicCommitFailed,
FilesystemFailure,
ProcessFailure,
OutputLimit,
Internal,
}
impl FileToolErrorCode {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::InvalidArguments => "invalid_arguments",
Self::InvalidPath => "invalid_path",
Self::NotFound => "not_found",
Self::NotAFile => "not_a_file",
Self::InvalidUtf8 => "invalid_utf8",
Self::BinaryFile => "binary_file",
Self::TooLarge => "too_large",
Self::NoMatch => "no_match",
Self::MatchCountMismatch => "match_count_mismatch",
Self::PathChanged => "path_changed",
Self::AtomicCommitFailed => "atomic_commit_failed",
Self::FilesystemFailure => "filesystem_failure",
Self::ProcessFailure => "process_failure",
Self::OutputLimit => "output_limit",
Self::Internal => "internal",
}
}
const fn message(self) -> &'static str {
match self {
Self::InvalidArguments => "validated file tool arguments are invalid",
Self::InvalidPath => "workspace file path is invalid or changed",
Self::NotFound => "workspace file does not exist",
Self::NotAFile => "workspace target is not a regular file",
Self::InvalidUtf8 => "workspace file is not valid UTF-8 text",
Self::BinaryFile => "workspace file appears to be binary",
Self::TooLarge => "workspace file exceeds the supported byte limit",
Self::NoMatch => "edit text was not found",
Self::MatchCountMismatch => "edit text match count is not the expected value",
Self::PathChanged => "workspace file changed during the edit operation",
Self::AtomicCommitFailed => "workspace file could not be replaced atomically",
Self::FilesystemFailure => "workspace file operation failed",
Self::ProcessFailure => "configured process execution failed",
Self::OutputLimit => "process output exceeded the supported spill limit",
Self::Internal => "tool produced an invalid internal result",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[error("{code:?}: {message}")]
pub struct FileToolError {
code: FileToolErrorCode,
message: &'static str,
}
impl FileToolError {
pub(crate) const fn new(code: FileToolErrorCode) -> Self {
Self {
code,
message: code.message(),
}
}
#[must_use]
pub const fn code(self) -> FileToolErrorCode {
self.code
}
#[must_use]
pub const fn message(self) -> &'static str {
self.message
}
}
impl From<WorkspacePathError> for FileToolError {
fn from(error: WorkspacePathError) -> Self {
let code = match error.code() {
WorkspacePathErrorCode::TargetNotFound => FileToolErrorCode::NotFound,
WorkspacePathErrorCode::FilesystemFailure => FileToolErrorCode::FilesystemFailure,
_ => FileToolErrorCode::InvalidPath,
};
Self::new(code)
}
}