use core::fmt;
use crcbl_reflect::{Kind, Reflect, Value};
use super::{DebugModule, DebugSection};
pub const REFLECTED_DECIMALS: usize = 3;
const VARIANT_LABEL: &str = "variant";
const PATH_SEPARATOR: char = '.';
#[derive(Clone, Copy)]
pub struct ReflectedSection<'a> {
pub title: &'a str,
pub value: &'a dyn Reflect,
}
impl fmt::Debug for ReflectedSection<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ReflectedSection")
.field("title", &self.title)
.field("type", &self.value.type_name())
.finish()
}
}
impl DebugModule for ReflectedSection<'_> {
fn debug_section(&self, out: &mut DebugSection) {
out.set_title(self.title);
let mut label = String::new();
rows(out, &mut label, self.value);
}
}
fn rows(out: &mut DebugSection, label: &mut String, value: &dyn Reflect) {
match value.kind() {
Kind::Leaf(_) => {
let shown = if label.is_empty() {
value.type_name()
} else {
label.as_str()
};
out.row(shown, format_args!("{}", Leaf(value)));
}
Kind::Struct => fields(out, label, value),
Kind::Enum => {
let shown = if label.is_empty() {
VARIANT_LABEL
} else {
label.as_str()
};
out.row_str(shown, value.variant().unwrap_or_default());
fields(out, label, value);
}
Kind::List { len } => {
let elements = (0..len).filter_map(|index| value.field(index));
if elements
.clone()
.all(|element| matches!(element.kind(), Kind::Leaf(_)))
{
let shown = if label.is_empty() {
value.type_name()
} else {
label.as_str()
};
out.row(shown, format_args!("{}", Joined(value, len)));
} else {
for (index, element) in elements.enumerate() {
nested(out, label, format_args!("{index}"), element);
}
}
}
}
}
fn fields(out: &mut DebugSection, label: &mut String, value: &dyn Reflect) {
for (index, field) in value.fields().iter().enumerate() {
if let Some(child) = value.field(index) {
nested(out, label, format_args!("{}", field.label), child);
}
}
}
fn nested(
out: &mut DebugSection,
label: &mut String,
segment: fmt::Arguments<'_>,
child: &dyn Reflect,
) {
let kept = label.len();
if kept > 0 {
label.push(PATH_SEPARATOR);
}
let _ = fmt::Write::write_fmt(label, segment);
rows(out, label, child);
label.truncate(kept);
}
struct Leaf<'a>(&'a dyn Reflect);
impl fmt::Display for Leaf<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0.get() {
Some(Value::Float(value)) => write!(f, "{value:.REFLECTED_DECIMALS$}"),
Some(value) => write!(f, "{value}"),
None => write!(f, "<{} has no value>", self.0.type_name()),
}
}
}
struct Joined<'a>(&'a dyn Reflect, usize);
impl fmt::Display for Joined<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self(list, len) = *self;
for (index, element) in (0..len).filter_map(|index| list.field(index)).enumerate() {
if index > 0 {
f.write_str(", ")?;
}
write!(f, "{}", Leaf(element))?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use glam::Vec2;
use super::*;
use crate::debug::DebugOverlay;
use crate::draw_list::{DrawCommand, DrawList};
use crate::text::FontAtlas;
#[derive(Reflect)]
enum Mode {
Idle,
Chasing { speed: f32 },
}
#[derive(Reflect)]
struct Offset {
x: f64,
}
#[derive(Reflect)]
struct Guard {
#[reflect(name = "Hit points")]
hit_points: i32,
position: [f32; 3],
offset: Offset,
mode: Mode,
waypoints: [Offset; 2],
awake: bool,
}
fn guard() -> Guard {
Guard {
hit_points: 12,
position: [1.0, 0.3, -2.5],
offset: Offset { x: 0.125 },
mode: Mode::Chasing { speed: 4.5 },
waypoints: [Offset { x: 1.0 }, Offset { x: 2.0 }],
awake: true,
}
}
fn rows_of(title: &str, value: &dyn Reflect) -> Vec<(String, String)> {
let mut section = DebugSection::default();
ReflectedSection { title, value }.debug_section(&mut section);
assert_eq!(section.title(), title);
section
.rows()
.iter()
.map(|row| (row.label.clone(), row.value.clone()))
.collect()
}
fn pairs(expected: &[(&str, &str)]) -> Vec<(String, String)> {
expected
.iter()
.map(|&(label, value)| (label.to_owned(), value.to_owned()))
.collect()
}
#[test]
fn a_struct_becomes_a_row_per_leaf_labelled_by_its_path() {
assert_eq!(
rows_of("guards", &guard()),
pairs(&[
("Hit points", "12"),
("position", "1.000, 0.300, -2.500"),
("offset.x", "0.125"),
("mode", "Chasing"),
("mode.speed", "4.500"),
("waypoints.0.x", "1.000"),
("waypoints.1.x", "2.000"),
("awake", "true"),
]),
);
}
#[test]
fn a_value_with_no_field_name_is_labelled_by_its_type() {
assert_eq!(rows_of("speed", &2.0f32), pairs(&[("f32", "2.000")]));
assert_eq!(
rows_of("tint", &[0.5f32, 0.25, 1.0]),
pairs(&[("[f32; 3]", "0.500, 0.250, 1.000")]),
);
assert_eq!(rows_of("mode", &Mode::Idle), pairs(&[("variant", "Idle")]));
}
#[test]
fn a_reflected_section_is_drawn_in_the_panel() {
let atlas = FontAtlas::built_in();
let mut overlay = DebugOverlay::with_visible(true);
overlay.begin_frame();
overlay.add(&ReflectedSection {
title: "guards",
value: &guard(),
});
let mut dl = DrawList::new();
overlay.render(&mut dl, Vec2::new(1280.0, 720.0), &atlas);
let drawn: Vec<String> = dl
.commands()
.iter()
.filter_map(|command| match command {
DrawCommand::Text { text, .. } => Some(text.clone()),
_ => None,
})
.collect();
let at = drawn
.iter()
.position(|text| text == "guards")
.unwrap_or_else(|| panic!("the section's title: {drawn:?}"));
assert_eq!(
drawn[at + 1..at + 5],
["Hit points", "12", "position", "1.000, 0.300, -2.500"],
"each label is followed by its value: {drawn:?}",
);
}
}