//! Row and mark types shared by the cache actor and its callers. use serde::Serialize; use serde_json::Value; /// One cache row selected for publishing. /// /// `data` is the exact JSON text stored in `cves.data` (compact ASCII-escaped /// JSON); use [`CveRow::value`] when the parsed document is needed. #[derive(Debug, Clone, PartialEq, Eq)] pub struct CveRow { pub id: String, pub data: String, } impl CveRow { pub fn new(id: impl Into, data: Value) -> Self { Self { id: id.into(), data: cache_json(&data), } } pub fn value(&self) -> anyhow::Result { Ok(serde_json::from_str(&self.data)?) } } #[derive(Debug, Clone, Default)] pub struct PublicationMark { pub repo_did: String, pub cve_id: String, pub status: String, pub uri: Option, pub cid: Option, pub blob_cid: Option, pub blob_bytes: Option, pub record_bytes: Option, pub mode: Option, } impl PublicationMark { pub fn new( repo_did: impl Into, cve_id: impl Into, status: impl Into, ) -> Self { Self { repo_did: repo_did.into(), cve_id: cve_id.into(), status: status.into(), ..Self::default() } } } #[derive(Debug, Clone, Default, Serialize)] pub struct SoftwareRow { pub repo_did: String, pub slug: String, pub part: Option, pub vendor: Option, pub product: Option, pub project_uri: Option, pub project_cid: Option, pub software_uri: Option, pub software_cid: Option, pub status: String, pub updated_at: Option, } #[derive(Debug, Clone, Default)] pub struct SoftwareMark { pub repo_did: String, pub slug: String, pub part: String, pub vendor: String, pub product: String, pub project_uri: String, pub project_cid: String, pub software_uri: String, pub software_cid: String, } #[derive(Debug, Clone, Default, Serialize)] pub struct PurlRow { pub repo_did: String, pub purl: String, pub software_slug: String, pub purl_uri: Option, pub purl_cid: Option, pub status: String, pub updated_at: Option, } #[derive(Debug, Clone, Copy, Default, Serialize)] pub struct Counts { pub pending: i64, pub present: i64, pub created: i64, pub skipped: i64, pub total: i64, } /// One outgoing link to index (`path` in the source record -> strongRef). #[derive(Debug, Clone, PartialEq, Eq)] pub struct Link { pub path: String, pub subject_uri: String, pub subject_cid: Option, } impl Link { pub fn new(path: impl Into, subject_uri: impl Into) -> Self { Self { path: path.into(), subject_uri: subject_uri.into(), subject_cid: None, } } } #[derive(Debug, Clone, Default, Serialize)] pub struct LinkRow { pub repo_did: String, pub src_uri: String, pub src_collection: String, pub src_rkey: String, pub src_cid: Option, pub path: String, pub subject_uri: String, pub subject_cid: Option, pub indexed_at: String, } #[derive(Debug, Clone, Default, Serialize)] pub struct ReleaseCandidate { pub slug: String, pub vendor: Option, pub product: Option, pub project_uri: Option, pub project_cid: Option, pub purls: Vec, pub latest_version: Option, pub release_updated_at: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct ReleaseProjectRow { pub repo_did: String, pub slug: String, pub purl: String, pub system: String, pub latest_version: Option, pub latest_version_raw: Option, pub project_uri: Option, pub project_cid: Option, pub manifest_uri: Option, pub manifest_cid: Option, pub version_uri: Option, pub version_cid: Option, pub channel_uri: Option, pub channel_cid: Option, pub status: String, pub updated_at: String, } #[derive(Debug, Clone, Default)] pub struct ReleaseProjectMark { pub repo_did: String, pub slug: String, pub purl: String, pub system: String, pub latest_version: String, pub latest_version_raw: String, pub project_uri: String, pub project_cid: String, pub manifest_uri: String, pub manifest_cid: String, pub version_uri: String, pub version_cid: String, pub channel_uri: String, pub channel_cid: String, } #[derive(Debug, Clone, Default, Serialize)] pub struct ReleaseVersionRow { pub repo_did: String, pub project_slug: String, pub version: String, pub version_raw: String, pub rkey: String, pub uri: Option, pub cid: Option, pub manifest_uri: Option, pub manifest_cid: Option, pub status: String, pub updated_at: String, } #[derive(Debug, Clone, Default)] pub struct ReleaseVersionMark { pub repo_did: String, pub project_slug: String, pub version: String, pub version_raw: String, pub rkey: String, pub uri: String, pub cid: String, pub manifest_uri: String, pub manifest_cid: String, } #[derive(Debug, Clone, Default, Serialize)] pub struct ReleaseChannelRow { pub repo_did: String, pub project_slug: String, pub name: String, pub rkey: String, pub uri: Option, pub cid: Option, pub version_uri: Option, pub version_cid: Option, pub status: String, pub updated_at: String, } #[derive(Debug, Clone, Default)] pub struct ReleaseChannelMark { pub repo_did: String, pub project_slug: String, pub name: String, pub rkey: String, pub uri: String, pub cid: String, pub version_uri: String, pub version_cid: String, } #[derive(Debug, Clone, Default, Serialize)] pub struct ReleaseStatusRow { pub repo_did: String, pub subject_uri: String, pub status: String, pub rkey: String, pub uri: Option, pub cid: Option, pub updated_at: String, } #[derive(Debug, Clone, Default)] pub struct ReleaseStatusMark { pub repo_did: String, pub subject_uri: String, pub status: String, pub rkey: String, pub uri: String, pub cid: String, } #[derive(Debug, Clone, Default, Serialize)] pub struct SoftwareIdentityRow { pub repo_did: String, pub raw_slug: String, pub canonical_slug: String, pub identity_key: Option, pub source: Option, pub confidence: Option, pub updated_at: String, } #[derive(Debug, Clone, Default)] pub struct SoftwareIdentityMark { pub repo_did: String, pub raw_slug: String, pub canonical_slug: String, pub identity_key: Option, pub source: Option, pub confidence: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct ProjectAliasRow { pub repo_did: String, pub alias_key: String, pub canonical_slug: String, pub source: Option, pub updated_at: String, } #[derive(Debug, Clone, Default)] pub struct ProjectAliasMark { pub repo_did: String, pub alias_key: String, pub canonical_slug: String, pub source: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct ProjectChannelRow { pub repo_did: String, pub project_slug: String, pub channel: String, pub latest_version: Option, pub eol_at: Option, pub status: String, pub updated_at: String, } #[derive(Debug, Clone, Default)] pub struct ProjectChannelMark { pub repo_did: String, pub project_slug: String, pub channel: String, pub latest_version: Option, pub eol_at: Option, pub status: String, } #[derive(Debug, Clone, Default, Serialize)] pub struct ReleaseArtifactRow { pub repo_did: String, pub source: String, pub package: String, pub version: String, pub file_name: Option, pub url: String, pub sha256: Option, pub cid: Option, pub size: Option, pub mime_type: Option, pub metadata_json: Option, pub fetched_at: String, } #[derive(Debug, Clone, Default)] pub struct ReleaseArtifactMark { pub repo_did: String, pub source: String, pub package: String, pub version: String, pub file_name: Option, pub url: String, pub sha256: Option, pub cid: Option, pub size: Option, pub mime_type: Option, pub metadata_json: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct ReleaseManifestRow { pub repo_did: String, pub project_slug: String, pub manifest_key: String, pub uri: Option, pub cid: Option, pub artifacts_json: Option, pub created_at: String, } #[derive(Debug, Clone, Default)] pub struct ReleaseManifestMark { pub repo_did: String, pub project_slug: String, pub manifest_key: String, pub uri: Option, pub cid: Option, pub artifacts_json: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct ReleaseHistoryJobRow { pub repo_did: String, pub project_slug: String, pub version_raw: String, pub version: Option, pub source: Option, pub purl: Option, pub status: String, pub attempts: i64, pub error: Option, pub updated_at: String, } #[derive(Debug, Clone, Default)] pub struct ReleaseHistoryJobMark { pub repo_did: String, pub project_slug: String, pub version_raw: String, pub version: Option, pub source: Option, pub purl: Option, pub status: String, pub attempts: i64, pub error: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct ReleaseOsvRow { pub repo_did: String, pub purl: String, pub osv_id: String, pub aliases_json: Option, pub affected_json: Option, pub modified: Option, pub fetched_at: String, } #[derive(Debug, Clone, Default)] pub struct ReleaseOsvMark { pub repo_did: String, pub purl: String, pub osv_id: String, pub aliases_json: Option, pub affected_json: Option, pub modified: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct ReleaseCveLinkRow { pub repo_did: String, pub cve_id: String, pub project_slug: String, pub version_uri: Option, pub version_cid: Option, pub channel_uri: Option, pub channel_cid: Option, pub manifest_uri: Option, pub manifest_cid: Option, pub version_label: Option, pub affected_ranges: Option, pub updated_at: String, } #[derive(Debug, Clone, Default)] pub struct ReleaseCveLinkMark { pub repo_did: String, pub cve_id: String, pub project_slug: String, pub version_uri: Option, pub version_cid: Option, pub channel_uri: Option, pub channel_cid: Option, pub manifest_uri: Option, pub manifest_cid: Option, pub version_label: Option, pub affected_ranges: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct ReleaseSourceStateRow { pub repo_did: String, pub source: String, pub cursor: Option, pub etag: Option, pub updated_at: String, } #[derive(Debug, Clone, Default)] pub struct ReleaseSourceStateMark { pub repo_did: String, pub source: String, pub cursor: Option, pub etag: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct ReleaseStatusEventRow { pub repo_did: String, pub subject_uri: String, pub rkey: String, pub status: String, pub kind: Option, pub reason: Option, pub cid: Option, pub created_at: String, } #[derive(Debug, Clone, Default)] pub struct ReleaseStatusEventMark { pub repo_did: String, pub subject_uri: String, pub rkey: String, pub status: String, pub kind: Option, pub reason: Option, pub cid: Option, pub created_at: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct ReleaseProjectStateRow { pub repo_did: String, pub slug: String, pub canonical_purl: Option, pub source: Option, pub raw_versions_json: Option, pub checked_at: Option, pub unsupported_reason: Option, } #[derive(Debug, Clone, Default)] pub struct ReleaseProjectStateMark { pub repo_did: String, pub slug: String, pub canonical_purl: Option, pub source: Option, pub raw_versions_json: Option, pub checked_at: Option, pub unsupported_reason: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct ReleaseVersionMetaRow { pub repo_did: String, pub project_slug: String, pub version: String, pub source: Option, pub published_at: Option, pub raw_version: Option, pub artifact_cid: Option, pub artifact_size: Option, } #[derive(Debug, Clone, Default)] pub struct ReleaseVersionMetaMark { pub repo_did: String, pub project_slug: String, pub version: String, pub source: Option, pub published_at: Option, pub raw_version: Option, pub artifact_cid: Option, pub artifact_size: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct IdentityMigrationRow { pub repo_did: String, pub phase: String, pub raw_slug: String, pub canonical_slug: Option, pub action: Option, pub status: String, pub attempts: i64, pub error: Option, pub updated_at: String, } #[derive(Debug, Clone, Default)] pub struct IdentityMigrationMark { pub repo_did: String, pub phase: String, pub raw_slug: String, pub canonical_slug: Option, pub action: Option, pub status: String, pub attempts: i64, pub error: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct LinkRepairRow { pub repo_did: String, pub src_uri: String, pub src_cid: Option, pub collection: Option, pub record_json: String, pub attempts: i64, pub last_error: Option, pub next_retry_at: Option, } #[derive(Debug, Clone, Default)] pub struct LinkRepairMark { pub repo_did: String, pub src_uri: String, pub src_cid: Option, pub collection: Option, pub record_json: String, pub attempts: i64, pub last_error: Option, pub next_retry_at: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct PublishErrorRow { pub repo_did: String, pub cve_id: String, pub attempts: i64, pub last_error: Option, pub next_retry_at: Option, } #[derive(Debug, Clone, Default)] pub struct PublishErrorMark { pub repo_did: String, pub cve_id: String, pub attempts: i64, pub last_error: Option, pub next_retry_at: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct SourceHostStateRow { pub host: String, pub window_start: f64, pub requests: i64, pub blocked_until: Option, } #[derive(Debug, Clone, Default)] pub struct SourceHostStateMark { pub host: String, pub window_start: f64, pub requests: i64, pub blocked_until: Option, } #[derive(Debug, Clone, Default, Serialize)] pub struct RateBudget { pub repo_did: String, pub hour_start: f64, pub hour_points: f64, pub day_start: f64, pub day_points: f64, pub updated_at: String, } /// Compact JSON with non-ASCII escaped as `\uXXXX`. /// /// The cache stores rows this way so `data.chars().count()` equals the stored /// byte length; `prepare_row` uses that count for its size branch. pub fn cache_json(value: &Value) -> String { let mut out = String::new(); write_cache_json(value, &mut out); out } fn write_cache_json(value: &Value, out: &mut String) { match value { Value::Null => out.push_str("null"), Value::Bool(true) => out.push_str("true"), Value::Bool(false) => out.push_str("false"), Value::Number(number) => { if let Some(value) = number.as_i64() { out.push_str(&value.to_string()); } else if let Some(value) = number.as_u64() { out.push_str(&value.to_string()); } else if let Some(value) = number.as_f64() { write_json_float(value, out); } else { out.push_str(&number.to_string()); } } Value::String(value) => write_json_string(value, out), Value::Array(items) => { out.push('['); for (index, item) in items.iter().enumerate() { if index > 0 { out.push(','); } write_cache_json(item, out); } out.push(']'); } Value::Object(map) => { out.push('{'); for (index, (key, item)) in map.iter().enumerate() { if index > 0 { out.push(','); } write_json_string(key, out); out.push(':'); write_cache_json(item, out); } out.push('}'); } } } fn write_json_string(value: &str, out: &mut String) { out.push('"'); for character in value.chars() { match character { '"' => out.push_str("\\\""), '\\' => out.push_str("\\\\"), '\u{08}' => out.push_str("\\b"), '\u{0c}' => out.push_str("\\f"), '\n' => out.push_str("\\n"), '\r' => out.push_str("\\r"), '\t' => out.push_str("\\t"), character if (' '..='~').contains(&character) => out.push(character), character => { let code = character as u32; if code <= 0xFFFF { write_u_escape(code, out); } else { let adjusted = code - 0x1_0000; write_u_escape(0xD800 + (adjusted >> 10), out); write_u_escape(0xDC00 + (adjusted & 0x3FF), out); } } } } out.push('"'); } fn write_u_escape(code: u32, out: &mut String) { out.push_str(&format!("\\u{code:04x}")); } fn write_json_float(value: f64, out: &mut String) { if value.is_nan() { out.push_str("NaN"); return; } if value.is_infinite() { out.push_str(if value > 0.0 { "Infinity" } else { "-Infinity" }); return; } let rendered = format!("{value:?}"); if let Some(index) = rendered.find(['e', 'E']) { let (mantissa, exponent) = rendered.split_at(index); let exponent = &exponent[1..]; let (sign, digits) = match exponent.strip_prefix('-') { Some(rest) => ('-', rest), None => ('+', exponent.strip_prefix('+').unwrap_or(exponent)), }; out.push_str(mantissa); out.push('e'); out.push(sign); if digits.len() < 2 { out.push('0'); } out.push_str(digits); } else { out.push_str(&rendered); } } #[cfg(test)] mod tests { use super::*; use serde_json::json; #[test] fn cache_json_ascii_escapes_non_ascii() { let bs = char::from_u32(0x5c).expect("backslash"); assert_eq!( cache_json(&json!({"a": "é", "b": 1.0, "c": [true, null]})), format!("{{\"a\":\"{bs}u00e9\",\"b\":1.0,\"c\":[true,null]}}") ); assert_eq!( cache_json(&json!({"emoji": "😀", "del": "\u{7f}"})), format!("{{\"emoji\":\"{bs}ud83d{bs}ude00\",\"del\":\"{bs}u007f\"}}") ); assert_eq!(cache_json(&json!({"n": 1e-7})), r#"{"n":1e-07}"#); } }