kernel/manifests/
provenance.rs1use std::path::Path;
6
7use serde::{Deserialize, Serialize};
8
9use crate::persistence::{self, StoreError};
10use crate::time::now_millis;
11
12pub const COMMUNITY_ORIGIN: &str = "community";
14const FILE_NAME: &str = ".provenance.json";
15
16#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
18pub struct RuntimeProvenance {
19 pub origin: String,
20 pub installed_at: i64,
22}
23
24impl RuntimeProvenance {
25 pub fn new(origin: impl Into<String>) -> Self {
27 Self {
28 origin: origin.into(),
29 installed_at: now_millis(),
30 }
31 }
32
33 pub fn community() -> Self {
35 Self::new(COMMUNITY_ORIGIN)
36 }
37
38 pub fn is_community(&self) -> bool {
40 self.origin == COMMUNITY_ORIGIN
41 }
42
43 pub fn read(directory: &Path) -> Option<Self> {
47 let bytes = std::fs::read(directory.join(FILE_NAME)).ok()?;
48 serde_json::from_slice(&bytes).ok()
49 }
50
51 pub fn write(&self, directory: &Path) -> Result<(), StoreError> {
53 persistence::write_json_atomic(&directory.join(FILE_NAME), self)
54 }
55}