Skip to main content

Mountain/IPC/WindServiceHandler/
TextFile.rs

1#![allow(non_snake_case)]
2
3//! TextFile domain handlers for Wind IPC.
4
5use std::sync::Arc;
6
7use serde_json::Value;
8
9use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
10
11/// Read a text file from disk.
12pub async fn handle_textfile_read(_Runtime:Arc<ApplicationRunTime>, Args:Vec<Value>) -> Result<Value, String> {
13	let Path = Args
14		.first()
15		.and_then(|V| V.as_str())
16		.ok_or_else(|| "textFile:read requires path as first argument".to_string())?;
17
18	tokio::fs::read_to_string(Path)
19		.await
20		.map(Value::String)
21		.map_err(|Error| format!("textFile:read failed: {}", Error))
22}
23
24/// Write text to a file on disk.
25pub async fn handle_textfile_write(_Runtime:Arc<ApplicationRunTime>, Args:Vec<Value>) -> Result<Value, String> {
26	let Path = Args
27		.first()
28		.and_then(|V| V.as_str())
29		.ok_or_else(|| "textFile:write requires path as first argument".to_string())?;
30	let Content = Args.get(1).and_then(|V| V.as_str()).unwrap_or("").to_string();
31
32	tokio::fs::write(Path, Content.as_bytes())
33		.await
34		.map(|()| Value::Null)
35		.map_err(|Error| format!("textFile:write failed: {}", Error))
36}
37
38/// Save a document - forward save intent to Sky frontend.
39pub async fn handle_textfile_save(_Runtime:Arc<ApplicationRunTime>, Args:Vec<Value>) -> Result<Value, String> {
40	let _Uri = Args.first().and_then(|V| V.as_str()).unwrap_or("").to_string();
41	dev_log!("vfs", "textFile:save uri={:?}", _Uri);
42	Ok(Value::Null)
43}