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 AttemptMementoRecovery::Fn(StorageFilePath, &Content);
33 HashMap::new()
34 })
35 },
36
37 Err(Error) => {
38 dev_log!(
39 "storage",
40 "error: [MementoLoader] Failed to read '{}': {}. Attempting recovery.",
41 StorageFilePath.display(),
42 Error
43 );
44
45 if let Some(Parent) = StorageFilePath.parent()
46 && !Parent.exists()
47 && let Err(DirError) = fs::create_dir_all(Parent)
48 {
49 dev_log!(
50 "storage",
51 "warn: [MementoLoader] Failed to create directory '{}': {}",
52 Parent.display(),
53 DirError
54 );
55 }
56
57 HashMap::new()
58 },
59 }
60}