pgrx_sql_entity_graph/postgres_ord/
mod.rs1pub mod entity;
19
20use crate::enrich::{ToEntityGraphTokens, ToRustCodeTokens};
21use proc_macro2::TokenStream as TokenStream2;
22use quote::{format_ident, quote};
23use syn::parse::{Parse, ParseStream};
24use syn::{DeriveInput, Ident};
25
26use crate::{CodeEnrichment, ToSqlConfig};
27
28#[derive(Debug, Clone)]
74pub struct PostgresOrd {
75 pub name: Ident,
76 pub to_sql_config: ToSqlConfig,
77}
78
79impl PostgresOrd {
80 pub fn new(
81 name: Ident,
82 to_sql_config: ToSqlConfig,
83 ) -> Result<CodeEnrichment<Self>, syn::Error> {
84 if !to_sql_config.overrides_default() {
85 crate::ident_is_acceptable_to_postgres(&name)?;
86 }
87
88 Ok(CodeEnrichment(Self { name, to_sql_config }))
89 }
90
91 pub fn from_derive_input(
92 derive_input: DeriveInput,
93 ) -> Result<CodeEnrichment<Self>, syn::Error> {
94 let to_sql_config =
95 ToSqlConfig::from_attributes(derive_input.attrs.as_slice())?.unwrap_or_default();
96 Self::new(derive_input.ident, to_sql_config)
97 }
98}
99
100impl ToEntityGraphTokens for PostgresOrd {
101 fn to_entity_graph_tokens(&self) -> TokenStream2 {
102 let name = &self.name;
103 let sql_graph_entity_fn_name = format_ident!("__pgrx_internals_ord_{}", self.name);
104 let to_sql_config = &self.to_sql_config;
105 quote! {
106 #[no_mangle]
107 #[doc(hidden)]
108 #[allow(nonstandard_style, unknown_lints, clippy::no_mangle_with_rust_abi)]
109 pub extern "Rust" fn #sql_graph_entity_fn_name() -> ::pgrx::pgrx_sql_entity_graph::SqlGraphEntity {
110 use core::any::TypeId;
111 extern crate alloc;
112 use alloc::vec::Vec;
113 use alloc::vec;
114 let submission = ::pgrx::pgrx_sql_entity_graph::PostgresOrdEntity {
115 name: stringify!(#name),
116 file: file!(),
117 line: line!(),
118 full_path: core::any::type_name::<#name>(),
119 module_path: module_path!(),
120 id: TypeId::of::<#name>(),
121 to_sql_config: #to_sql_config,
122 };
123 ::pgrx::pgrx_sql_entity_graph::SqlGraphEntity::Ord(submission)
124 }
125 }
126 }
127}
128
129impl ToRustCodeTokens for PostgresOrd {}
130
131impl Parse for CodeEnrichment<PostgresOrd> {
132 fn parse(input: ParseStream) -> Result<Self, syn::Error> {
133 use syn::Item;
134
135 let parsed = input.parse()?;
136 let (ident, attrs) = match &parsed {
137 Item::Enum(item) => (item.ident.clone(), item.attrs.as_slice()),
138 Item::Struct(item) => (item.ident.clone(), item.attrs.as_slice()),
139 _ => return Err(syn::Error::new(input.span(), "expected enum or struct")),
140 };
141 let to_sql_config = ToSqlConfig::from_attributes(attrs)?.unwrap_or_default();
142 PostgresOrd::new(ident, to_sql_config)
143 }
144}