← All Workshops

MudEngine Part 3: File-Based World Loading

Update the World model

Replace the hardcoded World::new() with a World::from_file(path) method that deserializes the TOML file.

We define serde-compatible structs for the TOML format — TomlRoom includes a numeric id field, and each exit's destination is a room id. The runtime Room and World from Part 2 stay exactly as they were (the HashMap edition — no id field on Room, keys in HashMap<usize, Room>). The loader's job is to map the file's rows onto that runtime shape: the file's id becomes the key in World.rooms, and each exit becomes a (direction, destination) pair in Room.exits.

mud-engine-repl/src/main.rs
use std::collections::HashMap;
use std::io::{self, Write};
use serde::Deserialize;

#[derive(Deserialize)]
struct TomlRoom {
    id: usize,
    name: String,
    description: String,
    exits: Vec<TomlExit>,
}

#[derive(Deserialize)]
struct TomlExit {
    direction: String,
    destination: usize,
}

#[derive(Deserialize)]
struct TomlWorld {
    rooms: Vec<TomlRoom>,
}
🔍 Room vs. TomlRoom — why two structs?

At first glance this looks like duplication — two structs that both describe a room. Could we not just #[derive(Deserialize)] on Room itself and delete TomlRoom? Technically yes, if the shapes matched, and small differences could be bridged with #[serde(rename)] or #[serde(default)]. But they do not match — not even close — and that is exactly why the split is good practice:

  • Disk and memory formats differ. The file stores rooms as an array of tables with an id field and exits as arrays of named tables { direction = "north", destination = 2 }. In memory, World.rooms is a HashMap<usize, Room> — the room id is the map key, not a field on Room — and Room.exits is a HashMap<String, usize>. Serde has no way to reach into the file's id and use it as a map key; the file shape must be read first and then folded into the map.
  • The runtime struct diverges over time. Part 2's HashMap edition already removed id from Room. Later parts swap String for a Direction enum, and grow fields for spawning and players — while Game.toml stays a stable file contract. Coupling the two would make every engine refactor silently change the world-file format.
  • The loader is the natural place to validate and transform — lift each room's id out of the file data and into the map key, and, later, build the additional lookup maps you need for thousands of rooms.

For a handful of rooms a single struct is fine. As soon as on-disk and in-memory shapes diverge — and here they already do — the DTO split is the scalable habit, not bad practice.

🔍 Why not serde try_from here?

Serde has a #[serde(try_from = "TomlRoom")] attribute that runs a TryFrom<TomlRoom> for Room conversion during deserialization. It does not apply here for one reason: the id has to become the key of World.rooms, but Room no longer has an id field (Part 2 removed it). Serde cannot reach into a list element and promote one of its fields to a map key — so the hand-written loop is the idiomatic way to lift each id out of the file data and into the key.