ifc_lite_processing/
product_geometry_audit.rs

1// SPDX-License-Identifier: MPL-2.0
2//! Opt-in, request-local refusal receipts from the canonical producer (#7711).
3use std::{collections::BTreeMap, sync::{Arc, Mutex}};
4use crate::element::ProducedElementMeshes;
5
6/// Per-product processing receipts for consumers that cannot accept partial geometry.
7/// No entry means NOT proven: skipped/undecodable jobs never become a clean receipt.
8/// `Ok` certifies only the diagnostic checks below, not source referential integrity
9/// or shared-cache provenance. Strict consumers must check those independently.
10#[derive(Clone, Default)]
11pub struct ProductGeometryAudit {
12    receipts: Arc<Mutex<BTreeMap<u32, Result<(), String>>>>,
13}
14
15impl ProductGeometryAudit {
16    /// Require a completed job with no recorded item drop, input refusal, CSG
17    /// diagnostic (including fallbacks), empty output or degenerate backstop drop.
18    /// This intentionally refuses informational CSG records too: absence of loss
19    /// must be proved before a strict consumer can exempt a diagnostic reason.
20    pub fn require_complete(&self, product_id: u32) -> Result<(), String> {
21        self.receipts.lock().unwrap_or_else(|e| e.into_inner()).get(&product_id)
22            .cloned().unwrap_or_else(|| Err("no completed processing receipt".into()))
23            .map_err(|reason| format!("incomplete product #{product_id}: {reason}"))
24    }
25
26    pub(crate) fn record(
27        &self, id: u32, produced: &ProducedElementMeshes,
28        decoder: &ifc_lite_core::EntityDecoder,
29        unsupported: &rustc_hash::FxHashMap<String, u64>,
30    ) {
31        let mut reasons = Vec::new();
32        if !unsupported.is_empty() { reasons.push(format!("dropped representation items: {}", ifc_lite_geometry::format_unsupported_breakdown(unsupported))); }
33        if decoder.refused_records() > 0 { reasons.push("refused geometry input".into()); }
34        if produced.csg_failures.values().any(|failures| !failures.is_empty()) { reasons.push("CSG diagnostic/fallback".into()); }
35        if produced.degenerate_triangles_dropped > 0 { reasons.push("degenerate triangles dropped".into()); }
36        if produced.meshes.is_empty() { reasons.push("no mesh output".into()); }
37        let receipt = if reasons.is_empty() { Ok(()) } else { Err(reasons.join("; ")) };
38        self.receipts.lock().unwrap_or_else(|e| e.into_inner()).insert(id, receipt);
39    }
40}