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