Skip to main content

Mountain/Track/Effect/CreateEffectForRequest/
FileReadAlias.rs

1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3//! Cocoon legacy aliases: `openDocument`, `readFile`, `stat` - short-hand
4//! routes used by Cocoon's Effect-TS Workspace + FileSystem services before
5//! the canonical `FileSystem.*` naming was established. Backed by the same
6//! `FileSystemReader` provider.
7
8use std::{future::Future, pin::Pin, sync::Arc};
9
10use CommonLibrary::{Environment::Requires::Requires, FileSystem::FileSystemReader::FileSystemReader};
11use serde_json::{Value, json};
12use tauri::Runtime;
13
14use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, Track::Effect::MappedEffectType::MappedEffect};
15
16/// Strip a leading `file://` (or `file:///`) scheme from the incoming path.
17/// Mirrors the helper in `FileSystem.rs`; inlined here to avoid a cross-module
18/// dependency on a private function in a sibling module.
19/// Cocoon sends full URIs like `file:///<home>/.land/extensions/...` through
20/// the legacy `openDocument`/`readFile`/`stat` routes; without stripping,
21/// `PathBuf` roots at the literal scheme string and every read 404s.
22fn StripFileUriScheme(Input:&str) -> &str {
23	if let Some(Rest) = Input.strip_prefix("file://") {
24		if Rest.starts_with('/') {
25			return Rest;
26		}
27		if let Some(Idx) = Rest.find('/') {
28			return &Rest[Idx..];
29		}
30	}
31	Input
32}
33
34pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
35	match MethodName {
36		"openDocument" | "readFile" | "stat" => {
37			let MethodNameOwned = MethodName.to_string();
38
39			let effect =
40				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
41					Box::pin(async move {
42						let fs_reader:Arc<dyn FileSystemReader> = run_time.Environment.Require();
43						let Path = if let Some(Object) = Parameters.as_object() {
44							Object
45								.get("uri")
46								.or_else(|| Object.get("path"))
47								.and_then(Value::as_str)
48								.unwrap_or("")
49								.to_string()
50						} else {
51							Parameters.get(0).and_then(Value::as_str).unwrap_or("").to_string()
52						};
53						// Empty-path guard: matches the FileSystem.* contract so that
54						// the LooksLike404 classifier in MountainVinegRPCService
55						// downgrades the log level and uses error code -32004 rather
56						// than tripping the circuit breaker with a -32000.
57						if Path.is_empty() {
58							return Err(format!("{}: empty path (resource not found)", MethodNameOwned));
59						}
60						let PathBuf_ = std::path::PathBuf::from(StripFileUriScheme(&Path));
61						match MethodNameOwned.as_str() {
62							"stat" => {
63								fs_reader
64									.StatFile(&PathBuf_)
65									.await
66									.map(|S| serde_json::to_value(S).unwrap_or(Value::Null))
67									.map_err(|e| e.to_string())
68							},
69							"readFile" | "openDocument" => {
70								fs_reader
71									.ReadFile(&PathBuf_)
72									.await
73									.map(|Bytes| {
74										let Text = String::from_utf8(Bytes).unwrap_or_default();
75										json!({ "uri": Path, "text": Text })
76									})
77									.map_err(|e| e.to_string())
78							},
79							_ => Ok(Value::Null),
80						}
81					})
82				};
83
84			Some(Ok(Box::new(effect)))
85		},
86
87		_ => None,
88	}
89}