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