From: Ian Jackson Date: Sat, 27 Jun 2020 00:46:53 +0000 (+0100) Subject: VisiblePieceId FromStr X-Git-Tag: otter-0.2.0~1540 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=6efefe3319f26efda98646a648a4ce2986efbffe;p=otter.git VisiblePieceId FromStr --- diff --git a/src/gamestate.rs b/src/gamestate.rs index b8d53951..d9a269c1 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -31,6 +31,18 @@ impl From for String { fn from(p: VisiblePieceId) -> String { format!("{}",p) } } +impl FromStr for VisiblePieceId { + type Err = AE; + fn from_str(s : &str) -> Result { + 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(self, s : &str) -> Result 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) } } diff --git a/src/imports.rs b/src/imports.rs index 36f54cda..08b5e331 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -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; pub type Coord = isize;