Coverage Report

Created: 2024-02-20 21:15

/builds/xfbs/cindy/common/src/tag/serde.rs
Line
Count
Source (jump to first uncovered line)
1
use super::{Tag, TagPredicate};
2
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
3
use std::str::FromStr;
4
5
// custom serde implementation that serializes and deserializes tags as strings (using their
6
// as string representation).
7
impl Serialize for Tag {
8
22
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
9
22
    where
10
22
        S: Serializer,
11
22
    {
12
22
        // serialize as hex string or as byte array, depending on format.
13
22
        self.to_string().serialize(serializer)
14
22
    }
15
}
16
17
impl<'de> Deserialize<'de> for Tag {
18
22
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
19
22
    where
20
22
        D: Deserializer<'de>,
21
22
    {
22
22
        let data: &'de str = <&'de str>::deserialize(deserializer)
?0
;
23
22
        Self::from_str(data).map_err(Error::custom)
24
22
    }
25
}
26
27
// custom serde implementation that serializes and deserializes tags as strings (using their
28
// as string representation).
29
impl<'a> Serialize for TagPredicate<'a> {
30
2
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
31
2
    where
32
2
        S: Serializer,
33
2
    {
34
2
        // serialize as hex string or as byte array, depending on format.
35
2
        self.to_string().serialize(serializer)
36
2
    }
37
}
38
39
impl<'de, 'a> Deserialize<'de> for TagPredicate<'a> {
40
1
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
41
1
    where
42
1
        D: Deserializer<'de>,
43
1
    {
44
1
        let data: String = String::deserialize(deserializer)
?0
;
45
1
        data.parse().map_err(Error::custom)
46
1
    }
47
}