1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
use serde_json::Value;
use super::components::Position;
#[derive(Debug, Clone)]
pub enum Direction {
North,
South,
East,
West,
None
}
impl Direction {
fn from_json(val: &Value) -> Option<Direction>{
match val {
Value::String(txt) => match txt.as_str() {
"north" => Some(Direction::North),
"south" => Some(Direction::South),
"east" => Some(Direction::East),
"west"=> Some(Direction::West),
"" => Some(Direction::None),
_ => None
}
Value::Null => Some(Direction::None),
_ => None
}
}
pub fn to_position(&self) -> Position {
match self {
Direction::North => Position::new(0, -1),
Direction::South => Position::new(0, 1),
Direction::East => Position::new(1, 0),
Direction::West => Position::new(-1, 0),
Direction::None => Position::new(0, 0)
}
}
}
#[derive(Debug, Clone)]
pub enum Control {
Move(Direction),
Take(u64)
}
impl Control {
pub fn from_json(val: Value) -> Option<Control>{
if let Value::String(control_type) = &val[0] {
match control_type.as_str() {
"move" => match Direction::from_json(&val[1]) {
Some(dir) => Some(Control::Move(dir)),
None => None
},
"take" => match val[1].as_u64() {
Some(rank) => Some(Control::Take(rank)),
_ => None
}
_ => None
}
} else {None}
}
}
#[derive(Debug, Clone)]
pub enum Action {
Join(String),
Leave(String),
Input(String, Control)
}
|