Skip to main content

Mountain/Track/Effect/CreateEffectForRequest/
Documents.rs

1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3use std::{future::Future, pin::Pin, sync::Arc};
4
5use CommonLibrary::{Document::DocumentProvider::DocumentProvider, Environment::Requires::Requires};
6use serde_json::{Value, json};
7use tauri::Runtime;
8use url::Url;
9
10use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, Track::Effect::MappedEffectType::MappedEffect};
11
12pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
13	match MethodName {
14		"Document.Save" => {
15			let effect =
16				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
17					Box::pin(async move {
18						let document_provider:Arc<dyn DocumentProvider> = run_time.Environment.Require();
19						let uri_str = Parameters.get(0).and_then(Value::as_str).unwrap_or("");
20						let uri = Url::parse(uri_str).unwrap_or_else(|_| Url::parse("file:///tmp/test.txt").unwrap());
21						document_provider
22							.SaveDocument(uri)
23							.await
24							.map(|success| json!(success))
25							.map_err(|e| e.to_string())
26					})
27				};
28			Some(Ok(Box::new(effect)))
29		},
30
31		"Document.SaveAs" => {
32			let effect =
33				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
34					Box::pin(async move {
35						let document_provider:Arc<dyn DocumentProvider> = run_time.Environment.Require();
36						let original_uri_str = Parameters.get(0).and_then(Value::as_str).unwrap_or("");
37						let original_uri = Url::parse(original_uri_str)
38							.unwrap_or_else(|_| Url::parse("file:///tmp/test.txt").unwrap());
39						let target_uri = Parameters
40							.get(1)
41							.and_then(Value::as_str)
42							.map(Url::parse)
43							.transpose()
44							.unwrap_or(None);
45						document_provider
46							.SaveDocumentAs(original_uri, target_uri)
47							.await
48							.map(|uri_option| json!(uri_option))
49							.map_err(|e| e.to_string())
50					})
51				};
52			Some(Ok(Box::new(effect)))
53		},
54
55		_ => None,
56	}
57}