From 4ed4eb46b4e59c0515da6cfa37fdbd7ddcb9fa6c Mon Sep 17 00:00:00 2001 From: Chris Mounce Date: Fri, 20 Mar 2026 19:02:23 -0700 Subject: [PATCH] Clean up interface for standalone board parsing --- src/world.rs | 32 +++++++++++++++++++------------- tests/fixtures/title.brd | Bin 0 -> 348 bytes 2 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 tests/fixtures/title.brd diff --git a/src/world.rs b/src/world.rs index 6ac1af8..c630f12 100644 --- a/src/world.rs +++ b/src/world.rs @@ -125,7 +125,7 @@ pub struct Keys { /// /// Boards mainly exist within a containing [`World`]. But as part of a editing workflow, they can /// also be packaged as standalone .BRD files, which use the same format; use -/// [`Board::from_brd_bytes`] to parse one. +/// [`Board::from_bytes`] to parse one. #[derive(Clone)] pub struct Board { /// The board's title. @@ -505,7 +505,7 @@ impl Board { self.tiles[(y - 1) * 60 + (x - 1)] = tile; } - /// Parse a board from bytes (including the 2-byte size header). + /// Parse a ZZT board from bytes. pub fn from_bytes(bytes: &[u8]) -> Result { // Ignore length bytes let (input, _) = le_u16.parse(bytes)?; @@ -569,11 +569,6 @@ impl Board { }) } - /// Parse a standalone .brd file (same format, no world header). - pub fn from_brd_bytes(bytes: &[u8]) -> Result { - Board::from_bytes(bytes) - } - /// Serialize this board to bytes. pub fn to_bytes(&self) -> Result, EncodeError> { let mut result = vec![]; @@ -764,22 +759,33 @@ mod tests { use super::*; use insta::assert_debug_snapshot; - const BYTES: &[u8] = include_bytes!("../tests/fixtures/all.zzt"); + const WORLD: &[u8] = include_bytes!("../tests/fixtures/all.zzt"); + const BOARD: &[u8] = include_bytes!("../tests/fixtures/title.brd"); #[test] fn roundtrip() { - let world = World::from_bytes(BYTES).unwrap(); + let world = World::from_bytes(WORLD).unwrap(); let encoded = world.to_bytes().unwrap(); - assert_eq!(BYTES, &encoded[..]); + assert_eq!(WORLD, &encoded[..]); } #[test] fn decode_snapshot() { - let world = World::from_bytes(BYTES).unwrap(); + let world = World::from_bytes(WORLD).unwrap(); assert_debug_snapshot!(world); } - /// Format board terrain as two hex grids (elements, then colors). + #[test] + fn brd_roundtrip() { + // We don't bother with a snapshot test for the individual board case, because the world + // snapshot already exercises the contents of boards. This test is just to make sure it's + // *possible* to read/write standalone board files. + let board = Board::from_bytes(BOARD).unwrap(); + let encoded = board.to_bytes().unwrap(); + assert_eq!(BOARD, &encoded[..]); + } + + /// Test helper: format board terrain as two hex grids (elements, then colors). fn format_tiles(board: &Board) -> String { let mut out = String::new(); for y in 1..=25 { @@ -801,7 +807,7 @@ mod tests { #[test] fn tiles_snapshot() { - let world = World::from_bytes(BYTES).unwrap(); + let world = World::from_bytes(WORLD).unwrap(); let mut all_tiles = String::new(); for (i, board) in world.boards.iter().enumerate() { all_tiles.push_str(&format!("// board {}: {}\n", i, board.name)); diff --git a/tests/fixtures/title.brd b/tests/fixtures/title.brd new file mode 100644 index 0000000000000000000000000000000000000000..986d7f047bdb349cfd2761489f5b1f8138776985 GIT binary patch literal 348 zcmbu2F$#k~6hwD|Wxyk3Z!mozx3Eelx&}dvs1R?juzYen7BjjKN3owBbINSOG-{1&#U=7|4N?1%2ufW!PLB-3R literal 0 HcmV?d00001