#![allow(
clippy::blocks_in_if_conditions,
clippy::cast_lossless,
clippy::cast_possible_truncation,
clippy::manual_find,
clippy::manual_let_else,
clippy::manual_map,
clippy::map_unwrap_or,
clippy::module_name_repetitions,
clippy::needless_pass_by_value,
clippy::option_if_let_else,
clippy::range_plus_one,
clippy::single_match_else,
clippy::struct_field_names,
clippy::too_many_lines,
clippy::wrong_self_convention
)]
extern crate proc_macro;
use proc_macro::{token_stream, Group, Ident, TokenStream, TokenTree};
#[proc_macro]
pub fn replace_ident(input: TokenStream) -> TokenStream {
let mut it = input.into_iter();
let target = get_ident(&mut it);
let _comma = it.next().unwrap();
let replace = get_ident(&mut it);
let _comma = it.next().unwrap();
replace_ident2(target, replace, it)
}
fn replace_ident2(target: Ident, replace: Ident, input: token_stream::IntoIter) -> TokenStream {
input
.map(|tt| {
match tt {
TokenTree::Ident(ref i) if i.to_string() == target.to_string() => {
TokenTree::Ident(replace.clone())
}
TokenTree::Group(group) => TokenTree::Group(Group::new(
group.delimiter(),
replace_ident2(target.clone(), replace.clone(), group.stream().into_iter()),
)),
other => other,
}
})
.collect()
}
fn get_ident(it: &mut token_stream::IntoIter) -> Ident {
match it.next() {
Some(TokenTree::Ident(i)) => i,
_ => panic!("Expected identifier"),
}
}