This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fs; | |
use std::collections::HashMap; | |
use serde::Deserialize; | |
use std::convert::TryFrom; | |
use std::fmt; | |
#[derive(Deserialize)] | |
struct Room { | |
name: String, | |
description: String, | |
connections: HashMap<Dir, String>, | |
items: Vec<Item> | |
} | |
#[derive(Deserialize)] | |
struct Item { | |
name: String, | |
description: String | |
} | |
#[derive(Debug, Deserialize, Hash, Eq, PartialEq, Copy, Clone)] | |
#[serde(try_from = "String")] | |
enum Dir { North, South, East, West, Northeast, Southeast, Northwest, Southwest, Up, Down } | |
struct InvalidDirError; | |
impl fmt::Display for InvalidDirError { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
f.write_str("InvalidDirError") | |
} | |
} | |
impl TryFrom<String> for Dir { | |
type Error = InvalidDirError; | |
fn try_from(s: String) -> Result<Self, Self::Error> { | |
match s.as_str() { | |
"North" => Ok(Self::North), | |
"South" => Ok(Self::South), | |
"East" => Ok(Self::East), | |
"West" => Ok(Self::West), | |
"Northeast" => Ok(Self::Northeast), | |
"Southeast" => Ok(Self::Southeast), | |
"Northwest" => Ok(Self::Northwest), | |
"Southwest" => Ok(Self::Southwest), | |
"Up" => Ok(Self::Up), | |
"Down" => Ok(Self::Down), | |
_ => Err(InvalidDirError), | |
} | |
} | |
} | |
#[derive(Deserialize)] | |
struct RoomsFile { rooms: Vec<Room> } | |
fn main() { | |
let rooms = fs::read_to_string("rooms.toml").expect("Failed to read rooms.toml"); | |
let rooms_file: RoomsFile = toml::from_str(rooms.as_str()).expect("Couldn't parse rooms.toml"); | |
let rooms: Vec<Room> = rooms_file.rooms; | |
for room in rooms { | |
println!("{}", room.name); | |
println!("{}\n", room.description); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[rooms]] | |
name = "Foyer" | |
description = """The grand foyer of the mansion. A staircase goes up to the | |
balcony and exits to the east and west lead to the drawing room and dining | |
room respectively.""" | |
connections.East = "Drawing Room" | |
connections.West = "Dining Room" | |
connections.Up = "Balcony" | |
items = [] | |
[[rooms]] | |
name = "Dining Room" | |
description = """An elegant dining room with a table that would seat 10 guests. | |
A door leads east to the main foyer, and another leads north to the kitchen.""" | |
connections.North = "Kitchen" | |
connections.East = "Foyer" | |
items = [] | |
[[rooms]] | |
name = "Kitchen" | |
description = """This kitchen is dusty and unused, showing empty spots where | |
appliances and utensils once stood. An exit to the south leads to the dining room""" | |
connections.South = "Dining Room" | |
items = [] | |
[[rooms]] | |
name = "Drawing Room" | |
description = """This drawing room once entertained the guests after dinner with | |
amusements such as its billiard table, piano, and bookshelves. A doorway leads west | |
to the main foyer.""" | |
connections.West = "Foyer" | |
items = [] | |
[[rooms]] | |
name = "Balcony" | |
description = """From the balcony you can look down on the foyer and its dusty crystal | |
chandelier. A starcase would let you descend to the foyer.""" | |
connections.Down = "Foyer" | |
items = [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment