Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceHandlers/Utilities/
RecentlyOpened.rs

1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3//! Recently-opened workspaces/files persistence.
4//! File lives at `~/.fiddee/workspaces/RecentlyOpened.json` (resolved
5//! through the `FiddeeRoot` atom). Parse failures degrade to an empty
6//! `{workspaces, files}` envelope so the UI never sees a missing field.
7
8use serde_json::{Value, json};
9
10use crate::IPC::WindServiceHandlers::Utilities::FiddeeRoot::FiddeeRoot;
11
12pub fn RecentlyOpenedPath() -> std::path::PathBuf { FiddeeRoot().join("workspaces").join("RecentlyOpened.json") }
13
14pub fn ReadRecentlyOpened() -> Result<Value, String> {
15	let Path = RecentlyOpenedPath();
16
17	match std::fs::read_to_string(&Path) {
18		Ok(Contents) => {
19			match serde_json::from_str::<Value>(&Contents) {
20				Ok(Parsed) => Ok(Parsed),
21
22				Err(_) => Ok(json!({ "workspaces": [], "files": [] })),
23			}
24		},
25
26		Err(_) => Ok(json!({ "workspaces": [], "files": [] })),
27	}
28}
29
30pub fn MutateRecentlyOpened<F:FnOnce(&mut serde_json::Map<String, Value>)>(Apply:F) {
31	let Path = RecentlyOpenedPath();
32
33	let mut Parsed:serde_json::Map<String, Value> = std::fs::read_to_string(&Path)
34		.ok()
35		.and_then(|Contents| serde_json::from_str::<Value>(&Contents).ok())
36		.and_then(|V| V.as_object().cloned())
37		.unwrap_or_default();
38
39	if !Parsed.contains_key("workspaces") {
40		Parsed.insert("workspaces".into(), json!([]));
41	}
42
43	if !Parsed.contains_key("files") {
44		Parsed.insert("files".into(), json!([]));
45	}
46
47	Apply(&mut Parsed);
48
49	if let Some(Parent) = Path.parent() {
50		let _ = std::fs::create_dir_all(Parent);
51	}
52
53	if let Ok(Serialised) = serde_json::to_vec_pretty(&Value::Object(Parsed)) {
54		let _ = std::fs::write(&Path, Serialised);
55	}
56}