pgx_sql_entity_graph/
enrich.rs

1use proc_macro2::TokenStream as TokenStream2;
2use quote::{quote, ToTokens, TokenStreamExt};
3
4pub struct CodeEnrichment<T>(pub T);
5
6/// Generates the rust code that pgx requires for one of its SQL interfaces such as `#[pg_extern]`
7pub trait ToRustCodeTokens {
8    fn to_rust_code_tokens(&self) -> TokenStream2 {
9        quote! {}
10    }
11}
12
13/// Generates the rust code to tie one of pgx' supported SQL interfaces into pgx' schema generator
14pub trait ToEntityGraphTokens {
15    fn to_entity_graph_tokens(&self) -> TokenStream2 {
16        quote! {}
17    }
18}
19
20impl<T> ToTokens for CodeEnrichment<T>
21where
22    T: ToEntityGraphTokens + ToRustCodeTokens,
23{
24    fn to_tokens(&self, tokens: &mut TokenStream2) {
25        #[cfg(not(feature = "no-schema-generation"))]
26        {
27            // only emit entity graph tokens when we're generating a schema, which is our default mode
28            tokens.append_all(self.0.to_entity_graph_tokens());
29        }
30
31        tokens.append_all(self.0.to_rust_code_tokens());
32    }
33}