pgrx_sql_entity_graph/pg_extern/cast.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.
10/*!
11
12`#[pg_cast]` related macro expansion for Rust to SQL translation
13
14> Like all of the [`sql_entity_graph`][crate] APIs, this is considered **internal**
15> to the `pgrx` framework and very subject to change between versions. While you may use this, please do it with caution.
16
17*/
18use proc_macro2::TokenStream as TokenStream2;
19use quote::{quote, ToTokens, TokenStreamExt};
20use syn::Path;
21
22/// A parsed `#[pg_cast]` operator.
23///
24/// It is created during [`PgExtern`](crate::PgExtern) parsing.
25#[derive(Debug, Clone, Default)]
26pub enum PgCast {
27 #[default]
28 Default,
29 Assignment,
30 Implicit,
31}
32
33pub type InvalidCastStyle = Path;
34
35impl TryFrom<Path> for PgCast {
36 type Error = InvalidCastStyle;
37
38 fn try_from(path: Path) -> Result<Self, Self::Error> {
39 if path.is_ident("implicit") {
40 Ok(Self::Implicit)
41 } else if path.is_ident("assignment") {
42 Ok(Self::Assignment)
43 } else {
44 Err(path)
45 }
46 }
47}
48
49impl ToTokens for PgCast {
50 fn to_tokens(&self, tokens: &mut TokenStream2) {
51 let quoted = match self {
52 PgCast::Default => quote! {
53 ::pgrx::pgrx_sql_entity_graph::PgCastEntity::Default
54 },
55 PgCast::Assignment => quote! {
56 ::pgrx::pgrx_sql_entity_graph::PgCastEntity::Assignment
57 },
58 PgCast::Implicit => quote! {
59 ::pgrx::pgrx_sql_entity_graph::PgCastEntity::Implicit
60 },
61 };
62 tokens.append_all(quoted);
63 }
64}