pub mod entity;
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens, TokenStreamExt};
use syn::parse::{Parse, ParseStream};
use syn::ItemMod;
#[derive(Debug, Clone)]
pub struct Schema {
pub module: ItemMod,
}
impl Schema {
#[cfg(feature = "no-schema-generation")]
fn entity_tokens(&self) -> TokenStream2 {
quote! {}
}
#[cfg(not(feature = "no-schema-generation"))]
fn entity_tokens(&self) -> TokenStream2 {
let ident = &self.module.ident;
let postfix = {
use std::hash::{Hash, Hasher};
let (_content_brace, content_items) =
&self.module.content.as_ref().expect("Can only support `mod {}` right now.");
let mut hasher = std::collections::hash_map::DefaultHasher::new();
content_items.hash(&mut hasher);
hasher.finish()
};
let sql_graph_entity_fn_name = syn::Ident::new(
&format!("__pgrx_internals_schema_{}_{}", ident, postfix),
proc_macro2::Span::call_site(),
);
quote! {
#[no_mangle]
#[doc(hidden)]
#[allow(unknown_lints, clippy::no_mangle_with_rust_abi)]
pub extern "Rust" fn #sql_graph_entity_fn_name() -> ::pgrx::pgrx_sql_entity_graph::SqlGraphEntity {
extern crate alloc;
use alloc::vec::Vec;
use alloc::vec;
let submission = ::pgrx::pgrx_sql_entity_graph::SchemaEntity {
module_path: module_path!(),
name: stringify!(#ident),
file: file!(),
line: line!(),
};
::pgrx::pgrx_sql_entity_graph::SqlGraphEntity::Schema(submission)
}
}
}
}
impl ToTokens for Schema {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let attrs = &self.module.attrs;
let vis = &self.module.vis;
let mod_token = &self.module.mod_token;
let ident = &self.module.ident;
let graph_tokens = self.entity_tokens();
let (_content_brace, content_items) =
&self.module.content.as_ref().expect("Can only support `mod {}` right now.");
let code = quote! {
#(#attrs)*
#vis #mod_token #ident {
#(#content_items)*
#graph_tokens
}
};
tokens.append_all(code)
}
}
impl Parse for Schema {
fn parse(input: ParseStream) -> Result<Self, syn::Error> {
let module: ItemMod = input.parse()?;
crate::ident_is_acceptable_to_postgres(&module.ident)?;
Ok(Self { module })
}
}