Skip to main content

ifc_template_catalog/
embedded.rs

1//! Embedded official and corrected catalog snapshots.
2//!
3//! Every edition comes from one container, `data/catalog.bin`
4//! ([`crate::snapshot`]), which stores text and templates the editions share
5//! once. The first load of an edition decodes that edition's records only.
6
7use std::sync::OnceLock;
8
9use thiserror::Error;
10
11use crate::catalog::{Catalog, CatalogProfile};
12use crate::definition::CatalogEdition;
13use crate::overlay::{corrected_patches, PatchError};
14use crate::snapshot::{decode_edition, ArchiveError};
15
16/// The committed container holding every edition.
17pub(crate) const CONTAINER: &[u8] = include_bytes!("../data/catalog.bin");
18
19static IFC2X3_TC1: OnceLock<Result<Catalog, ArchiveError>> = OnceLock::new();
20static IFC4_ADD2_TC1: OnceLock<Result<Catalog, ArchiveError>> = OnceLock::new();
21static IFC4X3_ADD2: OnceLock<Result<Catalog, ArchiveError>> = OnceLock::new();
22static IFC4_ADD2_TC1_CORRECTED: OnceLock<Result<Catalog, EmbeddedCatalogError>> = OnceLock::new();
23static IFC4X3_ADD2_CORRECTED: OnceLock<Result<Catalog, EmbeddedCatalogError>> = OnceLock::new();
24
25/// Load a catalog snapshot from committed generated data.
26pub fn load_catalog(
27    edition: CatalogEdition,
28    profile: CatalogProfile,
29) -> Result<Catalog, EmbeddedCatalogError> {
30    match profile {
31        CatalogProfile::Official => official_catalog(edition),
32        CatalogProfile::Corrected => corrected_catalog(edition),
33        _ => Err(EmbeddedCatalogError::UnsupportedProfile(profile)),
34    }
35}
36
37/// Load an unmodified official catalog.
38pub fn official_catalog(edition: CatalogEdition) -> Result<Catalog, EmbeddedCatalogError> {
39    let slot = match edition {
40        CatalogEdition::Ifc2x3Tc1 => &IFC2X3_TC1,
41        CatalogEdition::Ifc4Add2Tc1 => &IFC4_ADD2_TC1,
42        CatalogEdition::Ifc4x3Add2 => &IFC4X3_ADD2,
43    };
44    slot.get_or_init(|| decode_edition(CONTAINER, edition))
45        .clone()
46        .map_err(EmbeddedCatalogError::Archive)
47}
48
49/// Load the official catalog with the ordered built-in correction ledger.
50pub fn corrected_catalog(edition: CatalogEdition) -> Result<Catalog, EmbeddedCatalogError> {
51    let slot = match edition {
52        CatalogEdition::Ifc4Add2Tc1 => &IFC4_ADD2_TC1_CORRECTED,
53        CatalogEdition::Ifc4x3Add2 => &IFC4X3_ADD2_CORRECTED,
54        _ => return Err(EmbeddedCatalogError::UnavailableEdition(edition)),
55    };
56    slot.get_or_init(|| {
57        let official = official_catalog(edition)?;
58        official
59            .with_patches(CatalogProfile::Corrected, &corrected_patches(edition))
60            .map_err(EmbeddedCatalogError::Patch)
61    })
62    .clone()
63}
64
65/// Why loading a committed embedded catalog snapshot failed.
66#[derive(Debug, Clone, PartialEq, Eq, Error)]
67#[non_exhaustive]
68pub enum EmbeddedCatalogError {
69    /// No committed generated data exists for this edition/profile pair.
70    #[error("no embedded catalog for {0:?}")]
71    UnavailableEdition(CatalogEdition),
72    /// [`load_catalog`] was called with a profile other than `Official` or `Corrected`.
73    #[error("embedded loading does not construct {0:?} profiles")]
74    UnsupportedProfile(CatalogProfile),
75    /// The committed binary artifact failed to decode.
76    #[error(transparent)]
77    Archive(#[from] ArchiveError),
78    /// Applying the built-in correction ledger to the official catalog failed.
79    #[error(transparent)]
80    Patch(#[from] PatchError),
81}