]>
Commit | Line | Data |
---|---|---|
85aaf69f | 1 | use std::fmt::{Debug, Formatter, Error}; |
1a4d82fc JJ |
2 | use std::collections::HashMap; |
3 | ||
4 | trait HasInventory { | |
5 | fn getInventory<'s>(&'s self) -> &'s mut Inventory; | |
6 | fn addToInventory(&self, item: &Item); | |
7 | fn removeFromInventory(&self, itemName: &str) -> bool; | |
8 | } | |
9 | ||
10 | trait TraversesWorld { | |
11 | fn attemptTraverse(&self, room: &Room, directionStr: &str) -> Result<&Room, &str> { | |
12 | let direction = str_to_direction(directionStr); | |
13 | let maybe_room = room.direction_to_room.get(&direction); | |
1a4d82fc JJ |
14 | match maybe_room { |
15 | Some(entry) => Ok(entry), | |
0731742a | 16 | //~^ ERROR lifetime mismatch [E0623] |
1a4d82fc JJ |
17 | _ => Err("Direction does not exist in room.") |
18 | } | |
19 | } | |
20 | } | |
21 | ||
22 | ||
85aaf69f | 23 | #[derive(Debug, Eq, PartialEq, Hash)] |
1a4d82fc JJ |
24 | enum RoomDirection { |
25 | West, | |
26 | East, | |
27 | North, | |
28 | South, | |
29 | Up, | |
30 | Down, | |
31 | In, | |
32 | Out, | |
33 | ||
34 | None | |
35 | } | |
36 | ||
37 | struct Room { | |
38 | description: String, | |
39 | items: Vec<Item>, | |
40 | direction_to_room: HashMap<RoomDirection, Room>, | |
41 | } | |
42 | ||
43 | impl Room { | |
44 | fn new(description: &'static str) -> Room { | |
45 | Room { | |
46 | description: description.to_string(), | |
47 | items: Vec::new(), | |
48 | direction_to_room: HashMap::new() | |
49 | } | |
50 | } | |
51 | ||
52 | fn add_direction(&mut self, direction: RoomDirection, room: Room) { | |
53 | self.direction_to_room.insert(direction, room); | |
54 | } | |
55 | } | |
56 | ||
57 | struct Item { | |
58 | name: String, | |
59 | } | |
60 | ||
61 | struct Inventory { | |
62 | items: Vec<Item>, | |
63 | } | |
64 | ||
65 | impl Inventory { | |
66 | fn new() -> Inventory { | |
67 | Inventory { | |
68 | items: Vec::new() | |
69 | } | |
70 | } | |
71 | } | |
72 | ||
73 | struct Player { | |
74 | name: String, | |
75 | inventory: Inventory, | |
76 | } | |
77 | ||
78 | impl Player { | |
79 | fn new(name: &'static str) -> Player { | |
80 | Player { | |
81 | name: name.to_string(), | |
82 | inventory: Inventory::new() | |
83 | } | |
84 | } | |
85 | } | |
86 | ||
87 | impl TraversesWorld for Player { | |
88 | } | |
89 | ||
85aaf69f | 90 | impl Debug for Player { |
1a4d82fc JJ |
91 | fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> { |
92 | formatter.write_str("Player{ name:"); | |
85aaf69f | 93 | formatter.write_str(&self.name); |
1a4d82fc JJ |
94 | formatter.write_str(" }"); |
95 | Ok(()) | |
96 | } | |
97 | } | |
98 | ||
99 | fn str_to_direction(to_parse: &str) -> RoomDirection { | |
9fa01778 | 100 | match to_parse { |
1a4d82fc JJ |
101 | "w" | "west" => RoomDirection::West, |
102 | "e" | "east" => RoomDirection::East, | |
103 | "n" | "north" => RoomDirection::North, | |
104 | "s" | "south" => RoomDirection::South, | |
105 | "in" => RoomDirection::In, | |
106 | "out" => RoomDirection::Out, | |
107 | "up" => RoomDirection::Up, | |
108 | "down" => RoomDirection::Down, | |
32a655c1 | 109 | _ => None |
1a4d82fc | 110 | } |
dfeec247 | 111 | //~^^ ERROR `match` arms have incompatible types |
1a4d82fc JJ |
112 | } |
113 | ||
114 | fn main() { | |
115 | let mut player = Player::new("Test player"); | |
116 | let mut room = Room::new("A test room"); | |
117 | println!("Made a player: {:?}", player); | |
118 | println!("Direction parse: {:?}", str_to_direction("east")); | |
119 | match player.attemptTraverse(&room, "west") { | |
120 | Ok(_) => println!("Was able to move west"), | |
121 | Err(msg) => println!("Not able to move west: {}", msg) | |
122 | }; | |
123 | } |