Skip to main content

Mountain/Track/Effect/CreateEffectForRequest/
Documents.rs

1//! # Documents Effect (CreateEffectForRequest)
2//!
3//! Effect constructors for the `Document.*` RPC family. Delegates to the
4//! `DocumentProvider` trait on `MountainEnvironment` for save operations.
5//!
6//! ## Methods handled
7//!
8//! | Method | Description |
9//! |---|---|
10//! | `Document.Save` | Save the document at the given URI to disk |
11//! | `Document.SaveAs` | Save the document to a new location specified by the caller |
12
13use std::sync::Arc;
14
15use CommonLibrary::{Document::DocumentProvider::DocumentProvider, Environment::Requires::Requires};
16use serde_json::{Value, json};
17use tauri::Runtime;
18use url::Url;
19
20use crate::Track::Effect::{CreateEffectForRequest::Utilities::Params::str_at, MappedEffectType::MappedEffect};
21
22pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
23	match MethodName {
24		"Document.Save" => {
25			crate::effect!(run_time, {
26				let document_provider:Arc<dyn DocumentProvider> = run_time.Environment.Require();
27
28				let uri_str = str_at(&Parameters, 0);
29
30				if uri_str.is_empty() {
31					return Err("Document.Save: empty URI (resource not found)".to_string());
32				}
33
34				let uri =
35					Url::parse(uri_str).map_err(|e| format!("Document.Save: invalid URI '{}': {}", uri_str, e))?;
36
37				document_provider
38					.SaveDocument(uri)
39					.await
40					.map(|success| json!(success))
41					.map_err(|e| e.to_string())
42			})
43		},
44
45		"Document.SaveAs" => {
46			crate::effect!(run_time, {
47				let document_provider:Arc<dyn DocumentProvider> = run_time.Environment.Require();
48
49				let original_uri_str = str_at(&Parameters, 0);
50
51				if original_uri_str.is_empty() {
52					return Err("Document.SaveAs: empty URI (resource not found)".to_string());
53				}
54
55				let original_uri = Url::parse(original_uri_str)
56					.map_err(|e| format!("Document.SaveAs: invalid URI '{}': {}", original_uri_str, e))?;
57
58				let target_uri = Parameters
59					.get(1)
60					.and_then(Value::as_str)
61					.map(Url::parse)
62					.transpose()
63					.unwrap_or(None);
64
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		_ => None,
74	}
75}