Skip to content

Commit

Permalink
Fixed token generation in matcher.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
azriel91 committed Oct 16, 2018
1 parent fb57110 commit 68802fb
Showing 1 changed file with 44 additions and 37 deletions.
81 changes: 44 additions & 37 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,48 +123,55 @@ impl Matcher {
where
N: quote::ToTokens,
{
let mut t = proc_macro2::TokenStream::new();
let mut matches = Vec::new();

let binding = self.binding_style;
name.to_tokens(&mut t);
match style {
ast::Style::Unit => {}
let (stream, matches) = match style {
ast::Style::Unit => (proc_macro2::TokenStream::new(), Vec::new()),
ast::Style::Tuple => {
t.extend(quote!("("));
for (i, field) in fields.iter().enumerate() {
let ident: syn::Ident = syn::Ident::new(
&format!("{}_{}", self.binding_name, i),
proc_macro2::Span::call_site(),
);
quote!(#binding #ident ,).to_tokens(&mut t);
matches.push(BindingInfo {
ident: ident,
field: field,
});
}
t.extend(quote!(")"));
let (mut stream, matches) = fields.iter().enumerate().fold(
(proc_macro2::TokenStream::new(), Vec::new()),
|(mut stream, mut matches), (i, field)| {
let ident: syn::Ident = syn::Ident::new(
&format!("{}_{}", self.binding_name, i),
proc_macro2::Span::call_site(),
);
quote!(#binding #ident ,).to_tokens(&mut stream);
matches.push(BindingInfo { ident, field });

(stream, matches)
},
);

(quote! { ( #stream ) }, matches)
}
ast::Style::Struct => {
t.extend(quote!("{"));
for (i, field) in fields.iter().enumerate() {
let ident: syn::Ident = syn::Ident::new(
&format!("{}_{}", self.binding_name, i),
proc_macro2::Span::call_site(),
);
{
let field_name = field.ident.as_ref().unwrap();
quote!(#field_name : #binding #ident ,).to_tokens(&mut t);
}
matches.push(BindingInfo {
ident: ident,
field: field,
});
}
t.extend(quote!("}"));
let (mut stream, matches) = fields.iter().enumerate().fold(
(proc_macro2::TokenStream::new(), Vec::new()),
|(mut stream, mut matches), (i, field)| {
let ident: syn::Ident = syn::Ident::new(
&format!("{}_{}", self.binding_name, i),
proc_macro2::Span::call_site(),
);
{
let field_name = field.ident.as_ref().unwrap();
quote!(#field_name : #binding #ident ,).to_tokens(&mut stream);
}
matches.push(BindingInfo {
ident: ident,
field: field,
});

(stream, matches)
},
);

(quote! { { #stream } }, matches)
}
}
};

let mut all_tokens = proc_macro2::TokenStream::new();
name.to_tokens(&mut all_tokens);
all_tokens.extend(stream);

(t, matches)
(all_tokens, matches)
}
}

0 comments on commit 68802fb

Please sign in to comment.