pgrx_sql_entity_graph/
positioning_ref.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
12Positioning references for Rust to SQL mapping support.
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 quote::{quote, ToTokens};
19use std::fmt::Display;
20use syn::parse::{Parse, ParseStream};
21
22#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
23pub enum PositioningRef {
24    FullPath(String),
25    Name(String),
26}
27
28impl Display for PositioningRef {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        match self {
31            PositioningRef::FullPath(i) => f.write_str(i),
32            PositioningRef::Name(i) => f.write_str(i),
33        }
34    }
35}
36
37impl Parse for PositioningRef {
38    fn parse(input: ParseStream) -> Result<Self, syn::Error> {
39        let maybe_litstr: Option<syn::LitStr> = input.parse()?;
40        let found = if let Some(litstr) = maybe_litstr {
41            Self::Name(litstr.value())
42        } else {
43            let path: syn::Path = input.parse()?;
44            let path_str = path.to_token_stream().to_string().replace(' ', "");
45            Self::FullPath(path_str)
46        };
47        Ok(found)
48    }
49}
50
51impl ToTokens for PositioningRef {
52    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
53        let toks = match self {
54            PositioningRef::FullPath(item) => quote! {
55                ::pgrx::pgrx_sql_entity_graph::PositioningRef::FullPath(String::from(#item))
56            },
57            PositioningRef::Name(item) => quote! {
58                ::pgrx::pgrx_sql_entity_graph::PositioningRef::Name(String::from(#item))
59            },
60        };
61        toks.to_tokens(tokens);
62    }
63}