chiark / gitweb /
adjustable sep
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 27 Jun 2020 12:39:34 +0000 (13:39 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 27 Jun 2020 12:39:41 +0000 (13:39 +0100)
src/gamestate.rs
src/keydata.rs

index bf325f7e62a9f0010d6e93c46360fbe4adc6bafc..6aa8d438428c02d04d7fd8781db1cd30a9a7208d 100644 (file)
@@ -26,14 +26,16 @@ pub type VisiblePieceIdSvgIds = &'static [&'static str];
 
 impl Display for VisiblePieceId {
   #[throws(fmt::Error)]
-  fn fmt(&self, f : &mut fmt::Formatter) { slotkey_write(self.0,f)? }
+  fn fmt(&self, f : &mut fmt::Formatter) { slotkey_write(self.0,'.',f)? }
 }
 display_consequential_impls!{VisiblePieceId}
 
 impl TryFrom<&str> for VisiblePieceId {
   type Error = AE;
   #[throws(AE)]
-  fn try_from(s : &str) -> VisiblePieceId { VisiblePieceId(slotkey_parse(s)?) }
+  fn try_from(s : &str) -> VisiblePieceId {
+    VisiblePieceId(slotkey_parse(s,'.')?)
+  }
 }
 
 impl PieceRenderInstructions {
index f8edaa52528ccb250efde0ace63f061869e2247d..58f25525a2c50d3f6bfeeb680f40c0e71375519f 100644 (file)
@@ -1,6 +1,8 @@
 
 use crate::imports::*;
 
+type SKD = slotmap::KeyData;
+
 #[macro_export]
 macro_rules! display_consequential_impls {
   ( $x:path ) => {
@@ -18,11 +20,17 @@ macro_rules! display_consequential_impls {
 pub use crate::display_consequential_impls; // this is madness!
 
 #[throws(AE)]
-pub fn slotkey_parse(s : &str) -> slotmap::KeyData {
+pub fn slotkey_parse(s : &str, sep : char) -> SKD {
   let e = || anyhow!("could not deserialise visibile piece id");
-  let mut i = s.splitn(2,'.').map(|s| s.parse().map_err(|_| e()));
+  let mut i = s.splitn(2,sep).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)??;
   let v = ((h as u64) << 32) | (l as u64);
-  slotmap::KeyData::from_ffi(v)
+  SKD::from_ffi(v)
+}
+
+#[throws(fmt::Error)]
+pub fn slotkey_write(k : SKD, sep : char, f : &mut fmt::Formatter) {
+  let v = k.as_ffi();
+  write!(f, "{}{}{}", v >> 32, sep, v & 0xffffffff)?
 }