← All Workshops

MudEngine Part 3: File-Based World Loading

Update main

The main function changes slightly: instead of World::new(), we call World::from_file("game.toml"). Everything else — the REPL loop, command parsing, and output — is unchanged from Part 2.

mud-engine-repl/src/main.rs
fn main() {
    let mut world = match World::from_file("game.toml") {
        Ok(w) => w,
        Err(e) => {
            eprintln!("Error: {}", e);
            std::process::exit(1);
        }
    };

    println!("=== MudEngine REPL ===\n");
    println!("World loaded from game.toml\n");
    println!("{}\n", world.look());

    loop {
        print!("> ");
        io::stdout().flush().unwrap();

        let mut input = String::new();
        if io::stdin().read_line(&mut input).is_err() {
            break;
        }

        let input = input.trim().to_lowercase();
        if input.is_empty() {
            continue;
        }

        let parts: Vec<&str> = input.splitn(2, ' ').collect();
        match parts[0] {
            "quit" | "exit" => {
                println!("Farewell, adventurer!");
                break;
            }
            "look" | "l" => println!("{}", world.look()),
            "north" | "n" | "south" | "s" | "east" | "e" | "west" | "w" => {
                let dir = match parts[0] {
                    "n" => "north",
                    "s" => "south",
                    "e" => "east",
                    "w" => "west",
                    d => d,
                };
                println!("{}", world.go(dir));
            }
            "help" | "?" => {
                println!("Commands:");
                println!("  look / l            — describe the current room");
                println!("  north/n  south/s");
                println!("  east/e   west/w     — move in a direction");
                println!("  help / ?            — show this help");
                println!("  quit / exit         — leave the game");
            }
            _ => println!("Unknown command. Type 'help' or '?' for a list."),
        }
    }
}
💡 Error handling

We use eprintln! + std::process::exit(1) instead of .unwrap() so the user gets a clear error message if the file is missing or malformed. Try deleting game.toml or adding invalid TOML — the error tells you exactly what went wrong.