1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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<E: de::Error>(self, s: &str) -> Result<Self::Value, E> {
Ok(Guid::from_slice(s.as_ref()))
}
}
impl<'de> Deserialize<'de> for Guid {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(GuidVisitor)
}
}
impl Serialize for Guid {
#[inline]
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
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")]);
}
}
|