Mountain/ApplicationState/Internal/Persistence/MementoLoader/
LoadInitialMementoFromDisk.rs1use std::{collections::HashMap, fs, path::Path};
7
8use serde_json::Value;
9
10use crate::{ApplicationState::Internal::Persistence::MementoLoader::AttemptMementoRecovery, dev_log};
11
12pub fn Fn(StorageFilePath:&Path) -> HashMap<String, Value> {
13 if !StorageFilePath.exists() {
14 dev_log!(
15 "storage",
16 "[MementoLoader] Memento file does not exist: {}",
17 StorageFilePath.display()
18 );
19
20 return HashMap::new();
21 }
22
23 match fs::read_to_string(StorageFilePath) {
24 Ok(Content) => {
25 serde_json::from_str(&Content).unwrap_or_else(|Error| {
26 dev_log!(
27 "storage",
28 "error: [MementoLoader] Failed to parse JSON from '{}': {}. Attempting recovery.",
29 StorageFilePath.display(),
30 Error
31 );
32
33 AttemptMementoRecovery::Fn(StorageFilePath, &Content);
34
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}