robinpath_modules/modules/
fs_mod.rs1use robinpath::{RobinPath, Value};
2
3pub fn register(rp: &mut RobinPath) {
4 rp.register_builtin("fs.read", |args, _| {
5 let path = args.first().map(|v| v.to_display_string()).unwrap_or_default();
6 match std::fs::read_to_string(&path) {
7 Ok(content) => Ok(Value::String(content)),
8 Err(e) => Err(format!("fs.read error: {}", e)),
9 }
10 });
11
12 rp.register_builtin("fs.write", |args, _| {
13 let path = args.first().map(|v| v.to_display_string()).unwrap_or_default();
14 let content = args.get(1).map(|v| v.to_display_string()).unwrap_or_default();
15 match std::fs::write(&path, &content) {
16 Ok(()) => Ok(Value::Bool(true)),
17 Err(e) => Err(format!("fs.write error: {}", e)),
18 }
19 });
20
21 rp.register_builtin("fs.append", |args, _| {
22 let path = args.first().map(|v| v.to_display_string()).unwrap_or_default();
23 let content = args.get(1).map(|v| v.to_display_string()).unwrap_or_default();
24 use std::io::Write;
25 match std::fs::OpenOptions::new().append(true).create(true).open(&path) {
26 Ok(mut file) => match file.write_all(content.as_bytes()) {
27 Ok(()) => Ok(Value::Bool(true)),
28 Err(e) => Err(format!("fs.append error: {}", e)),
29 },
30 Err(e) => Err(format!("fs.append error: {}", e)),
31 }
32 });
33
34 rp.register_builtin("fs.exists", |args, _| {
35 let path = args.first().map(|v| v.to_display_string()).unwrap_or_default();
36 Ok(Value::Bool(std::path::Path::new(&path).exists()))
37 });
38
39 rp.register_builtin("fs.delete", |args, _| {
40 let path = args.first().map(|v| v.to_display_string()).unwrap_or_default();
41 match std::fs::remove_file(&path) {
42 Ok(()) => Ok(Value::Bool(true)),
43 Err(e) => Err(format!("fs.delete error: {}", e)),
44 }
45 });
46
47 rp.register_builtin("fs.copy", |args, _| {
48 let src = args.first().map(|v| v.to_display_string()).unwrap_or_default();
49 let dest = args.get(1).map(|v| v.to_display_string()).unwrap_or_default();
50 match std::fs::copy(&src, &dest) {
51 Ok(_) => Ok(Value::Bool(true)),
52 Err(e) => Err(format!("fs.copy error: {}", e)),
53 }
54 });
55
56 rp.register_builtin("fs.move", |args, _| {
57 let src = args.first().map(|v| v.to_display_string()).unwrap_or_default();
58 let dest = args.get(1).map(|v| v.to_display_string()).unwrap_or_default();
59 match std::fs::rename(&src, &dest) {
60 Ok(()) => Ok(Value::Bool(true)),
61 Err(e) => Err(format!("fs.move error: {}", e)),
62 }
63 });
64
65 rp.register_builtin("fs.rename", |args, _| {
66 let old_path = args.first().map(|v| v.to_display_string()).unwrap_or_default();
67 let new_path = args.get(1).map(|v| v.to_display_string()).unwrap_or_default();
68 match std::fs::rename(&old_path, &new_path) {
69 Ok(()) => Ok(Value::Bool(true)),
70 Err(e) => Err(format!("fs.rename error: {}", e)),
71 }
72 });
73
74 rp.register_builtin("fs.list", |args, _| {
75 let dir = args.first().map(|v| v.to_display_string()).unwrap_or_else(|| ".".to_string());
76 match std::fs::read_dir(&dir) {
77 Ok(entries) => {
78 let mut items = Vec::new();
79 for entry in entries.flatten() {
80 items.push(Value::String(
81 entry.file_name().to_string_lossy().to_string(),
82 ));
83 }
84 Ok(Value::Array(items))
85 }
86 Err(e) => Err(format!("fs.list error: {}", e)),
87 }
88 });
89
90 rp.register_builtin("fs.mkdir", |args, _| {
91 let path = args.first().map(|v| v.to_display_string()).unwrap_or_default();
92 match std::fs::create_dir_all(&path) {
93 Ok(()) => Ok(Value::Bool(true)),
94 Err(e) => Err(format!("fs.mkdir error: {}", e)),
95 }
96 });
97
98 rp.register_builtin("fs.rmdir", |args, _| {
99 let path = args.first().map(|v| v.to_display_string()).unwrap_or_default();
100 match std::fs::remove_dir_all(&path) {
101 Ok(()) => Ok(Value::Bool(true)),
102 Err(e) => Err(format!("fs.rmdir error: {}", e)),
103 }
104 });
105
106 rp.register_builtin("fs.stat", |args, _| {
107 let path = args.first().map(|v| v.to_display_string()).unwrap_or_default();
108 match std::fs::metadata(&path) {
109 Ok(meta) => {
110 let mut obj = indexmap::IndexMap::new();
111 obj.insert("size".to_string(), Value::Number(meta.len() as f64));
112 obj.insert("isFile".to_string(), Value::Bool(meta.is_file()));
113 obj.insert("isDirectory".to_string(), Value::Bool(meta.is_dir()));
114 if let Ok(modified) = meta.modified() {
115 if let Ok(dur) = modified.duration_since(std::time::UNIX_EPOCH) {
116 obj.insert("modified".to_string(), Value::Number(dur.as_secs() as f64));
117 }
118 }
119 if let Ok(created) = meta.created() {
120 if let Ok(dur) = created.duration_since(std::time::UNIX_EPOCH) {
121 obj.insert("created".to_string(), Value::Number(dur.as_secs() as f64));
122 }
123 }
124 Ok(Value::Object(obj))
125 }
126 Err(e) => Err(format!("fs.stat error: {}", e)),
127 }
128 });
129
130 rp.register_builtin("fs.isFile", |args, _| {
131 let path = args.first().map(|v| v.to_display_string()).unwrap_or_default();
132 Ok(Value::Bool(std::path::Path::new(&path).is_file()))
133 });
134
135 rp.register_builtin("fs.isDir", |args, _| {
136 let path = args.first().map(|v| v.to_display_string()).unwrap_or_default();
137 Ok(Value::Bool(std::path::Path::new(&path).is_dir()))
138 });
139}