Skip to main content

Mountain/RunTime/Shutdown/
SaveApplicationState.rs

1#![allow(non_snake_case)]
2
3//! Persist the global memento to disk before the runtime tears down. Creates
4//! the parent directory if missing.
5
6use CommonLibrary::Error::CommonError::CommonError;
7
8use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
9
10impl ApplicationRunTime {
11	pub async fn SaveApplicationState(&self) -> Result<(), CommonError> {
12		dev_log!("lifecycle", "[ApplicationRunTime] Saving application state...");
13
14		let GlobalMementoGuard = self
15			.Environment
16			.ApplicationState
17			.Configuration
18			.MementoGlobalStorage
19			.lock()
20			.map_err(|E| CommonError::StateLockPoisoned { Context:E.to_string() })?;
21
22		let GlobalMementoPath = self
23			.Environment
24			.ApplicationState
25			.GlobalMementoPath
26			.lock()
27			.map_err(|E| CommonError::StateLockPoisoned { Context:E.to_string() })?
28			.clone();
29
30		if let Some(Parent) = GlobalMementoPath.parent() {
31			if !Parent.exists() {
32				std::fs::create_dir_all(Parent)
33					.map_err(|E| CommonError::FileSystemIO { Path:Parent.to_path_buf(), Description:E.to_string() })?;
34			}
35		}
36
37		let MementoJSON = serde_json::to_string_pretty(&*GlobalMementoGuard)
38			.map_err(|E| CommonError::SerializationError { Description:E.to_string() })?;
39
40		std::fs::write(&GlobalMementoPath, MementoJSON)
41			.map_err(|E| CommonError::FileSystemIO { Path:GlobalMementoPath.clone(), Description:E.to_string() })
42	}
43}