From: Ian Jackson Date: Wed, 14 Oct 2020 00:00:50 +0000 (+0100) Subject: sort out deserialize for ZCoord X-Git-Tag: otter-0.2.0~659 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=068533553fab1f0020f993ef27c016432aed1228;p=otter.git sort out deserialize for ZCoord Signed-off-by: Ian Jackson --- diff --git a/zcoord/zcoord.rs b/zcoord/zcoord.rs index 39ad3599..bec465ce 100644 --- a/zcoord/zcoord.rs +++ b/zcoord/zcoord.rs @@ -71,7 +71,7 @@ use std::fmt::{self, Debug, Display, Formatter}; use std::num::{TryFromIntError, Wrapping}; use std::str; use fehler::{throw, throws}; -use serde::{Serialize, Serializer, Deserialize}; +use serde::{Serialize, Serializer, Deserialize, Deserializer}; use thiserror::Error; //---------- core definitions ---------- @@ -96,8 +96,6 @@ const TEXT_PER_LIMB : usize = DIGITS_PER_LIMB + 1; const LIMB_MODULUS : LimbVal = Wrapping(RAW_LIMB_MODULUS); const LIMB_MASK : LimbVal = Wrapping(RAW_LIMB_MODULUS-1); -#[derive(Deserialize)] -#[serde(try_from="&str")] pub struct ZCoord(innards::Innards); #[derive(Error,Clone,Copy,Debug,Eq,PartialEq,Serialize,Deserialize)] @@ -450,6 +448,25 @@ impl Serialize for ZCoord { } } +impl<'de> Deserialize<'de> for ZCoord { + fn deserialize>(d: D) -> Result { + use serde::de::{Visitor, Error, Unexpected}; + struct V; + impl<'de> Visitor<'de> for V { + type Value = ZCoord; + fn expecting(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "a z coordinate (as a string)") + } + fn visit_str(self, s: &str) -> Result { + ZCoord::from_str(s).map_err(|ParseError| Error::invalid_value( + Unexpected::Str(s), &self + )) + } + } + d.deserialize_str(V) + } +} + //---------- construction of ZCoord contents --------- // // We can panic if this code is buggy, but not compromise safety.