1 // Copyright 2020-2021 Ian Jackson and contributors to Otter
2 // SPDX-License-Identifier: AGPL-3.0-or-later
3 // There is NO WARRANTY.
5 #![allow(clippy::many_single_char_names)]
9 type SKD = slotmap::KeyData;
12 macro_rules! display_consequential_impls {
14 impl From<$x> for String {
15 fn from(p: $x) -> String { format!("{}",p) }
18 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
19 <Self as Display>::fmt(self, f)
24 pub use crate::display_consequential_impls; // this is madness!
27 pub fn slotkey_parse(s: &str, sep: u8) -> SKD {
28 if s.as_bytes() == [b'0', sep, b'0', b'0'] { return default() }
29 let sep: char = sep.into();
30 let e = || anyhow!("could not deserialise visibile piece id");
31 let mut i = s.splitn(2, sep).map(|s| s.parse().map_err(|_| e()));
32 let l: u32 = i.next().ok_or_else(e)??;
33 let h: u32 = i.next().ok_or_else(e)??;
34 let v = ((h as u64) << 32) | (l as u64);
39 pub fn slotkey_write(k: SKD, sep: u8, f: &mut fmt::Formatter) {
40 let sep: char = sep.into();
41 if k == default() { write!(f, "0{}00", sep)?; return; }
43 write!(f, "{}{}{}", v & 0xffffffff, sep, v >> 32)?
47 macro_rules! visible_slotmap_key {
48 ( $x:ident($sep:expr) ) => {
50 #[derive(Copy,Default,Clone,Eq,PartialEq,Ord,PartialOrd,Serialize,Deserialize,Hash)]
51 #[serde(into="String")]
52 #[serde(try_from="String")]
53 pub struct $x(pub slotmap::KeyData);
55 impl_for_slotmap_key!($x($sep));
56 hformat_as_display!{$x}
60 macro_rules! impl_for_slotmap_key {
61 ( $x:ident($sep:expr) ) => {
64 fn fmt(&self, f: &mut fmt::Formatter) { slotkey_write(self.0,$sep,f)? }
67 impl TryFrom<&str> for $x {
70 fn try_from(s: &str) -> $x { $x(slotkey_parse(s,$sep)?) }
72 impl TryFrom<String> for $x {
75 fn try_from(s: String) -> $x { $x(slotkey_parse(&s,$sep)?) }
78 impl slotmap::Key for $x {
79 fn data(&self) -> slotmap::KeyData { self.0 }
81 impl From<slotmap::KeyData> for $x {
82 fn from(d: slotmap::KeyData) -> Self { $x(d) }
84 impl From<$x> for slotmap::KeyData {
85 fn from(p: $x) -> Self {
90 display_consequential_impls!{$x}
93 pub use crate::visible_slotmap_key; // this is madness!
94 pub use crate::impl_for_slotmap_key; // this is madness!