use proc_macro2::TokenStream;
use quote::{format_ident, ToTokens, TokenStreamExt};
use std::fmt;
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Namespace {
last: usize,
}
impl Namespace {
pub fn init() -> Self {
Namespace { last: 0 }
}
pub fn new_ident(&mut self) -> Position {
let pos = Position::Out(self.last);
self.last += 1;
pos
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
pub enum Position {
Arg(usize),
Out(usize),
}
impl fmt::Debug for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Position::Arg(n) => write!(f, "arg{}", n),
Position::Out(n) => write!(f, "out{}", n),
}
}
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl ToTokens for Position {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Position::Arg(n) => tokens.append(format_ident!("arg{}", n)),
Position::Out(n) => tokens.append(format_ident!("out{}", n)),
}
}
}