pgrx_sql_entity_graph/pg_extern/
cast.rs1use proc_macro2::TokenStream as TokenStream2;
19use quote::{ToTokens, TokenStreamExt, quote};
20use syn::Path;
21
22#[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}
65
66impl PgCast {
67 pub fn section_len_tokens(&self) -> TokenStream2 {
68 quote! { ::pgrx::pgrx_sql_entity_graph::section::u8_len() }
69 }
70
71 pub fn section_writer_tokens(&self, writer: TokenStream2) -> TokenStream2 {
72 match self {
73 PgCast::Default => quote! {
74 #writer.u8(::pgrx::pgrx_sql_entity_graph::section::OPERATOR_CAST_DEFAULT)
75 },
76 PgCast::Assignment => quote! {
77 #writer.u8(::pgrx::pgrx_sql_entity_graph::section::OPERATOR_CAST_ASSIGNMENT)
78 },
79 PgCast::Implicit => quote! {
80 #writer.u8(::pgrx::pgrx_sql_entity_graph::section::OPERATOR_CAST_IMPLICIT)
81 },
82 }
83 }
84}