← All Workshops

MudEngine Part 3: File-Based World Loading

File loader

Read the Game.toml as World State

mud-engine-repl/src/main.rs
impl World {
    fn from_file(path: &str) -> Self {
        let content = std::fs::read_to_string(path)
            .expect("Failed to read Game.toml");
        let toml_world: TomlWorld = toml::from_str(&content)
            .expect("Failed to parse Game.toml");

        let rooms: HashMap<usize, Room> = toml_world
            .rooms
            .into_iter()
            .map(|r| {
                (
                    r.id,
                    Room {
                        name: r.name,
                        description: r.description,
                        exits: r
                            .exits
                            .into_iter()
                            .map(|e| (e.direction, e.destination))
                            .collect(),
                    },
                )
            })
            .collect();

        World { rooms, player_room: 1 }
    }
}
🔍 How room lookup works

Since World.rooms is a HashMap<usize, Room> (keyed by the file's id), the look and go methods from Part 2 need no changes — they already do O(1) lookups without any iter().find() scan:

let room = &self.rooms[&self.player_room];

The loader builds this map once at startup: each room's id from the file becomes a key, and its exits are collected into HashMap<String, usize> for O(1) direction lookups. The linear scans we removed in Part 2 stay removed — the file loader produces the exact same runtime shape the hardcoded World::new() used to.