summaryrefslogtreecommitdiff
path: root/src/ships.rs
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@gmail.com>2017-05-14 21:49:48 +0200
committerJustin Worthe <justin.worthe@gmail.com>2017-05-14 21:49:48 +0200
commit10c8ceb168e86a58e38086691ddd519bac63ff03 (patch)
treea40b433e7cfad492a60b37c5a337758ffe7d1786 /src/ships.rs
parent25a551316e27f4cc52c160d099db9cc3673b3421 (diff)
Added model for knowledge of the game's state
Will be useful to track deductions that have already been made.
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::*;