use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::persistence::{self, StoreError};
use crate::time::now_millis;
pub const COMMUNITY_ORIGIN: &str = "community";
const FILE_NAME: &str = ".provenance.json";
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RuntimeProvenance {
pub origin: String,
pub installed_at: i64,
}
impl RuntimeProvenance {
pub fn new(origin: impl Into<String>) -> Self {
Self {
origin: origin.into(),
installed_at: now_millis(),
}
}
pub fn community() -> Self {
Self::new(COMMUNITY_ORIGIN)
}
pub fn is_community(&self) -> bool {
self.origin == COMMUNITY_ORIGIN
}
pub fn read(directory: &Path) -> Option<Self> {
let bytes = std::fs::read(directory.join(FILE_NAME)).ok()?;
serde_json::from_slice(&bytes).ok()
}
pub fn write(&self, directory: &Path) -> Result<(), StoreError> {
persistence::write_json_atomic(&directory.join(FILE_NAME), self)
}
}