pgx_sql_entity_graph/postgres_ord/
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(PostgresOrd)]` related macro expansion for Rust to SQL translation
12
13> Like all of the [`sql_entity_graph`][crate::pgx_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 crate::enrich::{ToEntityGraphTokens, ToRustCodeTokens};
20use proc_macro2::{Span, TokenStream as TokenStream2};
21use quote::quote;
22use syn::parse::{Parse, ParseStream};
23use syn::{DeriveInput, Ident};
24
25use crate::{CodeEnrichment, ToSqlConfig};
26
27/// A parsed `#[derive(PostgresOrd)]` item.
28///
29/// It should be used with [`syn::parse::Parse`] functions.
30///
31/// Using [`quote::ToTokens`] will output the declaration for a `pgx::datum::pgx_sql_entity_graph::InventoryPostgresOrd`.
32///
33/// On structs:
34///
35/// ```rust
36/// use syn::{Macro, parse::Parse, parse_quote, parse};
37/// use quote::{quote, ToTokens};
38/// use pgx_sql_entity_graph::PostgresOrd;
39///
40/// # fn main() -> eyre::Result<()> {
41/// use pgx_sql_entity_graph::CodeEnrichment;
42/// let parsed: CodeEnrichment<PostgresOrd> = parse_quote! {
43///     #[derive(PostgresOrd)]
44///     struct Example<'a> {
45///         demo: &'a str,
46///     }
47/// };
48/// let sql_graph_entity_tokens = parsed.to_token_stream();
49/// # Ok(())
50/// # }
51/// ```
52///
53/// On enums:
54///
55/// ```rust
56/// use syn::{Macro, parse::Parse, parse_quote, parse};
57/// use quote::{quote, ToTokens};
58/// use pgx_sql_entity_graph::PostgresOrd;
59///
60/// # fn main() -> eyre::Result<()> {
61/// use pgx_sql_entity_graph::CodeEnrichment;
62/// let parsed: CodeEnrichment<PostgresOrd> = parse_quote! {
63///     #[derive(PostgresOrd)]
64///     enum Demo {
65///         Example,
66///     }
67/// };
68/// let sql_graph_entity_tokens = parsed.to_token_stream();
69/// # Ok(())
70/// # }
71/// ```
72#[derive(Debug, Clone)]
73pub struct PostgresOrd {
74    pub name: Ident,
75    pub to_sql_config: ToSqlConfig,
76}
77
78impl PostgresOrd {
79    pub fn new(
80        name: Ident,
81        to_sql_config: ToSqlConfig,
82    ) -> Result<CodeEnrichment<Self>, syn::Error> {
83        if !to_sql_config.overrides_default() {
84            crate::ident_is_acceptable_to_postgres(&name)?;
85        }
86
87        Ok(CodeEnrichment(Self { name, to_sql_config }))
88    }
89
90    pub fn from_derive_input(
91        derive_input: DeriveInput,
92    ) -> Result<CodeEnrichment<Self>, syn::Error> {
93        let to_sql_config =
94            ToSqlConfig::from_attributes(derive_input.attrs.as_slice())?.unwrap_or_default();
95        Self::new(derive_input.ident, to_sql_config)
96    }
97}
98
99impl ToEntityGraphTokens for PostgresOrd {
100    fn to_entity_graph_tokens(&self) -> TokenStream2 {
101        let name = &self.name;
102        let sql_graph_entity_fn_name =
103            syn::Ident::new(&format!("__pgx_internals_ord_{}", self.name), Span::call_site());
104        let to_sql_config = &self.to_sql_config;
105        quote! {
106            #[no_mangle]
107            #[doc(hidden)]
108            pub extern "Rust" fn  #sql_graph_entity_fn_name() -> ::pgx::pgx_sql_entity_graph::SqlGraphEntity {
109                use core::any::TypeId;
110                extern crate alloc;
111                use alloc::vec::Vec;
112                use alloc::vec;
113                let submission = ::pgx::pgx_sql_entity_graph::PostgresOrdEntity {
114                    name: stringify!(#name),
115                    file: file!(),
116                    line: line!(),
117                    full_path: core::any::type_name::<#name>(),
118                    module_path: module_path!(),
119                    id: TypeId::of::<#name>(),
120                    to_sql_config: #to_sql_config,
121                };
122                ::pgx::pgx_sql_entity_graph::SqlGraphEntity::Ord(submission)
123            }
124        }
125    }
126}
127
128impl ToRustCodeTokens for PostgresOrd {}
129
130impl Parse for CodeEnrichment<PostgresOrd> {
131    fn parse(input: ParseStream) -> Result<Self, syn::Error> {
132        use syn::Item;
133
134        let parsed = input.parse()?;
135        let (ident, attrs) = match &parsed {
136            Item::Enum(item) => (item.ident.clone(), item.attrs.as_slice()),
137            Item::Struct(item) => (item.ident.clone(), item.attrs.as_slice()),
138            _ => return Err(syn::Error::new(input.span(), "expected enum or struct")),
139        };
140        let to_sql_config = ToSqlConfig::from_attributes(attrs)?.unwrap_or_default();
141        PostgresOrd::new(ident, to_sql_config)
142    }
143}