Common/Document/
OpenDocument.rs

1// File: Common/Source/Document/OpenDocument.rs
2// Role: Defines the `OpenDocument` ActionEffect.
3// Responsibilities:
4//   - Provide a declarative effect for opening or creating a new text document.
5//   - This effect abstracts the "what" (open a document) from the "how" (the
6//     DocumentProvider implementation).
7
8//! # OpenDocument Effect
9//!
10//! Defines the `ActionEffect` for opening or creating a new text document.
11
12use std::sync::Arc;
13
14use serde_json::Value;
15use url::Url;
16
17use super::DocumentProvider::DocumentProvider;
18use crate::{Effect::ActionEffect::ActionEffect, Error::CommonError::CommonError};
19
20/// Creates an effect that, when executed, will open an existing document from a
21/// URI or create a new untitled document, potentially with initial content.
22///
23/// It uses the `DocumentProvider` capability from the environment to perform
24/// the operation, which may involve file I/O and updating the central document
25/// store.
26///
27/// # Parameters
28///
29/// * `URIComponentsDTO`: A `serde_json::Value` DTO representing the URI of the
30///   document to open, or `null` for a new untitled document.
31/// * `LanguageIdentifier`: An optional language ID, primarily for new untitled
32///   documents.
33/// * `Content`: Optional initial content for a new untitled document.
34///
35/// # Returns
36///
37/// An `ActionEffect` that resolves with the canonical `Url` of the opened
38/// document.
39pub fn OpenDocument(
40	URIComponentsDTO:Value,
41
42	LanguageIdentifier:Option<String>,
43
44	Content:Option<String>,
45) -> ActionEffect<Arc<dyn DocumentProvider>, CommonError, Url> {
46	ActionEffect::New(Arc::new(move |Provider:Arc<dyn DocumentProvider>| {
47		let URIDTOClone = URIComponentsDTO.clone();
48
49		let LanguageIdentifierClone = LanguageIdentifier.clone();
50
51		let ContentClone = Content.clone();
52
53		Box::pin(async move { Provider.OpenDocument(URIDTOClone, LanguageIdentifierClone, ContentClone).await })
54	}))
55}