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
|
use std::fmt;
use std::str;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub enum Ship {
Battleship,
Carrier,
Cruiser,
Destroyer,
Submarine
}
impl fmt::Display for Ship {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Ship::*;
f.write_str(
match self {
&Battleship => "Battleship",
&Carrier => "Carrier",
&Cruiser => "Cruiser",
&Destroyer => "Destroyer",
&Submarine => "Submarine"
}
)
}
}
impl str::FromStr for Ship {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
use Ship::*;
match s {
"Battleship" => Ok(Battleship),
"Carrier" => Ok(Carrier),
"Cruiser" => Ok(Cruiser),
"Destroyer" => Ok(Destroyer),
"Submarine" => Ok(Submarine),
_ => Err(String::from("ship type is not known"))
}
}
}
impl Ship {
pub fn length(&self) -> u16 {
use Ship::*;
match self {
&Battleship => 4,
&Carrier => 5,
&Cruiser => 3,
&Destroyer => 2,
&Submarine => 3
}
}
}
|