Skip to main content

icydb_schema/build/
mod.rs

1use crate::{Error, ThisError, node::Schema, prelude::*, validate::validate_schema};
2use std::sync::{LazyLock, OnceLock, RwLock, RwLockReadGuard, RwLockWriteGuard};
3
4///
5/// BuildError
6///
7/// Error returned when the process-global schema graph fails validation before
8/// build-time code generation.
9///
10
11#[derive(Debug, ThisError)]
12pub enum BuildError {
13    #[error("validation failed: {0}")]
14    Validation(ErrorTree),
15}
16
17/// Process-global schema graph used during build-time code generation.
18static SCHEMA: LazyLock<RwLock<Schema>> = LazyLock::new(|| RwLock::new(Schema::new()));
19
20static SCHEMA_VALIDATED: OnceLock<bool> = OnceLock::new();
21
22/// Acquire a write guard to the global schema during build-time codegen.
23pub fn schema_write() -> RwLockWriteGuard<'static, Schema> {
24    SCHEMA
25        .write()
26        .expect("schema RwLock poisoned while acquiring write lock")
27}
28
29/// Read the schema graph without triggering validation.
30pub(crate) fn schema_read() -> RwLockReadGuard<'static, Schema> {
31    SCHEMA
32        .read()
33        .expect("schema RwLock poisoned while acquiring read lock")
34}
35
36/// Read the global schema, validating it exactly once per process.
37pub fn get_schema() -> Result<RwLockReadGuard<'static, Schema>, Error> {
38    let schema = schema_read();
39    validate(&schema).map_err(BuildError::Validation)?;
40
41    Ok(schema)
42}
43
44/// Validate the schema once per process before exposing it to codegen.
45fn validate(schema: &Schema) -> Result<(), ErrorTree> {
46    if *SCHEMA_VALIDATED.get_or_init(|| false) {
47        return Ok(());
48    }
49
50    validate_schema(schema)?;
51
52    SCHEMA_VALIDATED.set(true).ok();
53
54    Ok(())
55}