Mountain/IPC/WindServiceHandlers/Model/TextfileWrite.rs
1#![allow(non_snake_case)]
2
3//! Write text to a file on disk. Counterpart to `TextfileRead`;
4//! does not touch the document registry. Callers wanting Monaco
5//! to observe the change should follow up with
6//! `ModelUpdateContent` for the same URI.
7
8use std::sync::Arc;
9
10use serde_json::Value;
11
12use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
13
14pub async fn TextfileWrite(_runtime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
15 let Path = Arguments
16 .first()
17 .and_then(|V| V.as_str())
18 .ok_or_else(|| "textFile:write requires path as first argument".to_string())?;
19 let Content = Arguments.get(1).and_then(|V| V.as_str()).unwrap_or("").to_string();
20
21 tokio::fs::write(Path, Content.as_bytes())
22 .await
23 .map(|()| Value::Null)
24 .map_err(|Error| format!("textFile:write failed: {}", Error))
25}