Mountain/IPC/WindServiceHandlers/FileSystem/Native/FileDeleteNative.rs
1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3//! Wire method `file:delete`. Honours `{ recursive }` option for
4//! directories; `useTrash` is accepted but not yet implemented (future
5//! atom: trash.rs on macOS/Linux via `trash-rs`, Windows via SHFileOp).
6
7use serde_json::Value;
8
9use crate::IPC::WindServiceHandlers::Utilities::PathExtraction::extract_path_from_arg;
10
11pub async fn FileDeleteNative(Arguments:Vec<Value>) -> Result<Value, String> {
12 let Path = extract_path_from_arg(Arguments.get(0).ok_or("Missing file path")?)?;
13
14 let Recursive = Arguments
15 .get(1)
16 .and_then(|V| V.as_object())
17 .and_then(|O| O.get("recursive"))
18 .and_then(|V| V.as_bool())
19 .unwrap_or(false);
20
21 let PathBuf = std::path::Path::new(&Path);
22
23 if PathBuf.is_dir() {
24 if Recursive {
25 tokio::fs::remove_dir_all(&Path).await
26 } else {
27 tokio::fs::remove_dir(&Path).await
28 }
29 } else {
30 tokio::fs::remove_file(&Path).await
31 }
32 .map_err(|E| format!("Failed to delete: {} ({})", Path, E))?;
33
34 Ok(Value::Null)
35}