|
| 1 | +//! A whole load of custom debug impls to keep the output more succinct |
| 2 | +//! |
| 3 | +//! Mostly to reduce noise for snapshot tests, but also good in general |
| 4 | +
|
| 5 | +use std::fmt; |
| 6 | + |
| 7 | +use crate::{positioner::Spacer, text::Text}; |
| 8 | + |
| 9 | +pub struct DebugInlineMaybeF32Color<'a>(pub &'a Option<[f32; 4]>); |
| 10 | + |
| 11 | +impl fmt::Debug for DebugInlineMaybeF32Color<'_> { |
| 12 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 13 | + match self.0 { |
| 14 | + None => f.write_str("None"), |
| 15 | + Some(rgba) => f.write_fmt(format_args!("Some({:?})", DebugF32Color(*rgba))), |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +pub struct DebugF32Color(pub [f32; 4]); |
| 21 | + |
| 22 | +impl fmt::Debug for DebugF32Color { |
| 23 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 24 | + if self.0 == [0.0, 0.0, 0.0, 1.0] { |
| 25 | + f.write_str("Color(BLACK)") |
| 26 | + } else { |
| 27 | + let Self([r, g, b, a]) = self; |
| 28 | + |
| 29 | + if *a == 1.0 { |
| 30 | + f.write_fmt(format_args!("Color {{ r: {r:.2}, g: {g:.2}, b: {b:.2} }}")) |
| 31 | + } else { |
| 32 | + f.write_fmt(format_args!( |
| 33 | + "Color {{ r: {r:.2}, g: {g:.2}, b: {b:.2}, a: {a:.2} }}" |
| 34 | + )) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +pub struct DebugInline<'inner, T>(pub &'inner T); |
| 41 | + |
| 42 | +impl<T: fmt::Debug> fmt::Debug for DebugInline<'_, T> { |
| 43 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 44 | + f.write_fmt(format_args!("{:?}", self.0)) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +fn debug_inline_some<T: fmt::Debug>( |
| 49 | + debug: &mut fmt::DebugStruct<'_, '_>, |
| 50 | + name: &'static str, |
| 51 | + maybe_t: &Option<T>, |
| 52 | +) { |
| 53 | + if maybe_t.is_some() { |
| 54 | + debug.field(name, &DebugInline(maybe_t)); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +pub struct DebugBytesPrefix<'a>(pub &'a [u8]); |
| 59 | + |
| 60 | +impl<'a> fmt::Debug for DebugBytesPrefix<'a> { |
| 61 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 62 | + match self.0 { |
| 63 | + [x, y, z, _, ..] => { |
| 64 | + let len = self.0.len(); |
| 65 | + f.write_fmt(format_args!("{{ len: {len}, data: [{x}, {y}, {z}, ..] }}")) |
| 66 | + } |
| 67 | + three_or_less => f.write_fmt(format_args!("{three_or_less:?}")), |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +pub fn text(text: &Text, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 73 | + #[derive(Copy, Clone)] |
| 74 | + struct StyleWrapper { |
| 75 | + is_bold: bool, |
| 76 | + is_italic: bool, |
| 77 | + is_underlined: bool, |
| 78 | + is_striked: bool, |
| 79 | + } |
| 80 | + |
| 81 | + impl StyleWrapper { |
| 82 | + fn is_regular(self) -> bool { |
| 83 | + let Self { |
| 84 | + is_bold, |
| 85 | + is_italic, |
| 86 | + is_underlined, |
| 87 | + is_striked, |
| 88 | + } = self; |
| 89 | + |
| 90 | + ![is_bold, is_italic, is_underlined, is_striked].contains(&true) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + impl fmt::Debug for StyleWrapper { |
| 95 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 96 | + let Self { |
| 97 | + is_bold, |
| 98 | + is_italic, |
| 99 | + is_underlined, |
| 100 | + is_striked, |
| 101 | + } = *self; |
| 102 | + |
| 103 | + if self.is_regular() { |
| 104 | + f.write_str("REGULAR")?; |
| 105 | + } else { |
| 106 | + if is_bold { |
| 107 | + f.write_str("BOLD ")?; |
| 108 | + } |
| 109 | + if is_italic { |
| 110 | + f.write_str("ITALIC ")?; |
| 111 | + } |
| 112 | + if is_underlined { |
| 113 | + f.write_str("UNDERLINED ")?; |
| 114 | + } |
| 115 | + if is_striked { |
| 116 | + f.write_str("STRIKED ")?; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + Ok(()) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + let Text { |
| 125 | + text, |
| 126 | + size, |
| 127 | + color, |
| 128 | + link, |
| 129 | + is_bold, |
| 130 | + is_italic, |
| 131 | + is_underlined, |
| 132 | + is_striked, |
| 133 | + font: _, |
| 134 | + // Globally consistent so avoid displaying as noise |
| 135 | + hidpi_scale: _, |
| 136 | + default_color, |
| 137 | + } = text; |
| 138 | + |
| 139 | + let mut debug = f.debug_struct("Text"); |
| 140 | + |
| 141 | + // Fields that we will always display |
| 142 | + debug.field("text", text); |
| 143 | + |
| 144 | + // Fields that we only display when set to unique values |
| 145 | + if *size != 16.0 { |
| 146 | + debug.field("size", size); |
| 147 | + } |
| 148 | + if color.is_none() { |
| 149 | + debug.field("default_color", &DebugF32Color(*default_color)); |
| 150 | + } else { |
| 151 | + let color = color.map(DebugF32Color); |
| 152 | + debug.field("color", &DebugInline(&color)); |
| 153 | + } |
| 154 | + let style = StyleWrapper { |
| 155 | + is_bold: *is_bold, |
| 156 | + is_italic: *is_italic, |
| 157 | + is_underlined: *is_underlined, |
| 158 | + is_striked: *is_striked, |
| 159 | + }; |
| 160 | + if !style.is_regular() { |
| 161 | + debug.field("style", &style); |
| 162 | + } |
| 163 | + debug_inline_some(&mut debug, "link", link); |
| 164 | + |
| 165 | + debug.finish_non_exhaustive() |
| 166 | +} |
| 167 | + |
| 168 | +pub fn spacer(spacer: &Spacer, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 169 | + let Spacer { space, visible } = spacer; |
| 170 | + |
| 171 | + if *visible { |
| 172 | + f.write_fmt(format_args!("VisibleSpacer({space})")) |
| 173 | + } else { |
| 174 | + f.write_fmt(format_args!("InvisibleSpacer({space})")) |
| 175 | + } |
| 176 | +} |
0 commit comments