#[cfg(not(target_arch = "wasm32"))]
mod actor;
use crate::{
ThisError,
node::{Schema, SchemaGraphError},
prelude::*,
};
use std::sync::{LazyLock, RwLock, RwLockReadGuard};
#[cfg(not(target_arch = "wasm32"))]
pub use actor::{BuildOptions, BuildSqlUpdatePolicy, generate_with_options};
#[cfg(not(target_arch = "wasm32"))]
use crate::{Error, node::SchemaNode, schema_validate::validate_schema};
#[cfg(not(target_arch = "wasm32"))]
use std::sync::{Mutex, RwLockWriteGuard};
#[derive(Debug, ThisError)]
pub enum BuildError {
#[error(transparent)]
Graph(#[from] SchemaGraphError),
#[error("validation failed: {0}")]
Validation(ErrorTree),
}
static SCHEMA: LazyLock<RwLock<Schema>> = LazyLock::new(|| RwLock::new(Schema::new()));
#[cfg(not(target_arch = "wasm32"))]
static REGISTRATION_GATE: Mutex<()> = Mutex::new(());
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn schema_write() -> RwLockWriteGuard<'static, Schema> {
SCHEMA
.write()
.expect("schema RwLock poisoned while acquiring write lock")
}
#[cfg(not(target_arch = "wasm32"))]
pub fn register_node(node: SchemaNode) {
let _registration = REGISTRATION_GATE
.lock()
.expect("schema registration gate poisoned while registering node");
schema_write().insert_node(node);
}
pub(crate) fn schema_read() -> RwLockReadGuard<'static, Schema> {
SCHEMA
.read()
.expect("schema RwLock poisoned while acquiring read lock")
}
#[cfg(not(target_arch = "wasm32"))]
pub fn get_schema() -> Result<RwLockReadGuard<'static, Schema>, Error> {
let _registration = REGISTRATION_GATE
.lock()
.expect("schema registration gate poisoned while sealing graph");
{
let schema = schema_read();
if !schema.is_sealed() {
validate_schema(&schema).map_err(BuildError::Validation)?;
}
}
{
let mut schema = schema_write();
schema.seal().map_err(BuildError::Graph)?;
}
Ok(schema_read())
}