use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::apply::write_atomic;
use crate::error::{CoreError, ErrorCode};
use crate::fs::Fs;
use crate::layering::manifest::{Project, same_path};
pub const TRUST_FILE: &str = "trust.json";
pub(crate) const TRUST_SCHEMA: u32 = 1;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrustState {
Trusted,
Untrusted,
Changed,
}
impl TrustState {
pub const fn as_str(self) -> &'static str {
match self {
TrustState::Trusted => "trusted",
TrustState::Untrusted => "untrusted",
TrustState::Changed => "changed",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TrustRecord {
pub id: String,
pub path: String,
pub hash: String,
#[serde(flatten)]
pub extra: serde_json::Map<String, serde_json::Value>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TrustStore {
pub schema: u32,
#[serde(default)]
pub projects: Vec<TrustRecord>,
#[serde(flatten)]
pub extra: serde_json::Map<String, serde_json::Value>,
}
impl Default for TrustStore {
fn default() -> Self {
Self {
schema: TRUST_SCHEMA,
projects: Vec::new(),
extra: serde_json::Map::new(),
}
}
}
impl TrustStore {
pub fn load(fs: &dyn Fs, state_dir: &Path) -> Result<Self, CoreError> {
let path = state_dir.join(TRUST_FILE);
if !fs.exists(&path) {
return Ok(Self::default());
}
let text = fs.read_to_string(&path).map_err(|err| {
CoreError::new(
ErrorCode::IoError,
format!("cannot read the trust file: {err}"),
)
.with_path(&path)
})?;
let store: Self = serde_json::from_str(&text).map_err(|err| {
CoreError::new(
ErrorCode::ParseError,
format!("the trust file does not parse: {err}"),
)
.with_path(&path)
})?;
if store.schema != TRUST_SCHEMA {
return Err(CoreError::new(
ErrorCode::UnsupportedSchema,
format!(
"trust file schema {} is not supported; this build reads schema {TRUST_SCHEMA}",
store.schema
),
)
.with_path(&path));
}
Ok(store)
}
pub fn save(&self, fs: &dyn Fs, state_dir: &Path) -> Result<(), CoreError> {
let mut text = serde_json::to_string_pretty(self).map_err(|err| {
CoreError::new(
ErrorCode::InvalidValue,
format!("cannot serialize the trust file: {err}"),
)
})?;
text.push('\n');
write_atomic(fs, &state_dir.join(TRUST_FILE), text.as_bytes())
}
pub fn state(&self, project: &Project) -> TrustState {
match self.record(&project.root) {
None => TrustState::Untrusted,
Some(record) if record.hash == project.hash() => TrustState::Trusted,
Some(_) => TrustState::Changed,
}
}
pub fn knows(&self, project: &Project) -> bool {
self.record(&project.root)
.is_some_and(|record| record.id == project.id)
}
pub fn trust(&mut self, project: &Project) {
let hash = project.hash();
match self
.projects
.iter_mut()
.find(|known| same_path(Path::new(&known.path), &project.root))
{
Some(slot) => {
slot.id.clone_from(&project.id);
slot.path = project.root.to_string_lossy().into_owned();
slot.hash = hash;
}
None => self.projects.push(TrustRecord {
id: project.id.clone(),
path: project.root.to_string_lossy().into_owned(),
hash,
extra: serde_json::Map::new(),
}),
}
}
pub fn forget(&mut self, id: &str) -> bool {
let before = self.projects.len();
self.projects.retain(|record| record.id != id);
self.projects.len() != before
}
fn record(&self, root: &Path) -> Option<&TrustRecord> {
self.projects
.iter()
.find(|record| same_path(Path::new(&record.path), root))
}
}