#![warn(missing_docs)]
use std::{fmt::Display, write};
use quote::{quote, ToTokens};
#[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord)]
pub enum Ident {
Inherit(String),
Synth(String),
Local(String),
Unknown(String)
}
impl ToTokens for Ident {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let new_tokens = match self {
Self::Inherit(s) => quote!(wagon_ident::Ident::Inherit(#s.to_string())),
Self::Synth(s) => quote!(wagon_ident::Ident::Synth(#s.to_string())),
Self::Local(s) => quote!(wagon_ident::Ident::Local(#s.to_string())),
Self::Unknown(s) => quote!(wagon_ident::Ident::Unknown(#s.to_string())),
};
tokens.extend(new_tokens);
}
}
impl Default for Ident {
fn default() -> Self {
Self::Unknown(String::new())
}
}
impl Display for Ident {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Inherit(i) => write!(f, "*{i}"),
Self::Synth(i) => write!(f, "&{i}"),
Self::Local(i) => write!(f, "${i}"),
Self::Unknown(i) => write!(f, "{i}"),
}
}
}
impl Ident {
#[must_use]
pub fn extract_string(&self) -> &str {
match self {
Self::Inherit(s) | Self::Synth(s) | Self::Local(s) | Self::Unknown(s) => s,
}
}
#[must_use]
pub fn to_ident(&self) -> proc_macro2::Ident {
let text = match self {
Self::Inherit(s) => format!("i_{s}"),
Self::Synth(s) => format!("s_{s}"),
Self::Local(s) => format!("l_{s}"),
Self::Unknown(s) => format!("u_{s}"),
};
proc_macro2::Ident::new(&text, proc_macro2::Span::call_site())
}
#[must_use]
pub fn to_synth(&self) -> Self {
Self::Synth(self.extract_string().to_string())
}
}
impl From<&str> for Ident {
fn from(value: &str) -> Self {
Self::Unknown(value.to_string())
}
}