MudEngine Part 7: Save & Load
Define Save Data Models
Instead of writing separate TOML mirror types and then hand-mapping them into runtime types, we define one set of reused types and derive serde directly on them. Each type serves three roles at once:
- TOML — the type is the deserialization target for
Game.toml/Players.toml(and the serialization target for writing back) - Runtime — the in-memory value the game logic reads and mutates
- Wire — the type sent to clients in
ServerMessage
We need three shared types plus two thin file wrappers:
Exit— a single{ direction, destination }pair. It replaces the raw(String, usize)tuple everywhere, because a named struct deserializes cleanly from TOML's array-of-inline-tables exitsRoomInfo— a room. This is the single room type:Game.tomldeserializes into it,GameState.roomsholds it at runtime, and it is sent to the client so it can render room names without hardcoded staticsPlayerInfo— a connected player. It is the target forPlayers.toml, the value inGameState.players, and the wire type
There are no TOML mirror types for exits, rooms, or players, and (crucially) no conversion code — serde maps the file straight onto the same structs the game uses.
The TomlWorld and TomlPlayers wrappers stay: TOML requires a top-level table to hold an array of rooms and an array of players. They are the only scaffolding left.
All of these go into the new src/game/persistence.rs module. RoomInfo and PlayerInfo are shared with the client (they appear in ServerMessage::State), so both need to be re-exported from src/game/messages.rs — we'll do that in the next step.
use std::collections::HashMap; use serde::{Deserialize, Serialize}; // ── Shared models: one type per concept, reused for TOML, runtime, and wire ── /// A single exit — matches the `{ direction, destination }` entries in Game.toml, /// lives on every runtime room, and is sent to clients for the world grid. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct Exit { pub direction: String, pub destination: usize, } /// Room info sent to clients — and the same type Game.toml deserializes into. /// Carries id, name, description, and the exits the WorldGrid uses to highlight /// which cells are clickable. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct RoomInfo { pub id: usize, pub name: String, pub description: String, pub exits: Vec<Exit>, } /// A connected player — the type Players.toml deserializes into, the value in /// GameState.players, and the wire type broadcast to clients. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct PlayerInfo { pub id: String, pub name: String, pub room: usize, } // ── TOML wrappers: thin containers for the file shape only ── #[derive(Serialize, Deserialize)] struct TomlWorld { rooms: Vec<RoomInfo>, } #[derive(Serialize, Deserialize)] struct TomlPlayers { #[serde(default)] players: Vec<PlayerInfo>, } // ── Runtime game state (server only) ── #[cfg(feature = "server")] pub struct GameState { pub rooms: Vec<RoomInfo>, pub players: HashMap<String, PlayerInfo>, }
Earlier versions of this game kept two parallel type trees — TomlRoom/TomlExit/TomlPlayer mirroring the exact file structure, and separate runtime types (RoomData/GameState). Every file read had to hand-copy fields from the TOML type into the runtime type, and every write had to copy them back. That is pure boilerplate: the two type trees described the same data in two places.
Here we reuse a single type per concept. Serde's derives map the file fields onto the same structs the game logic uses, so there is zero conversion code in load_world(), load_players(), and the save path.
What still differs is the shape of the two files, not the leaf types:
TomlWorld { rooms }andTomlPlayers { players }exist only so TOML has a top-level table to hang an array off of. The array elements are plainRoomInfo/PlayerInfo.GameStatemerges rooms + players (and later live state) into one in-memory object and indexes players by id in aHashMap.RoomInfois read once from Game.toml and reused at runtime — nothing copies room fields around.
The one real naming tension: RoomInfo is called "info sent to clients", yet it is also the runtime room and the TOML target. That is the point — it is the single room type, there is no second one to keep in sync.
The structs now live in their own module file. In main.rs everything shared one module scope, so private fields were visible everywhere. From persistence.rs, sibling modules (server.rs, messages.rs, the components) can only touch public items:
RoomInfoandPlayerInfo— re-exported bymessages.rs(pub use super::persistence::{...};) so the client components can import them;PlayerInfois used to typeGameState.playersand the wire messagesExit,RoomInfo,GameState— read byserver.rs(state.rooms,state.players,r.id…)
The thin TOML wrappers (TomlWorld, TomlPlayers) stay private — they are an implementation detail, only used inside persistence.rs.