From 134727652dc3a65bc5cf6a4d3cfb1c24fd023d5e Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Thu, 16 Sep 2021 10:51:39 -0300 Subject: [Project] Soni's Serde Utilities Some utilities for use with serde. --- src/lib.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/lib.rs (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..0002663 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,60 @@ +use serde::Deserialize; + +#[derive(Deserialize)] +#[serde(untagged)] +enum MaybeHelper { + Some(T), + None(serde::de::IgnoredAny), +} + +/// Something that may be an `T`. +/// +/// Unlike `Option`, this places no restriction on whether something +/// can't be something else entirely. +/// +/// Can only be used with self-describing formats, like JSON. +#[derive(Deserialize)] +#[serde(from = "Option>")] +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[repr(transparent)] +pub struct MayBe(pub Option); + +impl Default for MayBe { + fn default() -> Self { + Self(Default::default()) + } +} + +impl std::ops::Deref for MayBe { + type Target = Option; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl std::ops::DerefMut for MayBe { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl From> for Option { + fn from(thing: MayBe) -> Option { + thing.0 + } +} + +impl From> for MayBe { + fn from(thing: Option) -> MayBe { + Self(thing) + } +} + +impl From>> for MayBe { + fn from(thing: Option>) -> MayBe { + Self(match thing { + Some(MaybeHelper::Some(v)) => Some(v), + _ => None, + }) + } +} -- cgit 1.4.1