consciousness/jobkit-daemon/src/status.rs

30 lines
863 B
Rust
Raw Normal View History

// Status file management
//
// Writes a JSON status snapshot to data_dir/daemon-status.json.
// Applications provide their own status struct (must impl Serialize).
use std::fs;
use std::path::Path;
fn status_path(data_dir: &Path) -> std::path::PathBuf {
data_dir.join("daemon-status.json")
}
/// Write a status snapshot to the status file.
pub fn write<S: serde::Serialize>(data_dir: &Path, status: &S) {
if let Ok(json) = serde_json::to_string_pretty(status) {
let _ = fs::write(status_path(data_dir), json);
}
}
/// Read the status file as a string.
pub fn read(data_dir: &Path) -> Option<String> {
fs::read_to_string(status_path(data_dir)).ok()
}
/// Read and deserialize the status file.
pub fn load<S: serde::de::DeserializeOwned>(data_dir: &Path) -> Option<S> {
let s = read(data_dir)?;
serde_json::from_str(&s).ok()
}