ifc_template_catalog/
runtime.rs1use std::sync::OnceLock;
20
21use sha2::{Digest, Sha256};
22use thiserror::Error;
23
24use crate::catalog::{Catalog, CatalogProfile};
25use crate::definition::CatalogEdition;
26use crate::overlay::{corrected_patches, PatchError};
27use crate::snapshot::{self, ArchiveError};
28
29static IFC2X3_TC1: OnceLock<Catalog> = OnceLock::new();
30static IFC4_ADD2_TC1: OnceLock<Catalog> = OnceLock::new();
31static IFC4X3_ADD2: OnceLock<Catalog> = OnceLock::new();
32static IFC4_ADD2_TC1_CORRECTED: OnceLock<Result<Catalog, PatchError>> = OnceLock::new();
33static IFC4X3_ADD2_CORRECTED: OnceLock<Result<Catalog, PatchError>> = OnceLock::new();
34
35fn official_slot(edition: CatalogEdition) -> &'static OnceLock<Catalog> {
36 match edition {
37 CatalogEdition::Ifc2x3Tc1 => &IFC2X3_TC1,
38 CatalogEdition::Ifc4Add2Tc1 => &IFC4_ADD2_TC1,
39 CatalogEdition::Ifc4x3Add2 => &IFC4X3_ADD2,
40 }
41}
42
43pub fn decode_verified(
46 edition: CatalogEdition,
47 bytes: &[u8],
48) -> Result<Catalog, RuntimeCatalogError> {
49 verify(edition, bytes)?;
50 Ok(snapshot::decode_edition(bytes, edition)?)
51}
52
53pub fn install(edition: CatalogEdition, bytes: &[u8]) -> Result<(), RuntimeCatalogError> {
58 let slot = official_slot(edition);
59 if slot.get().is_some() {
60 verify(edition, bytes)?;
62 return Ok(());
63 }
64 let catalog = decode_verified(edition, bytes)?;
65 let _ = slot.set(catalog);
68 Ok(())
69}
70
71fn verify(edition: CatalogEdition, bytes: &[u8]) -> Result<(), RuntimeCatalogError> {
72 let actual: String = Sha256::digest(bytes)
73 .iter()
74 .map(|byte| format!("{byte:02x}"))
75 .collect();
76 let expected = snapshot::pinned_sha256(edition);
77 if actual == expected || actual == snapshot::CONTAINER_SHA256 {
78 Ok(())
79 } else {
80 Err(RuntimeCatalogError::DigestMismatch {
81 edition,
82 expected,
83 actual,
84 })
85 }
86}
87
88pub fn is_installed(edition: CatalogEdition) -> bool {
90 official_slot(edition).get().is_some()
91}
92
93pub fn load_catalog(
97 edition: CatalogEdition,
98 profile: CatalogProfile,
99) -> Result<Catalog, RuntimeCatalogError> {
100 let official = official_slot(edition)
101 .get()
102 .cloned()
103 .ok_or(RuntimeCatalogError::NotInstalled(edition))?;
104 match profile {
105 CatalogProfile::Official => Ok(official),
106 CatalogProfile::Corrected => {
107 let slot = match edition {
108 CatalogEdition::Ifc4Add2Tc1 => &IFC4_ADD2_TC1_CORRECTED,
109 CatalogEdition::Ifc4x3Add2 => &IFC4X3_ADD2_CORRECTED,
110 _ => return Err(RuntimeCatalogError::UnavailableEdition(edition)),
111 };
112 slot.get_or_init(|| {
113 official.with_patches(CatalogProfile::Corrected, &corrected_patches(edition))
114 })
115 .clone()
116 .map_err(RuntimeCatalogError::Patch)
117 }
118 _ => Err(RuntimeCatalogError::UnsupportedProfile(profile)),
119 }
120}
121
122#[derive(Debug, Clone, PartialEq, Eq, Error)]
124#[non_exhaustive]
125pub enum RuntimeCatalogError {
126 #[error(
130 "the {edition:?} catalog bytes have SHA-256 {actual}; the pinned snapshot has {expected}"
131 )]
132 DigestMismatch {
133 edition: CatalogEdition,
135 expected: &'static str,
137 actual: String,
139 },
140 #[error(transparent)]
143 Archive(#[from] ArchiveError),
144 #[error("no {0:?} catalog is installed; install its snapshot first")]
146 NotInstalled(CatalogEdition),
147 #[error("no corrected profile for {0:?}")]
149 UnavailableEdition(CatalogEdition),
150 #[error("runtime loading does not construct {0:?} profiles")]
152 UnsupportedProfile(CatalogProfile),
153 #[error(transparent)]
155 Patch(#[from] PatchError),
156}