Skip to content

Commit

Permalink
refactor: simp
Browse files Browse the repository at this point in the history
  • Loading branch information
fzyzcjy committed Mar 7, 2024
1 parent 9c9c22d commit 2ae4307
Showing 1 changed file with 0 additions and 32 deletions.
32 changes: 0 additions & 32 deletions frb_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,10 @@ use proc_macro::*;
#[proc_macro_attribute]
pub fn frb(attribute: TokenStream, item: TokenStream) -> TokenStream {
let mut output = format_frb_attribute(format!("#[frb({attribute})]"));
let item = strip_frb_attr(item);
output.extend(item);
output
}

fn strip_frb_attr(item: TokenStream) -> TokenStream {
item.into_iter()
.scan(None, |pound, tok| {
use TokenTree as TT;
match (&pound, &tok) {
(None, TT::Punct(punct)) if punct.as_char() == '#' => {
*pound = Some(tok);
Some(TokenStream::new())
}
(Some(_), TT::Group(group)) if is_frb_bracket(group) => {
_ = pound.take();
Some(format_frb_attribute(format!("#[{}]", group.stream())))
}
(_, TT::Group(group)) => Some(
[
pound.take(),
Some(TT::Group(Group::new(
group.delimiter(),
strip_frb_attr(group.stream()),
))),
]
.into_iter()
.flatten()
.collect(),
),
_ => Some(tok.into()),
}
})
.collect()
}

fn is_frb_bracket(group: &Group) -> bool {
matches!((group.delimiter(), group.stream().into_iter().next()), (Delimiter::Bracket, Some(TokenTree::Ident(ident))) if ident.to_string() == "frb")
}
Expand Down

0 comments on commit 2ae4307

Please sign in to comment.