chiark / gitweb /
VisiblePieceId FromStr
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 27 Jun 2020 00:46:53 +0000 (01:46 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 27 Jun 2020 00:46:53 +0000 (01:46 +0100)
src/gamestate.rs
src/imports.rs

index b8d53951af5862e55d2d01a56c2297cc64318ec7..d9a269c165515459b159b319428683a2aed391b4 100644 (file)
@@ -31,6 +31,18 @@ impl From<VisiblePieceId> for String {
   fn from(p: VisiblePieceId) -> String { format!("{}",p) }
 }
 
+impl FromStr for VisiblePieceId {
+  type Err = AE;
+  fn from_str(s : &str) -> Result<VisiblePieceId,AE> {
+    let e = || anyhow!("could not deserialise visibile piece id");
+    let mut i = s.splitn(2,'.').map(|s| s.parse().map_err(|_| e()));
+    let h : u32 = i.next().ok_or_else(e)??;
+    let l : u32 = i.next().ok_or_else(e)??;
+    Ok(VisiblePieceId(((h as u64) << 32) | (l as u64)))
+  }
+//fn from(_: T) -> Self { todo!() }`
+}
+
 struct VisiblePieceIdVisitor { }
 impl<'de> serde::de::Visitor<'de> for VisiblePieceIdVisitor {
   type Value = VisiblePieceId;
@@ -40,11 +52,7 @@ impl<'de> serde::de::Visitor<'de> for VisiblePieceIdVisitor {
   fn visit_str<DE>(self, s : &str) -> Result<VisiblePieceId, DE>
     where DE: serde::de::Error,
   {
-    let e = || DE::custom("could not deserialise visibile piece id");
-    let mut i = s.splitn(2,'.').map(|s| s.parse().map_err(|_| e()));
-    let h : u32 = i.next().ok_or_else(e)??;
-    let l : u32 = i.next().ok_or_else(e)??;
-    Ok(VisiblePieceId(((h as u64) << 32) | (l as u64)))
+    s.parse().map_err(DE::custom)
   }
 }
 
index 36f54cda1de30a939605a24e139603d9939e6547..08b5e331075ee0182ca884480efa0653452defae 100644 (file)
@@ -7,6 +7,8 @@ pub use std::time::Duration;
 pub use std::sync::{Arc,Mutex,RwLock};
 pub use std::collections::HashMap;
 pub use std::borrow::Borrow;
+pub use std::convert::TryFrom;
+pub use std::str::FromStr;
 
 pub use thiserror::Error;
 pub use anyhow::{Context,anyhow};
@@ -33,6 +35,7 @@ pub use crate::gamestate::*;
 pub use crate::pieces::*;
 
 pub type E = anyhow::Error;
+pub type AE = anyhow::Error;
 
 pub type SvgData = Vec<u8>;
 pub type Coord = isize;