summaryrefslogtreecommitdiff
path: root/src/ships.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/ships.rs')
-rw-r--r--src/ships.rs19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/ships.rs b/src/ships.rs
index e21b20e..344f9ed 100644
--- a/src/ships.rs
+++ b/src/ships.rs
@@ -1,6 +1,7 @@
use std::fmt;
+use std::str;
-#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub enum Ship {
Battleship,
Carrier,
@@ -25,6 +26,22 @@ impl fmt::Display for Ship {
}
}
+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::*;