|
| 1 | +//! This module implements the `fmt::Debug` trait for the AST types. |
| 2 | +
|
| 3 | +use super::node::*; |
| 4 | +use super::part::*; |
| 5 | +use std::fmt; |
| 6 | + |
| 7 | +macro_rules! impl_debug_for_enum { |
| 8 | + ($enum_name:ident { $($variant:ident),* $(,)? }) => { |
| 9 | + impl fmt::Debug for $enum_name { |
| 10 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 11 | + match self { |
| 12 | + $( |
| 13 | + Self::$variant(v) => { |
| 14 | + if f.alternate() { |
| 15 | + write!(f, "{:#?}", v) |
| 16 | + } else { |
| 17 | + write!(f, "{:?}", v) |
| 18 | + } |
| 19 | + } |
| 20 | + )* |
| 21 | + _ => Ok(()), // Default case for ignored variants |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +impl fmt::Debug for NodeData { |
| 29 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 30 | + write!(f, "NodeData {{id: {:?}, span: {:?}}}", self.id, self.span) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl fmt::Debug for ClassFlags { |
| 35 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 36 | + let flags = [ |
| 37 | + ("partial", self.partial), |
| 38 | + ("encapsulated", self.encapsulated), |
| 39 | + ("replaceable", self.replaceable), |
| 40 | + ("redeclare", self.redeclare), |
| 41 | + ("is_final", self.is_final), |
| 42 | + ("inner", self.inner), |
| 43 | + ("outer", self.outer), |
| 44 | + ] |
| 45 | + .iter() |
| 46 | + .filter_map(|&(flag, is_set)| if is_set { Some(flag) } else { None }) |
| 47 | + .collect::<Vec<_>>(); // Collect names of true flags into a vector |
| 48 | + write!(f, "[{}]", flags.join(", ")) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl fmt::Debug for TypeSpecifier { |
| 53 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 54 | + if self.local { |
| 55 | + write!(f, ".{:?}", self.name) |
| 56 | + } else { |
| 57 | + write!(f, "{:?}", self.name) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl_debug_for_enum!(Equation { |
| 63 | + Connect, |
| 64 | + For, |
| 65 | + If, |
| 66 | + Simple, |
| 67 | +}); |
| 68 | + |
| 69 | +impl_debug_for_enum!(Statement { |
| 70 | + Assignment, |
| 71 | + Break, |
| 72 | + For, |
| 73 | + If, |
| 74 | + Return, |
| 75 | + While, |
| 76 | +}); |
| 77 | + |
| 78 | +impl_debug_for_enum!(Expression { |
| 79 | + Array, |
| 80 | + Binary, |
| 81 | + Boolean, |
| 82 | + FunctionCall, |
| 83 | + If, |
| 84 | + Ref, |
| 85 | + Unary, |
| 86 | + UnsignedInteger, |
| 87 | + UnsignedReal |
| 88 | +}); |
| 89 | + |
| 90 | +impl fmt::Debug for ComponentReference { |
| 91 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 92 | + let parts = self |
| 93 | + .parts |
| 94 | + .iter() |
| 95 | + .map(|part| format!("{:?}", part)) |
| 96 | + .collect::<Vec<_>>() |
| 97 | + .join("."); |
| 98 | + write!( |
| 99 | + f, |
| 100 | + "ComponentReference {{{}{}, id: {}, span: {:?}}}", |
| 101 | + if self.local { "." } else { "" }, |
| 102 | + parts, |
| 103 | + self.node_data.id, |
| 104 | + self.node_data.span, |
| 105 | + ) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl fmt::Debug for RefPart { |
| 110 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 111 | + if self.array_subscripts.is_empty() { |
| 112 | + write!(f, "{:?}", self.name) |
| 113 | + } else { |
| 114 | + write!(f, "{:?}{:?}", self.name, self.array_subscripts) |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl_debug_for_enum!(Subscript { Range, Expression }); |
| 120 | + |
| 121 | +impl_debug_for_enum!(Argument { |
| 122 | + Modification, |
| 123 | + Redeclaration, |
| 124 | + Replaceable |
| 125 | +}); |
| 126 | + |
| 127 | +impl_debug_for_enum!(Modification { Class, Expression }); |
| 128 | + |
| 129 | +impl_debug_for_enum!(ModExpr { Break, Expression }); |
| 130 | + |
| 131 | +impl fmt::Debug for Description { |
| 132 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 133 | + write!(f, "{} {:?}", self.strings.join(" "), self.annotation) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +impl fmt::Debug for ElementFlags { |
| 138 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 139 | + let flags = [ |
| 140 | + ("replaceable", self.replaceable), |
| 141 | + ("redeclare", self.redeclare), |
| 142 | + ("is_final", self.is_final), |
| 143 | + ("inner", self.inner), |
| 144 | + ("outer", self.outer), |
| 145 | + ] |
| 146 | + .iter() |
| 147 | + .filter_map(|&(flag, is_set)| if is_set { Some(flag) } else { None }) |
| 148 | + .collect::<Vec<_>>(); // Collect names of true flags into a vector |
| 149 | + write!(f, "[{}]", flags.join(", ")) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +impl fmt::Debug for DescriptionString { |
| 154 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 155 | + write!(f, "{}", self.parts.join(" ")) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +impl fmt::Debug for Name { |
| 160 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 161 | + write!(f, "{}", self.parts.join(".")) |
| 162 | + } |
| 163 | +} |
0 commit comments