Mountain/IPC/WindServiceHandlers/FileSystem/Native/
FileWriteNative.rs1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3use serde_json::Value;
8
9use crate::IPC::WindServiceHandlers::Utilities::PathExtraction::extract_path_from_arg;
10
11pub async fn FileWriteNative(Arguments:Vec<Value>) -> Result<Value, String> {
12 let Path = extract_path_from_arg(Arguments.get(0).ok_or("Missing file path")?)?;
13
14 let Content = Arguments.get(1).ok_or("Missing file content")?;
15
16 let Bytes = if let Some(S) = Content.as_str() {
17 S.as_bytes().to_vec()
18 } else if let Some(Obj) = Content.as_object() {
19 if let Some(Buf) = Obj.get("buffer") {
20 if let Some(Arr) = Buf.as_array() {
21 Arr.iter().filter_map(|V| V.as_u64().map(|N| N as u8)).collect()
22 } else if let Some(S) = Buf.as_str() {
23 S.as_bytes().to_vec()
24 } else {
25 return Err("Unsupported buffer format".to_string());
26 }
27 } else {
28 serde_json::to_string(Content).unwrap_or_default().into_bytes()
29 }
30 } else {
31 return Err("File content must be a string or VSBuffer".to_string());
32 };
33
34 if let Some(Parent) = std::path::Path::new(&Path).parent() {
35 tokio::fs::create_dir_all(Parent).await.ok();
36 }
37
38 tokio::fs::write(&Path, &Bytes)
39 .await
40 .map_err(|E| format!("Failed to write file: {} (path: {})", E, Path))?;
41
42 Ok(Value::Null)
43}