daemon: Start bringing up contexts and state loading/saving

This commit is contained in:
Vicki Pfau 2024-05-24 17:06:43 -07:00 committed by Jeremy Whiting
parent 23267c65e0
commit c6113ee739
6 changed files with 275 additions and 19 deletions

View file

@ -5,8 +5,40 @@
* SPDX-License-Identifier: MIT
*/
use anyhow::Result;
use anyhow::{anyhow, Result};
use std::io::ErrorKind;
use tokio::fs::{create_dir_all, read_to_string, write};
use tracing::{error, info};
pub(in crate::daemon) async fn read_config() -> Result<()> {
use crate::daemon::DaemonContext;
pub(in crate::daemon) async fn read_state<C: DaemonContext>(context: &C) -> Result<C::State> {
let path = context.state_path()?;
let state = match read_to_string(path).await {
Ok(state) => state,
Err(e) => {
if e.kind() == ErrorKind::NotFound {
info!("No state file found, reloading default state");
return Ok(C::State::default());
}
error!("Error loading state: {e}");
return Err(e.into());
}
};
Ok(toml::from_str(state.as_str())?)
}
pub(in crate::daemon) async fn write_state<C: DaemonContext>(context: &C) -> Result<()> {
let path = context.state_path()?;
create_dir_all(path.parent().ok_or(anyhow!(
"Context path {} has no parent dir",
path.to_string_lossy()
))?)
.await?;
let state = toml::to_string_pretty(&context.state())?;
Ok(write(path, state.as_bytes()).await?)
}
pub(in crate::daemon) async fn read_config<C: DaemonContext>(_context: &C) -> Result<C::Config> {
todo!();
}