Skip to content

Commit

Permalink
Customize impl Debug for Modifier.
Browse files Browse the repository at this point in the history
This keeps the output shorter by omitting uninformative wrappers.
  • Loading branch information
kpreid committed Aug 19, 2024
1 parent 4406242 commit 1e55bd4
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions all-is-cubes/src/block/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub use zoom::*;
/// exists, so no rules are currently enforceable).
///
/// [`Rotate`]: Self::Rotate
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[non_exhaustive]
pub enum Modifier {
Expand Down Expand Up @@ -79,6 +79,21 @@ pub enum Modifier {
Inventory(inv::Inventory),
}

impl core::fmt::Debug for Modifier {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// Print most modifiers’ data without the enum variant, because their struct names
// are identifying enough.
match self {
Self::Quote(q) => q.fmt(f),
Self::Rotate(r) => write!(f, "Rotate({r:?})"),
Self::Composite(c) => c.fmt(f),
Self::Zoom(z) => z.fmt(f),
Self::Move(m) => m.fmt(f),
Self::Inventory(i) => i.fmt(f),
}
}
}

impl Modifier {
/// Compute the effect of this modifier.
///
Expand Down Expand Up @@ -233,8 +248,10 @@ pub(crate) enum ModifierUnspecialize {
#[cfg(test)]
mod tests {
use super::*;
use crate::block::{BlockAttributes, TickAction};
use crate::block::{BlockCollision, EvaluatedBlock, Evoxel, Primitive, Resolution::R2};
use crate::block::{
BlockAttributes, BlockCollision, EvaluatedBlock, Evoxel, Primitive, Resolution::R2,
TickAction,
};
use crate::content::make_some_voxel_blocks;
use crate::math::{Cube, Face6, FaceMap, GridAab, OpacityCategory, Rgb, Rgba};
use crate::op::Operation;
Expand All @@ -253,6 +270,38 @@ mod tests {
assert_eq!(size_of::<Modifier>(), 3 * size_of::<*const ()>());
}

#[test]
fn modifier_debug() {
let modifiers: Vec<Modifier> = vec![
Modifier::Quote(Quote::new()),
Modifier::Rotate(GridRotation::RXyZ),
Modifier::Composite(Composite::new(block::AIR, CompositeOperator::Over)),
Modifier::Inventory(inv::Inventory::from_slots([])),
];
assert_eq!(
format!("{modifiers:#?}"),
indoc::indoc! {
r"[
Quote {
suppress_ambient: false,
},
Rotate(RXyZ),
Composite {
source: Block {
primitive: Air,
},
operator: Over,
reverse: false,
disassemblable: false,
},
Inventory {
slots: [],
},
]"
}
);
}

#[test]
fn rotate_evaluation() {
let resolution = R2;
Expand Down

0 comments on commit 1e55bd4

Please sign in to comment.