use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::Result;
use crate::Version;
use crate::engine::fs::Fs;
use crate::env::{EnvOp, EnvVar};
use crate::fs_path::AppDir;
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct VarSnapshot {
pub name: EnvVar,
pub op: EnvOp,
pub value: String,
pub old: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Receipt {
pub name: String,
pub version: Version,
pub app_dir: AppDir,
pub vars: Vec<VarSnapshot>,
}
impl Receipt {
#[inline]
pub fn to_toml(&self) -> Result<String> {
toml::to_string(self).map_err(|e| crate::Error::Manifest(e.to_string()))
}
#[inline]
pub fn from_toml(source: &str) -> Result<Self> {
toml::from_str(source).map_err(|e| crate::Error::Manifest(e.to_string()))
}
#[inline]
pub fn write(&self, fs: &dyn Fs, path: &Path) -> Result<()> {
fs.write(path, self.to_toml()?.as_bytes())
}
pub fn read(fs: &dyn Fs, path: &Path) -> Result<Option<Self>> {
if !fs.exists(path)? {
return Ok(None);
}
let bytes = fs.read(path)?;
Ok(Some(Self::from_toml(&String::from_utf8_lossy(&bytes))?))
}
}
#[cfg(test)]
mod tests;