Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement std::fmt::Debug for Map manually #305

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) struct MapTilesetGid {
}

/// All Tiled map files will be parsed into this. Holds all the layers and tilesets.
#[derive(PartialEq, Clone, Debug)]
#[derive(PartialEq, Clone)]
pub struct Map {
version: String,
/// The way tiles are laid out in the map.
Expand Down Expand Up @@ -65,6 +65,27 @@ pub struct Map {
pub user_type: Option<String>,
}

impl fmt::Debug for Map {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Map")
.field("version", &self.version)
.field("orientation", &self.orientation)
.field("width", &self.width)
.field("height", &self.height)
.field("tile_width", &self.tile_width)
.field("tile_height", &self.tile_height)
.field("stagger_axis", &self.stagger_axis)
.field("stagger_index", &self.stagger_index)
.field("tilesets", &format!("{} tilesets", self.tilesets.len()))
.field("layers", &format!("{} layers", self.layers.len()))
Comment on lines +79 to +80
Copy link
Contributor Author

@erayerdin erayerdin Aug 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of debug-printing all tilesets and layers, we only debug-print the length of self.tilesets and self.layers.

I don't know if this is a viable approach as it calls len and allocates String with format!, which would be not as performant as desired. On the other hand, we're talking about Debug here, which should/must be only relied upon debug builds, not release builds (only logs maybe).

.field("properties", &self.properties)
.field("background_color", &self.background_color)
.field("infinite", &self.infinite)
.field("user_type", &self.user_type)
.finish()
}
}

impl Map {
/// The TMX format version this map was saved to. Equivalent to the map file's `version`
/// attribute.
Expand Down
Loading