1#[cfg(not(target_arch = "wasm32"))]
2mod actor;
3
4use crate::{
5 ThisError,
6 node::{Schema, SchemaGraphError},
7 prelude::*,
8};
9use std::sync::{LazyLock, RwLock, RwLockReadGuard};
10
11#[cfg(not(target_arch = "wasm32"))]
12pub use actor::generate;
13
14#[cfg(not(target_arch = "wasm32"))]
15use crate::{Error, node::SchemaNode, schema_validate::validate_schema};
16#[cfg(not(target_arch = "wasm32"))]
17use std::sync::{Mutex, RwLockWriteGuard};
18
19#[derive(Debug, ThisError)]
27pub enum BuildError {
28 #[error(transparent)]
30 Graph(#[from] SchemaGraphError),
31
32 #[error("validation failed: {0}")]
34 Validation(ErrorTree),
35}
36
37static SCHEMA: LazyLock<RwLock<Schema>> = LazyLock::new(|| RwLock::new(Schema::new()));
39#[cfg(not(target_arch = "wasm32"))]
41static REGISTRATION_GATE: Mutex<()> = Mutex::new(());
42
43#[cfg(not(target_arch = "wasm32"))]
49pub(crate) fn schema_write() -> RwLockWriteGuard<'static, Schema> {
50 SCHEMA
51 .write()
52 .expect("schema RwLock poisoned while acquiring write lock")
53}
54
55#[cfg(not(target_arch = "wasm32"))]
64pub fn register_node(node: SchemaNode) {
65 let _registration = REGISTRATION_GATE
66 .lock()
67 .expect("schema registration gate poisoned while registering node");
68 schema_write().insert_node(node);
69}
70
71pub(crate) fn schema_read() -> RwLockReadGuard<'static, Schema> {
73 SCHEMA
74 .read()
75 .expect("schema RwLock poisoned while acquiring read lock")
76}
77
78#[cfg(not(target_arch = "wasm32"))]
88pub fn get_schema() -> Result<RwLockReadGuard<'static, Schema>, Error> {
89 let _registration = REGISTRATION_GATE
90 .lock()
91 .expect("schema registration gate poisoned while sealing graph");
92 {
93 let schema = schema_read();
94 if !schema.is_sealed() {
95 validate_schema(&schema).map_err(BuildError::Validation)?;
96 }
97 }
98 {
99 let mut schema = schema_write();
100 schema.seal().map_err(BuildError::Graph)?;
101 }
102
103 Ok(schema_read())
104}