Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/RunTime/Shutdown/
SaveApplicationState.rs

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