From 7c31089edad7dcbc7dfe8eb18d3f9cceeaf55e08 Mon Sep 17 00:00:00 2001 From: Chris Mounce Date: Sun, 10 Mar 2024 21:09:48 -0700 Subject: [PATCH] Extend serialization support to worlds --- src/main.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 41dce4d..41a821e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,6 +87,14 @@ impl World { } Ok(world) } + + fn to_bytes(&self) -> Result, Box> { + let mut result = self.header.clone(); + for board in &self.boards { + result.extend_from_slice(&board.to_bytes()?); + } + Ok(result) + } } impl Board { @@ -249,7 +257,7 @@ fn main() -> Result<(), Box> { } let bytes = fs::read(&args[1])?; - let world = World::from(&bytes)?; + let mut world = World::from(&bytes)?; // Print some of the data parsed. // TODO: Implement some macros, or other code modification @@ -269,8 +277,9 @@ fn main() -> Result<(), Box> { } } - // Try to export one of the boards - fs::write("tmp.brd", world.boards[0].to_bytes()?)?; + // Try to write a modified world file + world.boards.reverse(); + fs::write("tmp.zzt", world.to_bytes()?)?; Ok(()) }