use std::fmt::Display;
#[derive(Clone,Eq,PartialEq,Debug)]
pub enum CompileTimeConstant<T:PartialEq> {
Literal(T),
Identifier(String),
Substituted(T,String)
}
impl<T:PartialEq> CompileTimeConstant<T> {
pub fn substitute(&mut self, value: T) {
match self {
CompileTimeConstant::Identifier(old_name) => {
let mut tmp = String::new();
std::mem::swap(&mut tmp, old_name);
let new = CompileTimeConstant::Substituted(value, tmp);
*self = new;
}
_ => {}
}
}
}
impl<T:Display+PartialEq> Display for CompileTimeConstant<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CompileTimeConstant::Literal(x) => x.fmt(f),
CompileTimeConstant::Identifier(ident) => f.write_str(&ident),
CompileTimeConstant::Substituted(x, ident) => f.write_fmt(format_args!("{} (substituted from '{}')", x, ident))
}
}
}
impl<T:PartialEq> From<T> for CompileTimeConstant<T> {
fn from(val: T) -> Self {
CompileTimeConstant::Literal(val)
}
}