Mountain/Track/Effect/CreateEffectForRequest/
FileReadAlias.rs1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3use std::{future::Future, pin::Pin, sync::Arc};
9
10use CommonLibrary::{Environment::Requires::Requires, FileSystem::FileSystemReader::FileSystemReader};
11use serde_json::{Value, json};
12use tauri::Runtime;
13
14use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, Track::Effect::MappedEffectType::MappedEffect};
15
16pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
17 match MethodName {
18 "openDocument" | "readFile" | "stat" => {
19 let MethodNameOwned = MethodName.to_string();
20 let effect =
21 move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
22 Box::pin(async move {
23 let fs_reader:Arc<dyn FileSystemReader> = run_time.Environment.Require();
24 let Path = if let Some(Object) = Parameters.as_object() {
25 Object
26 .get("uri")
27 .or_else(|| Object.get("path"))
28 .and_then(Value::as_str)
29 .unwrap_or("")
30 .to_string()
31 } else {
32 Parameters.get(0).and_then(Value::as_str).unwrap_or("").to_string()
33 };
34 let PathBuf_ = std::path::PathBuf::from(&Path);
35 match MethodNameOwned.as_str() {
36 "stat" => {
37 fs_reader
38 .StatFile(&PathBuf_)
39 .await
40 .map(|S| serde_json::to_value(S).unwrap_or(Value::Null))
41 .map_err(|e| e.to_string())
42 },
43 "readFile" | "openDocument" => {
44 fs_reader
45 .ReadFile(&PathBuf_)
46 .await
47 .map(|Bytes| {
48 let Text = String::from_utf8(Bytes).unwrap_or_default();
49 json!({ "uri": Path, "text": Text })
50 })
51 .map_err(|e| e.to_string())
52 },
53 _ => Ok(Value::Null),
54 }
55 })
56 };
57 Some(Ok(Box::new(effect)))
58 },
59
60 _ => None,
61 }
62}