#[cfg(test)]
mod tests;
use crate::{EntitySchema, Schema};
use std::{fmt::Write, sync::Mutex};
#[doc(hidden)]
pub use ctor as __ctor;
#[doc(hidden)]
#[cfg(not(rust_analyzer))]
#[macro_export]
macro_rules! __register_schema {
($entity:ident) => {
const _: () = {
$crate::build::__ctor::declarative::ctor! {
#[ctor(unsafe, anonymous)]
fn register() {
$crate::build::__register(
<$entity as $crate::Entity>::declaration_path(),
<$entity as $crate::Entity>::SCHEMA,
);
}
}
};
};
}
#[derive(Debug)]
pub enum Error {
Poisoned,
LateRegistration,
DuplicatePath(&'static str),
InvalidModule,
EmptyModule,
InvalidReference(String),
Schema(crate::Error),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidReference(message) => f.write_str(message),
_ => write!(f, "{self:?}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Schema(error) => Some(error),
_ => None,
}
}
}
#[derive(Default)]
struct Registry {
nodes: Vec<(&'static str, &'static EntitySchema)>,
sealed: bool,
late: bool,
}
impl Registry {
fn register(&mut self, path: &'static str, schema: &'static EntitySchema) {
if self.sealed {
self.late = true;
} else {
self.nodes.push((path, schema));
}
}
fn generate(&mut self, source: &str, target: &str) -> Result<String, Error> {
self.assembly(source, target, false, |_| {})
}
fn assembly(
&mut self,
source: &str,
target: &str,
public: bool,
mut member: impl FnMut(&str),
) -> Result<String, Error> {
if self.late {
return Err(Error::LateRegistration);
}
if source.is_empty() || target.is_empty() {
return Err(Error::InvalidModule);
}
self.nodes.sort_by_key(|(path, _)| *path);
if let Some(pair) = self.nodes.windows(2).find(|pair| pair[0].0 == pair[1].0) {
return Err(Error::DuplicatePath(pair[0].0));
}
let prefix = format!("{source}::");
let mut selected: Vec<_> = self
.nodes
.iter()
.filter_map(|(path, schema)| path.strip_prefix(&prefix).map(|suffix| (suffix, *schema)))
.collect();
if selected.is_empty() {
return Err(Error::EmptyModule);
}
selected.sort_by_key(|(_, schema)| schema.name);
let mut reference_error = None;
let schema = Schema::admit_schema(
&selected
.iter()
.map(|(_, schema)| *schema)
.collect::<Vec<_>>(),
|entity, field, target, key| {
reference_error = Some(Error::InvalidReference(match key {
Some((key, expected)) => format!(
"{entity}.{} has type {:?} but references {target}.{key}, which requires {expected:?}", field.name, field.value
),
None => format!(
"{}.{} references {target}, which is missing from the selected schema module {source}", entity, field.name
),
}));
},
)
.map_err(|error| reference_error.unwrap_or(Error::Schema(error)))?;
let visibility = if public { "pub " } else { "" };
let mut code = String::from(
"// Generated from the validated host schema; do not edit.\n\
/// Obtain this root’s validated executable schema.\n",
);
code.push_str(visibility);
code.push_str(
"fn compiled() -> ::ic_db::Schema {\n\
::std::thread_local! { static SCHEMA: ::ic_db::Schema = {\n\
let schema = ::ic_db::Schema::new(&[\n",
);
for (path, _) in selected {
member(path);
writeln!(code, "<{target}::{path} as ::ic_db::Entity>::SCHEMA,").unwrap();
}
code.push_str("] ).expect(\"generated schema admission failed\");\n");
writeln!(
code,
"assert_eq!(schema.fingerprint(), {:?}, \"host/target schema mismatch\");",
schema.fingerprint()
)
.unwrap();
code.push_str("schema\n}; }\nSCHEMA.with(::std::clone::Clone::clone)\n}\n");
self.sealed = true;
Ok(code)
}
}
static REGISTRY: Mutex<Registry> = Mutex::new(Registry {
nodes: Vec::new(),
sealed: false,
late: false,
});
#[doc(hidden)]
pub fn __register(path: &'static str, schema: &'static EntitySchema) {
REGISTRY
.lock()
.expect("schema registration lock poisoned")
.register(path, schema);
}
pub fn generate(source_module: &str, target_module: &str) -> Result<String, Error> {
REGISTRY
.lock()
.map_err(|_| Error::Poisoned)?
.generate(source_module, target_module)
}
#[macro_export]
macro_rules! build_schema {
() => { $crate::build_schema!(src::schema => main); };
(=> $function:ident) => { $crate::build_schema!(src::schema => $function); };
(src::$schema:ident) => { $crate::build_schema!(src::$schema => main); };
(self::$schema:ident) => { $crate::build_schema!(self::$schema => main); };
(self::$schema:ident => $function:ident) => {
mod $schema;
$crate::build_schema!(@function $schema, $function, "::", false);
};
(src::$schema:ident => $function:ident) => {
$crate::build_schema!(@src $schema, $function, false);
};
($schema:ident) => {
$crate::build_schema!(@src $schema, main, true);
};
(@src $schema:ident, $function:ident, $owned:literal) => {
mod src { pub mod $schema; }
pub use self::src::$schema;
$crate::build_schema!(@function $schema, $function, "::src::", $owned);
};
(@function $schema:ident, $function:ident, $prefix:literal, $owned:literal) => {
fn $function() -> ::std::result::Result<(), ::std::boxed::Box<dyn ::std::error::Error>> {
$crate::build::__build_schema(
::std::concat!(::std::module_path!(), $prefix, ::std::stringify!($schema)),
::std::stringify!($schema), $owned,
)
}
};
}
#[doc(hidden)]
pub fn __build_schema(
source: &str,
module: &str,
owned: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let target = format!("crate::{module}");
if !owned {
return write_assembly(&generate(source, &target)?);
}
let prefix = format!("::{module}::");
let mut members = std::collections::BTreeMap::<String, Vec<String>>::new();
let assembly =
REGISTRY
.lock()
.map_err(|_| Error::Poisoned)?
.assembly(source, &target, true, |path| {
let name = path.rsplit("::").next().unwrap_or(path);
members
.entry(name.to_owned())
.or_default()
.push(format!("{prefix}{path}"));
})?;
write_assembly(&assembly)?;
println!("cargo::rustc-env=IC_DB_SCHEMA_ROOT={prefix}");
for (name, paths) in members {
println!(
"cargo::rustc-env=IC_DB_SCHEMA_MEMBER_{name}={}",
paths.join("|")
);
}
Ok(())
}
fn write_assembly(assembly: &str) -> Result<(), Box<dyn std::error::Error>> {
let out = std::env::var_os("OUT_DIR").ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::NotFound, "Cargo did not set OUT_DIR")
})?;
std::fs::write(std::path::PathBuf::from(out).join("schema.rs"), assembly)?;
Ok(())
}