use proc_macro2::Literal;
use proc_macro2::TokenStream;
use quote::ToTokens;
use quote::quote;
use rapidhash::rapidhash;
#[derive(Debug, Default)]
pub struct RustyTrackerBuilder {
pub current_index: u32,
}
impl RustyTrackerBuilder {
pub fn next_index_hash(&mut self, tokens: TokenStream) -> (u32, u64) {
let tokens_hash =
rapidhash(tokens.to_string().replace(" ", "").as_bytes());
let index = self.current_index;
self.current_index += 1;
(index, tokens_hash)
}
pub fn next_tracker(&mut self, val: impl ToTokens) -> TokenStream {
let (index, tokens_hash) = self.next_index_hash(val.to_token_stream());
quote! {RustyTracker::new(#index, #tokens_hash)}
}
pub fn next_tracker_ron(&mut self, val: impl ToTokens) -> TokenStream {
let (index, tokens_hash) = self.next_index_hash(val.to_token_stream());
let index = Literal::u32_unsuffixed(index);
let tokens_hash = Literal::u64_unsuffixed(tokens_hash);
quote! {
RustyTracker(
index: #index,
tokens_hash: #tokens_hash
)
}
}
}