Extend serialization support to worlds

This commit is contained in:
2024-03-10 21:09:48 -07:00
parent 2c9bb315ed
commit 7c31089eda
+12 -3
View File
@@ -87,6 +87,14 @@ impl World {
}
Ok(world)
}
fn to_bytes(&self) -> Result<Vec<u8>, Box<dyn Error>> {
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<dyn Error>> {
}
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<dyn Error>> {
}
}
// 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(())
}