Mountain/Track/Effect/CreateEffectForRequest/
FileReadAlias.rs1use std::sync::Arc;
7
8use CommonLibrary::{Environment::Requires::Requires, FileSystem::FileSystemReader::FileSystemReader};
9use serde_json::{Value, json};
10use tauri::Runtime;
11
12use crate::Track::Effect::{
13 CreateEffectForRequest::Utilities::Params::{str_obj_or_pos, strip_file_uri},
14 MappedEffectType::MappedEffect,
15};
16
17pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
18 match MethodName {
19 "openDocument" | "readFile" | "stat" => {
20 let MethodNameOwned = MethodName.to_string();
21
22 crate::effect!(run_time, {
23 let fs_reader:Arc<dyn FileSystemReader> = run_time.Environment.Require();
24
25 let Path = {
26 let s = str_obj_or_pos(&Parameters, "uri", 0);
27
28 if s.is_empty() { str_obj_or_pos(&Parameters, "path", 0) } else { s }
29 }
30 .to_string();
31
32 if Path.is_empty() {
37 return Err(format!("{}: empty path (resource not found)", MethodNameOwned));
38 }
39
40 let PathBuf_ = std::path::PathBuf::from(strip_file_uri(&Path));
41
42 match MethodNameOwned.as_str() {
43 "stat" => {
44 fs_reader
45 .StatFile(&PathBuf_)
46 .await
47 .map(|S| serde_json::to_value(S).unwrap_or(Value::Null))
48 .map_err(|e| e.to_string())
49 },
50 "readFile" | "openDocument" => {
51 fs_reader
52 .ReadFile(&PathBuf_)
53 .await
54 .map(|Bytes| {
55 let Text = String::from_utf8(Bytes).unwrap_or_default();
56
57 json!({ "uri": Path, "text": Text })
58 })
59 .map_err(|e| e.to_string())
60 },
61 _ => Ok(Value::Null),
62 }
63 })
64 },
65
66 _ => None,
67 }
68}