pgrx_sql_entity_graph/enrich.rs
1//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC.
2//LICENSE
3//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc.
4//LICENSE
5//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <contact@pgcentral.org>
6//LICENSE
7//LICENSE All rights reserved.
8//LICENSE
9//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
10use proc_macro2::TokenStream as TokenStream2;
11use quote::{quote, ToTokens, TokenStreamExt};
12
13pub struct CodeEnrichment<T>(pub T);
14
15/// Generates the rust code that pgrx requires for one of its SQL interfaces such as `#[pg_extern]`
16pub trait ToRustCodeTokens {
17 fn to_rust_code_tokens(&self) -> TokenStream2 {
18 quote! {}
19 }
20}
21
22/// Generates the rust code to tie one of pgrx' supported SQL interfaces into pgrx' schema generator
23pub trait ToEntityGraphTokens {
24 fn to_entity_graph_tokens(&self) -> TokenStream2;
25}
26
27impl<T> ToTokens for CodeEnrichment<T>
28where
29 T: ToEntityGraphTokens + ToRustCodeTokens,
30{
31 fn to_tokens(&self, tokens: &mut TokenStream2) {
32 #[cfg(not(feature = "no-schema-generation"))]
33 {
34 // only emit entity graph tokens when we're generating a schema, which is our default mode
35 tokens.append_all(self.0.to_entity_graph_tokens());
36 }
37
38 tokens.append_all(self.0.to_rust_code_tokens());
39 }
40}