Files
zesdex/src/tool/fs/delete.rs
T
asepharyana 29a9fae3f6 ci: add GitHub Actions workflows with semantic-release auto-versioning
chore: fix all 702 clippy warnings across codebase
- auto-fix 475 via cargo clippy --fix
- fix remaining 227 manually: uninlined_format_args, redundant_closure, match_same_arms,
  underscore_binding, format_push_string, items_after_statements, needless_pass_by_value,
  clone_on_copy, case_sensitive_extension, single_match/let-else, write_with_newline,
  and other clippy lints
2026-07-13 08:12:12 +07:00

71 lines
2.3 KiB
Rust

//! Tool: `delete` — remove a file or empty directory relative to a workspace root.
use std::fs;
use std::path::PathBuf;
use serde_json::{json, Value};
use anyhow::{Result, anyhow};
use super::super::Tool;
use super::super::ToolCtx;
use super::super::resolve_path;
use super::helpers::arg_str;
/// Tool: delete a file or empty directory. Refuses non-empty directories.
pub struct Delete;
impl Tool for Delete {
fn name(&self) -> &'static str {
"delete"
}
fn description(&self) -> &'static str {
"Delete a file or empty directory. Will not delete non-empty directories."
}
fn parameters(&self) -> Value {
json!({
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Path to the file or directory to delete (relative to workspace root)"
}
},
"required": ["path"]
})
}
/// Delete a file or empty directory. Returns success message or errors on failure.
///
/// Flow: resolve path → check existence → check dir/file → remove.
/// Only empty directories are deletable (non-empty returns an error).
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
let rel = arg_str(args, "path")?;
let path: PathBuf = resolve_path(&ctx.workspaces, &rel)?;
if !path.exists() {
return Ok(format!("path '{}' does not exist (resolved to {})", rel, path.display()));
}
let metadata = path.metadata()
.map_err(|e| anyhow!("failed to read metadata for '{rel}': {e}"))?;
if metadata.is_dir() {
let is_empty = fs::read_dir(&path)
.map_err(|e| anyhow!("failed to read directory '{rel}': {e}"))?
.next()
.is_none();
if is_empty {
fs::remove_dir(&path)
.map_err(|e| anyhow!("failed to remove directory '{rel}': {e}"))?;
Ok(format!("removed empty directory {rel}"))
} else {
anyhow::bail!("directory '{rel}' is not empty (refusing to delete)");
}
} else {
fs::remove_file(&path)
.map_err(|e| anyhow!("failed to delete '{rel}': {e}"))?;
Ok(format!("deleted {rel}"))
}
}
}