Mountain/Track/Effect/CreateEffectForRequest/
Documents.rs1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3use std::{future::Future, pin::Pin, sync::Arc};
16
17use CommonLibrary::{Document::DocumentProvider::DocumentProvider, Environment::Requires::Requires};
18use serde_json::{Value, json};
19use tauri::Runtime;
20use url::Url;
21
22use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, Track::Effect::MappedEffectType::MappedEffect};
23
24pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
25 match MethodName {
26 "Document.Save" => {
27 let effect =
28 move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
29 Box::pin(async move {
30 let document_provider:Arc<dyn DocumentProvider> = run_time.Environment.Require();
31 let uri_str = Parameters.get(0).and_then(Value::as_str).unwrap_or("");
32 if uri_str.is_empty() {
33 return Err("Document.Save: empty URI (resource not found)".to_string());
34 }
35 let uri = Url::parse(uri_str)
36 .map_err(|e| format!("Document.Save: invalid URI '{}': {}", uri_str, e))?;
37 document_provider
38 .SaveDocument(uri)
39 .await
40 .map(|success| json!(success))
41 .map_err(|e| e.to_string())
42 })
43 };
44
45 Some(Ok(Box::new(effect)))
46 },
47
48 "Document.SaveAs" => {
49 let effect =
50 move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
51 Box::pin(async move {
52 let document_provider:Arc<dyn DocumentProvider> = run_time.Environment.Require();
53 let original_uri_str = Parameters.get(0).and_then(Value::as_str).unwrap_or("");
54 if original_uri_str.is_empty() {
55 return Err("Document.SaveAs: empty URI (resource not found)".to_string());
56 }
57 let original_uri = Url::parse(original_uri_str)
58 .map_err(|e| format!("Document.SaveAs: invalid URI '{}': {}", original_uri_str, e))?;
59 let target_uri = Parameters
60 .get(1)
61 .and_then(Value::as_str)
62 .map(Url::parse)
63 .transpose()
64 .unwrap_or(None);
65 document_provider
66 .SaveDocumentAs(original_uri, target_uri)
67 .await
68 .map(|uri_option| json!(uri_option))
69 .map_err(|e| e.to_string())
70 })
71 };
72
73 Some(Ok(Box::new(effect)))
74 },
75
76 _ => None,
77 }
78}