Skip to main content

pgrx_sql_entity_graph/pg_trigger/
attribute.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_trigger]` attribute 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 crate::ToSqlConfig;
19
20use syn::Token;
21use syn::parse::{Parse, ParseStream};
22use syn::spanned::Spanned;
23
24#[derive(Debug, Clone, Hash, Eq, PartialEq)]
25pub enum PgTriggerAttribute {
26    Sql(ToSqlConfig),
27}
28
29impl Parse for PgTriggerAttribute {
30    fn parse(input: ParseStream) -> Result<Self, syn::Error> {
31        let ident: syn::Ident = input.parse()?;
32        let found = match ident.to_string().as_str() {
33            "sql" => {
34                use crate::pgrx_attribute::ArgValue;
35                use syn::Lit;
36
37                let _eq: Token![=] = input.parse()?;
38                match input.parse::<ArgValue>()? {
39                    ArgValue::Path(path) => {
40                        return Err(syn::Error::new(
41                            path.span(),
42                            "expected boolean or string literal",
43                        ));
44                    }
45                    ArgValue::Lit(Lit::Bool(b)) => Self::Sql(ToSqlConfig::from(b.value)),
46                    ArgValue::Lit(Lit::Str(s)) => Self::Sql(ToSqlConfig::from(s)),
47                    ArgValue::Lit(other) => {
48                        return Err(syn::Error::new(
49                            other.span(),
50                            "expected boolean or string literal",
51                        ));
52                    }
53                }
54            }
55            e => {
56                return Err(syn::Error::new(
57                    // FIXME: add a UI test for this
58                    input.span(),
59                    format!("Invalid option `{e}` inside `{ident} {input}`"),
60                ));
61            }
62        };
63        Ok(found)
64    }
65}