power_rules_daemon/
state.rs1use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5use anyhow::{Context, Result};
6use toml::{from_str, to_string};
7
8const STATE_FILE_PATH: &str = ".local/state/power-rules/state.toml";
9
10#[derive(Debug, Deserialize, Serialize, Default)]
11pub struct DaemonState {
12 pub paused_until: Option<i64>,
13}
14
15pub fn load_state(state_path: &PathBuf) -> Result<DaemonState> {
16 if !state_path.exists() {
17 return Ok(DaemonState::default());
18 }
19
20 let data = std::fs::read_to_string(state_path)
21 .context("Failed to read state file")?;
22 from_str(&data).context("Failed to parse state file")
23}
24
25pub fn save_state(state: &DaemonState, state_path: &PathBuf) -> Result<()> {
26 if let Some(parent) = state_path.parent() {
27 std::fs::create_dir_all(parent).context("Failed to create config directory")?;
28 }
29
30 let data = to_string(state).context("Failed to serialize state")?;
31 std::fs::write(state_path, data).context("Failed to write state file")?;
32 Ok(())
33}
34
35pub fn get_state_path() -> Result<PathBuf> {
36 Ok(dirs::home_dir()
37 .context("Could not find home directory")?
38 .join(STATE_FILE_PATH))
39}