From 4f72257fd6c1219ddca6155955464eac09264300 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 14 Oct 2020 01:28:58 +0100 Subject: [PATCH] use serde_with for ZCoord Signed-off-by: Ian Jackson --- Cargo.lock.example | 76 ++++++++++++++++++++++++++++++++++++++++++++++ zcoord/Cargo.toml | 1 + zcoord/zcoord.rs | 33 ++++++-------------- 3 files changed, 86 insertions(+), 24 deletions(-) diff --git a/Cargo.lock.example b/Cargo.lock.example index 9ead7f9f..7d8c4cfa 100644 --- a/Cargo.lock.example +++ b/Cargo.lock.example @@ -275,6 +275,41 @@ dependencies = [ "syn 1.0.44", ] +[[package]] +name = "darling" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2 1.0.24", + "quote 1.0.7", + "strsim", + "syn 1.0.44", +] + +[[package]] +name = "darling_macro" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72" +dependencies = [ + "darling_core", + "quote 1.0.7", + "syn 1.0.44", +] + [[package]] name = "delegate" version = "0.4.3" @@ -436,6 +471,12 @@ dependencies = [ "yansi", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "fs2" version = "0.4.3" @@ -629,6 +670,12 @@ dependencies = [ "url 1.7.2", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "0.1.5" @@ -1037,6 +1084,7 @@ version = "0.0.1" dependencies = [ "fehler", "serde", + "serde_with", "thiserror", ] @@ -1425,6 +1473,28 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_with" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bac272128fb3b1e98872dca27a05c18d8b78b9bd089d3edb7b5871501b50bce" +dependencies = [ + "serde", + "serde_with_macros", +] + +[[package]] +name = "serde_with_macros" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c747a9ab2e833b807f74f6b6141530655010bfa9c9c06d5508bce75c8f8072f" +dependencies = [ + "darling", + "proc-macro2 1.0.24", + "quote 1.0.7", + "syn 1.0.44", +] + [[package]] name = "sha-1" version = "0.8.2" @@ -1491,6 +1561,12 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7345c971d1ef21ffdbd103a75990a15eb03604fc8b8852ca8cb418ee1a099028" +[[package]] +name = "strsim" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" + [[package]] name = "subtle" version = "1.0.0" diff --git a/zcoord/Cargo.toml b/zcoord/Cargo.toml index 5fd7b635..f7705995 100644 --- a/zcoord/Cargo.toml +++ b/zcoord/Cargo.toml @@ -16,3 +16,4 @@ path = "zcoord.rs" serde = { version = "1", features = ["derive","rc"] } fehler = "1" thiserror = "1" +serde_with = "1" diff --git a/zcoord/zcoord.rs b/zcoord/zcoord.rs index bec465ce..d7f6ef26 100644 --- a/zcoord/zcoord.rs +++ b/zcoord/zcoord.rs @@ -70,9 +70,12 @@ use std::convert::{TryFrom, TryInto}; use std::fmt::{self, Debug, Display, Formatter}; use std::num::{TryFromIntError, Wrapping}; use std::str; +use std::str::FromStr; use fehler::{throw, throws}; -use serde::{Serialize, Serializer, Deserialize, Deserializer}; +use serde::{Serialize, Deserialize}; use thiserror::Error; +use serde_with::DeserializeFromStr; +use serde_with::SerializeDisplay; //---------- core definitions ---------- @@ -96,6 +99,7 @@ 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(DeserializeFromStr,SerializeDisplay)] pub struct ZCoord(innards::Innards); #[derive(Error,Clone,Copy,Debug,Eq,PartialEq,Serialize,Deserialize)] @@ -442,29 +446,10 @@ impl TryFrom<&str> for ZCoord { fn try_from(s: &str) -> ZCoord { ZCoord::from_str(s)? } } -impl Serialize for ZCoord { - fn serialize(&self, s: S) -> Result { - s.serialize_str(self.as_str()) - } -} - -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) - } +impl FromStr for ZCoord { + type Err = ParseError; + #[throws(ParseError)] + fn from_str(s: &str) -> ZCoord { ZCoord::from_str(s)? } } //---------- construction of ZCoord contents --------- -- 2.30.2