Skip to content

Commit

Permalink
Merge pull request #8 from negezor/derive-syn2
Browse files Browse the repository at this point in the history
Derive syn2
  • Loading branch information
daniel7grant committed Apr 1, 2024
2 parents bf0c11b + 250e3cf commit 55ff184
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
31 changes: 21 additions & 10 deletions redis-macros-derive/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions redis-macros-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ keywords = ["redis", "macro", "derive", "json"]
proc-macro = true

[dependencies]
proc-macro2 = "1.0.49"
quote = "1.0.23"
syn = { version = "1.0.107" }
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "2.0" }

[dev-dependencies]
redis = { version = "0.22.2", features = ["tokio-comp", "json"] }
Expand Down
37 changes: 10 additions & 27 deletions redis-macros-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,22 @@
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{
parenthesized,
parse::{Parse, ParseStream},
parse_macro_input, token, Attribute, DeriveInput, GenericParam, Result,
};

struct ParseParenthesed {
_p: token::Paren,
field: TokenStream2,
}

impl Parse for ParseParenthesed {
fn parse(input: ParseStream) -> Result<Self> {
let content;
Ok(ParseParenthesed {
_p: parenthesized!(content in input),
field: content.parse()?,
})
}
}
use quote::{quote, ToTokens};
use syn::{parse_macro_input, Attribute, DeriveInput, Expr, GenericParam};

fn get_serializer(attrs: Vec<Attribute>, default: &str) -> TokenStream2 {
let default_token = default.parse::<TokenStream2>().unwrap();

attrs
.into_iter()
.find(|a| a.path.segments.len() == 1 && a.path.segments[0].ident == "redis_serializer")
.map(|Attribute { tokens, .. }| {
let tokens = tokens.into();
let ParseParenthesed { field, .. } = parse_macro_input!(tokens as ParseParenthesed);
field.into()
.find(|attr| attr.path().is_ident("redis_serializer"))
.and_then(|attr| {
let Ok(Expr::Path(path)) = attr.parse_args::<Expr>() else {
return None;
};

Some(TokenStream2::from(path.to_token_stream()))
})
.unwrap_or(default_token.into())
.into()
}

/// Derive macro for the redis crate's [`FromRedisValue`](../redis/trait.FromRedisValue.html) trait to allow parsing Redis responses to this type.
Expand Down

0 comments on commit 55ff184

Please sign in to comment.