use std::collections::hash_map;
use std::fmt::{self, Display, Write};
use std::hash::{Hash, Hasher};
use syn::Ident;
pub(crate) struct Symbol(u64);
pub(crate) fn hash(ident: &Ident) -> Symbol {
let mut hasher = hash_map::DefaultHasher::new();
ident.hash(&mut hasher);
Symbol(hasher.finish())
}
impl Display for Symbol {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let mut remainder = self.0;
for _ in 0..8 {
let digit = (remainder % 62) as u8;
remainder /= 62;
formatter.write_char(match digit {
0..=25 => b'a' + digit,
26..=51 => b'A' + digit - 26,
52..=61 => b'0' + digit - 52,
_ => unreachable!(),
} as char)?;
}
Ok(())
}
}
#[test]
fn test_hash() {
let ident = Ident::new("EXAMPLE", proc_macro2::Span::call_site());
assert_eq!(hash(&ident).to_string(), "0GPSzIoo");
}