Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive syn2 #8

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading