Skip to content

Commit

Permalink
last round of cleanups. coffee is neat
Browse files Browse the repository at this point in the history
  • Loading branch information
eeeebbbbrrrr committed Oct 27, 2024
1 parent 32e949d commit 6f0cebc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
19 changes: 8 additions & 11 deletions pgrx-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,18 @@ pub fn pg_cast(attr: TokenStream, item: TokenStream) -> TokenStream {
Ok(paths) => {
let mut new_paths = Punctuated::<syn::Path, syn::Token![,]>::new();
for path in paths {
if path.is_ident("implicit") {
if let Some(cast) = &cast {
panic!("The cast type has already been set to `{cast:?}`");
match (PgCast::try_from(path), &cast) {
(Ok(style), None) => cast = Some(style),
(Ok(_), Some(cast)) => {
panic!("The cast type has already been set to `{cast:?}`")
}
cast = Some(PgCast::Implicit);
} else if path.is_ident("assignment") {
if let Some(cast) = &cast {
panic!("The cast type has already been set to `{cast:?}`");
}
cast = Some(PgCast::Assignment);
} else {

// ... and anything it doesn't understand is blindly passed through to the
// underlying `#[pg_extern]` function that gets created, which will ultimately
// decide what's naughty and what's nice
new_paths.push(path);
(Err(unknown), _) => {
new_paths.push(unknown);
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions pgrx-sql-entity-graph/src/pg_extern/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens, TokenStreamExt};
use syn::Path;

/// A parsed `#[pg_cast]` operator.
///
Expand All @@ -29,6 +30,22 @@ pub enum PgCast {
Implicit,
}

pub type InvalidCastStyle = Path;

impl TryFrom<Path> for PgCast {
type Error = InvalidCastStyle;

fn try_from(path: Path) -> Result<Self, Self::Error> {
if path.is_ident("implicit") {
Ok(Self::Implicit)
} else if path.is_ident("assignment") {
Ok(Self::Assignment)
} else {
Err(path)
}
}
}

impl ToTokens for PgCast {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let quoted = match self {
Expand Down

0 comments on commit 6f0cebc

Please sign in to comment.