Mountain/ApplicationState/Internal/Persistence/MementoLoader/
LoadInitialMementoFromDisk.rs1#![allow(non_snake_case)]
2
3use std::{collections::HashMap, fs, path::Path};
9
10use serde_json::Value;
11
12use crate::{ApplicationState::Internal::Persistence::MementoLoader::AttemptMementoRecovery, dev_log};
13
14pub fn Fn(StorageFilePath:&Path) -> HashMap<String, Value> {
15 if !StorageFilePath.exists() {
16 dev_log!(
17 "storage",
18 "[MementoLoader] Memento file does not exist: {}",
19 StorageFilePath.display()
20 );
21
22 return HashMap::new();
23 }
24
25 match fs::read_to_string(StorageFilePath) {
26 Ok(Content) => {
27 serde_json::from_str(&Content).unwrap_or_else(|Error| {
28 dev_log!(
29 "storage",
30 "error: [MementoLoader] Failed to parse JSON from '{}': {}. Attempting recovery.",
31 StorageFilePath.display(),
32 Error
33 );
34 AttemptMementoRecovery::Fn(StorageFilePath, &Content);
35 HashMap::new()
36 })
37 },
38
39 Err(Error) => {
40 dev_log!(
41 "storage",
42 "error: [MementoLoader] Failed to read '{}': {}. Attempting recovery.",
43 StorageFilePath.display(),
44 Error
45 );
46
47 if let Some(Parent) = StorageFilePath.parent()
48 && !Parent.exists()
49 && let Err(DirError) = fs::create_dir_all(Parent)
50 {
51 dev_log!(
52 "storage",
53 "warn: [MementoLoader] Failed to create directory '{}': {}",
54 Parent.display(),
55 DirError
56 );
57 }
58
59 HashMap::new()
60 },
61 }
62}