Customizing the debug
By default, the #[term]
macro will generate a Debug
impl that is guided by the #[grammar]
attributes on your type (see the parsing section for more details). But sometimes you want to generate custom logic. You can include a #[customize(debug)]
declaration to allow that. Most of the type, when you do this, you will also want to customize parsing, as the RigidTy
does:
#![allow(unused)] fn main() { #[term((rigid $name $*parameters))] #[customize(parse, debug)] pub struct RigidTy { pub name: RigidName, pub parameters: Parameters, } }
Now you must simply implement Debug
in the usual way. Here is the RigidTy
declaration:
#![allow(unused)] fn main() { impl Debug for RigidTy { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let RigidTy { name, parameters } = self; match name { RigidName::AdtId(name) => { write!( f, "{:?}{:?}", name, PrettyParameters::new("<", ">", parameters) ) } RigidName::ScalarId(s) if parameters.is_empty() => { write!(f, "{:?}", s) } RigidName::Ref(RefKind::Shared) if parameters.len() == 2 => { write!(f, "&{:?} {:?}", parameters[0], parameters[1]) } RigidName::Ref(RefKind::Mut) if parameters.len() == 2 => { write!(f, "&mut {:?} {:?}", parameters[0], parameters[1]) } RigidName::Tuple(arity) if parameters.len() == *arity => { if *arity != 0 { write!(f, "{:?}", PrettyParameters::new("(", ")", parameters)) } else { // PrettyParameters would skip the separators // for 0 arity write!(f, "()") } } _ => { write!(f, "{:?}{:?}", name, PrettyParameters::angle(parameters)) } } } } }