//! Project and version metadata records. //! //! Builds `com.nahdig.release.projectMeta` records and the `metadata`/`target` //! fragments embedded in `software.release.manifest` artifacts. Nothing here //! performs I/O; callers write the records and index the resulting links. //! //! Same-DID and immutability preflight helpers live here so ingestion can //! reject cross-repository references and overwrites of immutable //! manifest/version records before a PDS write. use anyhow::{Result, bail}; use serde_json::{Map, Value, json}; use crate::constants::{MANIFEST_COLLECTION, PROJECT_COLLECTION, VERSION_COLLECTION}; /// Collection for project metadata records; the record key is the project slug. pub const PROJECT_META_COLLECTION: &str = "com.nahdig.release.projectMeta"; /// Project-level metadata. Every field is optional; absent fields are omitted /// from the record rather than written empty. #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct ProjectMeta { pub description: Option, pub homepage: Option, pub license: Option, pub repository: Option, pub versioning_scheme: Option, } /// Pinned Git source facts for an artifact, matching the /// `com.nahdig.release.gitMeta` object. #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct GitMeta { pub repository: String, pub revision: Option, pub commit: Option, } /// Coarse runtime target, matching `software.release.defs#target`. #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct Target { pub os: Option, pub architectures: Vec, } /// Everything known about one published version. #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct VersionMeta { pub published_at: Option, pub raw_version: String, pub target: Option, pub git: Option, pub changelog_uri: Option, } fn project_uri(repo_did: &str, slug: &str) -> String { format!("at://{repo_did}/{PROJECT_COLLECTION}/{slug}") } fn insert_optional(object: &mut Map, key: &str, value: Option<&String>) { if let Some(value) = value.filter(|value| !value.is_empty()) { object.insert(key.to_owned(), Value::String(value.clone())); } } /// Build a `com.nahdig.release.projectMeta` record keyed by the project slug. /// /// `project` is the same-DID `software.release.project` AT-URI and `name` is /// the slug, which is also the rkey. pub fn project_meta_record(repo_did: &str, slug: &str, meta: &ProjectMeta) -> Value { let mut record = Map::new(); record.insert("$type".to_owned(), json!(PROJECT_META_COLLECTION)); record.insert("project".to_owned(), json!(project_uri(repo_did, slug))); record.insert("name".to_owned(), json!(slug)); insert_optional(&mut record, "description", meta.description.as_ref()); insert_optional(&mut record, "homepage", meta.homepage.as_ref()); insert_optional(&mut record, "license", meta.license.as_ref()); insert_optional(&mut record, "repository", meta.repository.as_ref()); insert_optional( &mut record, "versioningScheme", meta.versioning_scheme.as_ref(), ); Value::Object(record) } /// Git metadata fragment for `httpArtifact.metadata`/`blobArtifact.metadata`. /// /// The fragment carries no `$type`: the artifact metadata union is open, so /// the embedding lexicon does not require one. pub fn git_metadata_json(git: &GitMeta) -> Value { let mut fragment = Map::new(); fragment.insert("repository".to_owned(), json!(git.repository)); insert_optional(&mut fragment, "revision", git.revision.as_ref()); insert_optional(&mut fragment, "commit", git.commit.as_ref()); Value::Object(fragment) } fn target_json(target: &Target) -> Value { let mut value = Map::new(); insert_optional(&mut value, "os", target.os.as_ref()); if !target.architectures.is_empty() { value.insert("architectures".to_owned(), json!(target.architectures)); } Value::Object(value) } /// Attach optional git metadata and runtime target to manifest artifacts. /// /// Input fragments (`software.release.defs#httpArtifact` or `#blobArtifact`) /// are cloned with their `$type` and core fields intact. A git fragment is /// appended to any existing `metadata` array. pub fn build_manifest_artifacts( artifacts: &[Value], git: Option<&GitMeta>, target: Option<&Target>, ) -> Vec { artifacts .iter() .map(|artifact| { let mut artifact = artifact.clone(); if !artifact.is_object() { return artifact; } if let Some(git) = git { let fragment = git_metadata_json(git); match artifact.get_mut("metadata") { Some(Value::Array(items)) => items.push(fragment), _ => { artifact["metadata"] = Value::Array(vec![fragment]); } } } if let Some(target) = target { artifact["target"] = target_json(target); } artifact }) .collect() } /// Expose version facts for callers building `com.nahdig.cve.atprotoV1` /// `versionLabels` entries. This never builds or edits a CVE record. pub fn version_meta_json(meta: &VersionMeta) -> Value { let mut value = Map::new(); if !meta.raw_version.is_empty() { value.insert("versionLabels".to_owned(), json!([meta.raw_version])); } insert_optional(&mut value, "publishedAt", meta.published_at.as_ref()); insert_optional(&mut value, "changelog", meta.changelog_uri.as_ref()); if let Some(target) = &meta.target { value.insert("target".to_owned(), target_json(target)); } if let Some(git) = &meta.git { value.insert("git".to_owned(), git_metadata_json(git)); } Value::Object(value) } /// Reject any AT-URI that does not belong to `repo_did`. pub fn assert_same_did(repo_did: &str, uri: &str) -> Result<()> { let Some(rest) = uri.strip_prefix("at://") else { bail!("not an at-uri: {uri:?}"); }; let Some((did, _)) = rest.split_once('/') else { bail!("at-uri has no collection: {uri:?}"); }; if did != repo_did { bail!("cross-DID reference {uri:?}: expected {repo_did}, got {did}"); } Ok(()) } /// Manifest and version records are write-once. pub fn is_immutable(collection: &str) -> bool { matches!(collection, MANIFEST_COLLECTION | VERSION_COLLECTION) } /// Preflight a put: refuse to overwrite an existing immutable record. pub fn assert_put_allowed(collection: &str, exists: bool) -> Result<()> { if exists && is_immutable(collection) { bail!("{collection} is immutable: refusing to overwrite an existing record"); } Ok(()) }