Skip to main content

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

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