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
16fn StripFileUriScheme(Input:&str) -> &str {
23 if let Some(Rest) = Input.strip_prefix("file://") {
24 if Rest.starts_with('/') {
25 return Rest;
26 }
27 if let Some(Idx) = Rest.find('/') {
28 return &Rest[Idx..];
29 }
30 }
31 Input
32}
33
34pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
35 match MethodName {
36 "openDocument" | "readFile" | "stat" => {
37 let MethodNameOwned = MethodName.to_string();
38
39 let effect =
40 move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
41 Box::pin(async move {
42 let fs_reader:Arc<dyn FileSystemReader> = run_time.Environment.Require();
43 let Path = if let Some(Object) = Parameters.as_object() {
44 Object
45 .get("uri")
46 .or_else(|| Object.get("path"))
47 .and_then(Value::as_str)
48 .unwrap_or("")
49 .to_string()
50 } else {
51 Parameters.get(0).and_then(Value::as_str).unwrap_or("").to_string()
52 };
53 if Path.is_empty() {
58 return Err(format!("{}: empty path (resource not found)", MethodNameOwned));
59 }
60 let PathBuf_ = std::path::PathBuf::from(StripFileUriScheme(&Path));
61 match MethodNameOwned.as_str() {
62 "stat" => {
63 fs_reader
64 .StatFile(&PathBuf_)
65 .await
66 .map(|S| serde_json::to_value(S).unwrap_or(Value::Null))
67 .map_err(|e| e.to_string())
68 },
69 "readFile" | "openDocument" => {
70 fs_reader
71 .ReadFile(&PathBuf_)
72 .await
73 .map(|Bytes| {
74 let Text = String::from_utf8(Bytes).unwrap_or_default();
75 json!({ "uri": Path, "text": Text })
76 })
77 .map_err(|e| e.to_string())
78 },
79 _ => Ok(Value::Null),
80 }
81 })
82 };
83
84 Some(Ok(Box::new(effect)))
85 },
86
87 _ => None,
88 }
89}