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