diff options
author | HampusM <hampus@hampusmat.com> | 2023-03-07 20:30:13 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-03-07 20:30:13 +0100 |
commit | e30456097784ac4d443c89eb9a0b0a1da40fa987 (patch) | |
tree | ffde0de7961f394a7fbe4528cc3c0659cecc8cf2 /src/token_stream.rs | |
parent | 248f3dec0275f5fb673c38d999c4f7961aded188 (diff) |
refactor: improve recurse_replace_ident code style
Diffstat (limited to 'src/token_stream.rs')
-rw-r--r-- | src/token_stream.rs | 59 |
1 files changed, 33 insertions, 26 deletions
diff --git a/src/token_stream.rs b/src/token_stream.rs index 13df5ba..99e8ca5 100644 --- a/src/token_stream.rs +++ b/src/token_stream.rs @@ -34,37 +34,40 @@ fn recurse_replace_ident( token_stream .into_iter() .windows() - .filter_map(|(token_tree, next_token_tree)| match token_tree { - TokenTree::Punct(punct) if punct.as_char() == prefix => { - if matches!(next_token_tree, Some(TokenTree::Ident(ident)) if &ident == target_ident) { - prev_token_was_prefix = true; - - None - } - else { - prev_token_was_prefix = false; + .filter_map(|(token_tree, next_token_tree)| { + let output = match token_tree { + TokenTree::Punct(punct) if punct.as_char() == prefix => { + if matches!( + next_token_tree, + Some(TokenTree::Ident(ident)) if &ident == target_ident + ) { + prev_token_was_prefix = true; + + return None; + } Some(TokenTree::Punct(punct)) } - } - TokenTree::Ident(ident) if prev_token_was_prefix && &ident == target_ident => { - prev_token_was_prefix = false; - - Some(substitution.clone()) - } - TokenTree::Group(group) => { - prev_token_was_prefix = false; - - Some(TokenTree::Group(Group::new( + TokenTree::Ident(ident) + if prev_token_was_prefix && &ident == target_ident => + { + Some(substitution.clone()) + } + TokenTree::Group(group) => Some(TokenTree::Group(Group::new( group.delimiter(), - recurse_replace_ident(group.stream(), target_ident, substitution, prefix), - ))) - } - tt => { - prev_token_was_prefix = false; + recurse_replace_ident( + group.stream(), + target_ident, + substitution, + prefix, + ), + ))), + tt => Some(tt), + }; - Some(tt) - } + prev_token_was_prefix = false; + + output }) .collect() } @@ -120,6 +123,8 @@ mod tests assert_eq!( quote! { let abc = (hello, "123").iter_map(|_| #ht xyz); + + fn #ht hello() {} } .replace_ident( &format_ident!("xyz"), @@ -128,6 +133,8 @@ mod tests .to_string(), quote! { let abc = (hello, "123").iter_map(|_| foo); + + fn #ht hello() {} } .to_string() ); |