1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::fmt::{Display, Formatter, Result as FmtResult};
use ast::{Decl, Expr};
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum PrintStyle {
AST,
CST,
}
macro_rules! display_type {
($name:ident, $ty:ident) => {
struct $name<'a, Aux: 'a>(&'a $ty<Aux>, PrintStyle);
impl<'a, Aux: 'a> Display for $name<'a, Aux> {
fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
match self.1 {
PrintStyle::AST => Display::fmt(self.0, fmt),
PrintStyle::CST => Display::fmt(&self.0.to_cst(), fmt),
}
}
}
impl<Aux> $ty<Aux> {
pub fn display_as<'a>(&'a self, style: PrintStyle) -> impl 'a + Display {
$name(self, style)
}
}
};
}
display_type!(DisplayDecl, Decl);
display_type!(DisplayExpr, Expr);