use anyhow::{Context, Result};
use serde_json::Value;
use std::path::{Path, PathBuf};
pub fn python_module() -> Result<PathBuf> {
super::python_support::publish("state_store", include_str!("store.py"))
}
pub fn read_json(path: &Path) -> Result<Option<Value>> {
let metadata = match path.symlink_metadata() {
Ok(metadata) => metadata,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(error) => return Err(error.into()),
};
anyhow::ensure!(
metadata.is_file() && !metadata.file_type().is_symlink(),
"State must be a regular file"
);
Ok(Some(
serde_json::from_slice(&std::fs::read(path)?)
.context("State format is invalid; preserve the existing record")?,
))
}
#[cfg(test)]
mod tests;