Extend serialization support to worlds
This commit is contained in:
+12
-3
@@ -87,6 +87,14 @@ impl World {
|
|||||||
}
|
}
|
||||||
Ok(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 {
|
impl Board {
|
||||||
@@ -249,7 +257,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let bytes = fs::read(&args[1])?;
|
let bytes = fs::read(&args[1])?;
|
||||||
let world = World::from(&bytes)?;
|
let mut world = World::from(&bytes)?;
|
||||||
|
|
||||||
// Print some of the data parsed.
|
// Print some of the data parsed.
|
||||||
// TODO: Implement some macros, or other code modification
|
// 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
|
// Try to write a modified world file
|
||||||
fs::write("tmp.brd", world.boards[0].to_bytes()?)?;
|
world.boards.reverse();
|
||||||
|
fs::write("tmp.zzt", world.to_bytes()?)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user