use crate::utils::error::Result;
use serde::{de::DeserializeOwned, Serialize};
use std::path::Path;
pub trait LockEntry: Serialize + DeserializeOwned + Clone + Send + Sync + 'static {
fn id(&self) -> &str;
fn version(&self) -> &str;
fn integrity(&self) -> Option<&str>;
fn dependencies(&self) -> &[String];
}
pub trait Lockfile: Serialize + DeserializeOwned + Clone + Send + Sync + 'static {
type Entry: LockEntry;
fn schema_version(&self) -> u32;
fn generated_at(&self) -> &str;
fn entries(&self) -> Box<dyn Iterator<Item = (&str, &Self::Entry)> + '_>;
fn get(&self, id: &str) -> Option<&Self::Entry>;
fn upsert(&mut self, id: String, entry: Self::Entry);
fn remove(&mut self, id: &str) -> Option<Self::Entry>;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
}
pub trait LockfileFormat: Clone + Send + Sync + 'static {
const EXTENSION: &'static str;
const MIME_TYPE: &'static str;
fn serialize<L: Lockfile>(lockfile: &L) -> Result<String>;
fn deserialize<L: Lockfile>(content: &str) -> Result<L>;
}
pub trait LockfileManager: Send + Sync {
type Lockfile: Lockfile;
type Format: LockfileFormat;
fn lockfile_path(&self) -> &Path;
fn load(&self) -> Result<Option<Self::Lockfile>>;
fn save(&self, lockfile: &Self::Lockfile) -> Result<()>;
fn create(&self) -> Self::Lockfile;
fn load_or_create(&self) -> Result<Self::Lockfile> {
match self.load()? {
Some(lockfile) => Ok(lockfile),
None => Ok(self.create()),
}
}
}
pub trait PqcSignable: LockEntry {
fn pqc_signature(&self) -> Option<&str>;
fn pqc_pubkey(&self) -> Option<&str>;
fn pqc_algorithm(&self) -> Option<&str>;
fn set_pqc(
&mut self, algorithm: Option<String>, signature: Option<String>, pubkey: Option<String>,
);
}
pub trait CachingManager: LockfileManager {
fn resolve_deps_cached(&self, id: &str, version: &str) -> Result<Vec<String>>;
fn clear_cache(&self);
fn cache_stats(&self) -> super::cache::CacheStats;
}
pub trait Validatable {
fn validate(&self) -> Result<()>;
fn check_circular_deps(&self) -> Result<()>;
fn verify_integrity(&self) -> Result<()>;
}
#[cfg(test)]
mod tests {
use super::*;
fn _assert_validatable_object_safe(_: &dyn Validatable) {}
}