Mountain/IPC/WindServiceHandlers/Model/
TextfileWrite.rs1
2use std::sync::Arc;
8
9use serde_json::{Value, json};
10
11use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
12
13pub async fn Fn(_runtime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
14 let Path = Arguments
15 .first()
16 .and_then(|V| V.as_str())
17 .ok_or_else(|| "textFile:write requires path as first argument".to_string())?
18 .to_string();
19
20 let Content = Arguments.get(1).and_then(|V| V.as_str()).unwrap_or("").to_string();
21
22 tokio::fs::write(&Path, Content.as_bytes())
23 .await
24 .map_err(|Error| format!("textFile:write failed: {}", Error))?;
25
26 dev_log!("vfs", "textFile:write ok path={} bytes={}", Path, Content.len());
27
28 let FileUri = format!("file://{}", Path);
35
36 tokio::spawn(async move {
37 if let Err(Error) = crate::Vine::Client::SendNotification::Fn(
38 "cocoon-main".to_string(),
39 "$acceptModelSaved".to_string(),
40 json!({ "uri": FileUri }),
41 )
42 .await
43 {
44 dev_log!("vfs", "warn: [TextfileWrite] $acceptModelSaved notify failed: {:?}", Error);
45 }
46 });
47
48 Ok(Value::Null)
49}