Skip to main content

Mountain/IPC/WindServiceHandlers/FileSystem/Managed/
FileWrite.rs

1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3//! Legacy wire method `file:write` (UTF-8 content). Routes via RunTime's
4//! `FileSystemWriter` trait. Not currently wired into dispatch.
5
6use std::{path::PathBuf, sync::Arc};
7
8use CommonLibrary::{
9	Environment::Requires::Requires,
10	Error::CommonError::CommonError,
11	FileSystem::FileSystemWriter::FileSystemWriter,
12};
13use serde_json::Value;
14
15use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
16
17pub async fn FileWrite(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
18	let path = Arguments
19		.get(0)
20		.ok_or("Missing file path".to_string())?
21		.as_str()
22		.ok_or("File path must be a string".to_string())?;
23
24	let content = Arguments
25		.get(1)
26		.ok_or("Missing file content".to_string())?
27		.as_str()
28		.ok_or("File content must be a string".to_string())?;
29
30	let provider:Arc<dyn FileSystemWriter> = RunTime.Environment.Require();
31
32	provider
33		.WriteFile(&PathBuf::from(path), content.as_bytes().to_vec(), true, true)
34		.await
35		.map_err(|e:CommonError| format!("Failed to write file: {}", e))?;
36
37	dev_log!("vfs-verbose", "written: {} ({} bytes)", path, content.len());
38	Ok(Value::Null)
39}