pub mod entity;
use crate::enrich::{ToEntityGraphTokens, ToRustCodeTokens};
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::quote;
use syn::parse::{Parse, ParseStream};
use syn::{DeriveInput, Ident};
use crate::{CodeEnrichment, ToSqlConfig};
#[derive(Debug, Clone)]
pub struct PostgresOrd {
pub name: Ident,
pub to_sql_config: ToSqlConfig,
}
impl PostgresOrd {
pub fn new(
name: Ident,
to_sql_config: ToSqlConfig,
) -> Result<CodeEnrichment<Self>, syn::Error> {
if !to_sql_config.overrides_default() {
crate::ident_is_acceptable_to_postgres(&name)?;
}
Ok(CodeEnrichment(Self { name, to_sql_config }))
}
pub fn from_derive_input(
derive_input: DeriveInput,
) -> Result<CodeEnrichment<Self>, syn::Error> {
let to_sql_config =
ToSqlConfig::from_attributes(derive_input.attrs.as_slice())?.unwrap_or_default();
Self::new(derive_input.ident, to_sql_config)
}
}
impl ToEntityGraphTokens for PostgresOrd {
fn to_entity_graph_tokens(&self) -> TokenStream2 {
let name = &self.name;
let sql_graph_entity_fn_name =
syn::Ident::new(&format!("__pgrx_internals_ord_{}", self.name), Span::call_site());
let to_sql_config = &self.to_sql_config;
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 {
use core::any::TypeId;
extern crate alloc;
use alloc::vec::Vec;
use alloc::vec;
let submission = ::pgrx::pgrx_sql_entity_graph::PostgresOrdEntity {
name: stringify!(#name),
file: file!(),
line: line!(),
full_path: core::any::type_name::<#name>(),
module_path: module_path!(),
id: TypeId::of::<#name>(),
to_sql_config: #to_sql_config,
};
::pgrx::pgrx_sql_entity_graph::SqlGraphEntity::Ord(submission)
}
}
}
}
impl ToRustCodeTokens for PostgresOrd {}
impl Parse for CodeEnrichment<PostgresOrd> {
fn parse(input: ParseStream) -> Result<Self, syn::Error> {
use syn::Item;
let parsed = input.parse()?;
let (ident, attrs) = match &parsed {
Item::Enum(item) => (item.ident.clone(), item.attrs.as_slice()),
Item::Struct(item) => (item.ident.clone(), item.attrs.as_slice()),
_ => return Err(syn::Error::new(input.span(), "expected enum or struct")),
};
let to_sql_config = ToSqlConfig::from_attributes(attrs)?.unwrap_or_default();
PostgresOrd::new(ident, to_sql_config)
}
}