From 43a97878ce14b72f0981164f87f2e35e14151312 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:22:09 +0200 Subject: Adding upstream version 110.0.1. Signed-off-by: Daniel Baumann --- third_party/rust/sync-guid/src/serde_support.rs | 61 +++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 third_party/rust/sync-guid/src/serde_support.rs (limited to 'third_party/rust/sync-guid/src/serde_support.rs') diff --git a/third_party/rust/sync-guid/src/serde_support.rs b/third_party/rust/sync-guid/src/serde_support.rs new file mode 100644 index 0000000000..50220ffe12 --- /dev/null +++ b/third_party/rust/sync-guid/src/serde_support.rs @@ -0,0 +1,61 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#![cfg(feature = "serde_support")] + +use std::fmt; + +use serde::{ + de::{self, Deserialize, Deserializer, Visitor}, + ser::{Serialize, Serializer}, +}; + +use crate::Guid; + +struct GuidVisitor; +impl<'de> Visitor<'de> for GuidVisitor { + type Value = Guid; + #[inline] + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a sync guid") + } + #[inline] + fn visit_str(self, s: &str) -> Result { + Ok(Guid::from_slice(s.as_ref())) + } +} + +impl<'de> Deserialize<'de> for Guid { + #[inline] + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + deserializer.deserialize_str(GuidVisitor) + } +} + +impl Serialize for Guid { + #[inline] + fn serialize(&self, serializer: S) -> Result { + serializer.serialize_str(self.as_str()) + } +} + +#[cfg(test)] +mod test { + use super::*; + use serde_test::{assert_tokens, Token}; + #[test] + fn test_ser_de() { + let guid = Guid::from("asdffdsa12344321"); + assert_tokens(&guid, &[Token::Str("asdffdsa12344321")]); + + let guid = Guid::from(""); + assert_tokens(&guid, &[Token::Str("")]); + + let guid = Guid::from(&b"abcd43211234"[..]); + assert_tokens(&guid, &[Token::Str("abcd43211234")]); + } +} -- cgit v1.2.3