use proc_macro2::Span;
use quote::{ToTokens, TokenStreamExt};
use std::collections::HashSet;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Idx {
N(u32),
B(u32)
}
impl ToTokens for Idx {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
tokens.append(proc_macro2::Literal::u32_unsuffixed(self.val()))
}
}
impl Idx {
pub fn is_b(self) -> bool {
matches!(self, Idx::B(_))
}
pub fn val(self) -> u32 {
match self {
Idx::N(i) => i,
Idx::B(i) => i
}
}
}
pub fn check_uniq<'a, I>(s: Span, iter: I) -> syn::Result<()>
where
I: IntoIterator<Item = &'a Idx>
{
let mut set = HashSet::new();
let mut ctr = 0;
for u in iter {
set.insert(u.val());
ctr += 1;
}
if ctr != set.len() {
return Err(syn::Error::new(s, "duplicate index numbers"))
}
Ok(())
}