pgx_utils/sql_entity_graph/postgres_hash/
mod.rs

1/*
2Portions Copyright 2019-2021 ZomboDB, LLC.
3Portions Copyright 2021-2022 Technology Concepts & Design, Inc. <support@tcdi.com>
4
5All rights reserved.
6
7Use of this source code is governed by the MIT license that can be found in the LICENSE file.
8*/
9/*!
10
11`#[derive(PostgresHash)]` related macro expansion for Rust to SQL translation
12
13> Like all of the [`sql_entity_graph`][crate::sql_entity_graph] APIs, this is considered **internal**
14to the `pgx` framework and very subject to change between versions. While you may use this, please do it with caution.
15
16*/
17pub mod entity;
18
19use proc_macro2::{Span, TokenStream as TokenStream2};
20use quote::{quote, ToTokens, TokenStreamExt};
21use syn::parse::{Parse, ParseStream};
22use syn::{DeriveInput, Ident};
23
24use crate::sql_entity_graph::ToSqlConfig;
25
26/// A parsed `#[derive(PostgresHash)]` item.
27///
28/// It should be used with [`syn::parse::Parse`] functions.
29///
30/// Using [`quote::ToTokens`] will output the declaration for a `pgx::datum::sql_entity_graph::InventoryPostgresHash`.
31///
32/// On structs:
33///
34/// ```rust
35/// use syn::{Macro, parse::Parse, parse_quote, parse};
36/// use quote::{quote, ToTokens};
37/// use pgx_utils::sql_entity_graph::PostgresHash;
38///
39/// # fn main() -> eyre::Result<()> {
40/// let parsed: PostgresHash = parse_quote! {
41///     #[derive(PostgresHash)]
42///     struct Example<'a> {
43///         demo: &'a str,
44///     }
45/// };
46/// let sql_graph_entity_tokens = parsed.to_token_stream();
47/// # Ok(())
48/// # }
49/// ```
50///
51/// On enums:
52///
53/// ```rust
54/// use syn::{Macro, parse::Parse, parse_quote, parse};
55/// use quote::{quote, ToTokens};
56/// use pgx_utils::sql_entity_graph::PostgresHash;
57///
58/// # fn main() -> eyre::Result<()> {
59/// let parsed: PostgresHash = parse_quote! {
60///     #[derive(PostgresHash)]
61///     enum Demo {
62///         Example,
63///     }
64/// };
65/// let sql_graph_entity_tokens = parsed.to_token_stream();
66/// # Ok(())
67/// # }
68/// ```
69#[derive(Debug, Clone)]
70pub struct PostgresHash {
71    pub name: Ident,
72    pub to_sql_config: ToSqlConfig,
73}
74
75impl PostgresHash {
76    pub fn new(name: Ident, to_sql_config: ToSqlConfig) -> Result<Self, syn::Error> {
77        if !to_sql_config.overrides_default() {
78            crate::ident_is_acceptable_to_postgres(&name)?;
79        }
80        Ok(Self { name, to_sql_config })
81    }
82
83    pub fn from_derive_input(derive_input: DeriveInput) -> Result<Self, syn::Error> {
84        let to_sql_config =
85            ToSqlConfig::from_attributes(derive_input.attrs.as_slice())?.unwrap_or_default();
86        Self::new(derive_input.ident, to_sql_config)
87    }
88}
89
90impl Parse for PostgresHash {
91    fn parse(input: ParseStream) -> Result<Self, syn::Error> {
92        use syn::Item;
93
94        let parsed = input.parse()?;
95        let (ident, attrs) = match &parsed {
96            Item::Enum(item) => (item.ident.clone(), item.attrs.as_slice()),
97            Item::Struct(item) => (item.ident.clone(), item.attrs.as_slice()),
98            _ => return Err(syn::Error::new(input.span(), "expected enum or struct")),
99        };
100
101        let to_sql_config = ToSqlConfig::from_attributes(attrs)?.unwrap_or_default();
102        Self::new(ident, to_sql_config)
103    }
104}
105
106impl ToTokens for PostgresHash {
107    fn to_tokens(&self, tokens: &mut TokenStream2) {
108        let name = &self.name;
109        let sql_graph_entity_fn_name =
110            syn::Ident::new(&format!("__pgx_internals_hash_{}", self.name), Span::call_site());
111        let to_sql_config = &self.to_sql_config;
112        let inv = quote! {
113            #[no_mangle]
114            #[doc(hidden)]
115            pub extern "Rust" fn  #sql_graph_entity_fn_name() -> ::pgx::utils::sql_entity_graph::SqlGraphEntity {
116                use core::any::TypeId;
117                extern crate alloc;
118                use alloc::vec::Vec;
119                use alloc::vec;
120                let submission = ::pgx::utils::sql_entity_graph::PostgresHashEntity {
121                    name: stringify!(#name),
122                    file: file!(),
123                    line: line!(),
124                    full_path: core::any::type_name::<#name>(),
125                    module_path: module_path!(),
126                    id: TypeId::of::<#name>(),
127                    to_sql_config: #to_sql_config,
128                };
129                ::pgx::utils::sql_entity_graph::SqlGraphEntity::Hash(submission)
130            }
131        };
132        tokens.append_all(inv);
133    }
134}