tinyharness_lib/tools/
write.rs1use std::collections::HashMap;
2use std::fs;
3use std::path::Path;
4
5use crate::extract_args;
6use crate::tools::tool::{BoxFuture, ToolCategory, build_string_params_schema, make_tool};
7
8pub fn write_tool_entry() -> crate::tools::tool::Tool {
9 make_tool(
10 "write",
11 "Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Creates parent directories automatically.",
12 ToolCategory::Destructive,
13 build_string_params_schema(
14 &[
15 ("path", "The absolute path to the file to write"),
16 ("content", "The text content to write to the file"),
17 ],
18 &[],
19 ),
20 |args| Box::pin(write_tool(args)),
21 )
22}
23
24pub fn write_tool(args: HashMap<String, String>) -> BoxFuture<'static, String> {
25 Box::pin(async move {
26 extract_args!(args, path, content);
27
28 if let Some(parent) = Path::new(&path).parent()
30 && !parent.as_os_str().is_empty()
31 && let Err(e) = fs::create_dir_all(parent)
32 {
33 return format!("Error: Failed to create parent directories: {}", e);
34 }
35
36 match fs::write(&path, &content) {
37 Ok(_) => format!("Wrote {} bytes to '{}'.", content.len(), path),
38 Err(e) => format!("Error: Failed to write file: {}", e),
39 }
40 })
41}