Coverage Report

Created: 2022-07-04 16:17

src/serde/json_string.rs
Line
Count
Source (jump to first uncovered line)
1
use std::{fmt, marker::PhantomData};
2
3
use serde::{
4
    de::{self, DeserializeOwned, Deserializer, Visitor},
5
    ser::{self, Serialize, Serializer},
6
};
7
8
1
#[derive(
Debug0
, thiserror::Error)]
Unexecuted instantiation: <obws::serde::json_string::Error as core::fmt::Debug>::fmt
Unexecuted instantiation: <obws::serde::json_string::Error as core::fmt::Debug>::fmt
Unexecuted instantiation: <obws::serde::json_string::Error as core::fmt::Display>::fmt
Unexecuted instantiation: <obws::serde::json_string::Error as std::error::Error>::source
Unexecuted instantiation: <obws::serde::json_string::Error as std::error::Error>::source
<obws::serde::json_string::Error as core::fmt::Display>::fmt
Line
Count
Source
8
1
#[derive(Debug, thiserror::Error)]
9
enum Error {
10
    #[error("failed deserializing JSON string")]
11
    InvalidJson(#[source] serde_json::Error),
12
}
13
14
1
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
15
1
where
16
1
    S: Serializer,
17
1
    T: Serialize,
18
1
{
19
1
    let json = serde_json::to_string(value).map_err(ser::Error::custom)
?0
;
20
1
    serializer.serialize_str(&json)
21
1
}
Unexecuted instantiation: obws::serde::json_string::serialize::<_, _>
obws::serde::json_string::serialize::<&mut serde_test::ser::Serializer, obws::serde::json_string::tests::Inner>
Line
Count
Source
14
1
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
15
1
where
16
1
    S: Serializer,
17
1
    T: Serialize,
18
1
{
19
1
    let json = serde_json::to_string(value).map_err(ser::Error::custom)
?0
;
20
1
    serializer.serialize_str(&json)
21
1
}
22
23
#[allow(dead_code)]
24
3
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
25
3
where
26
3
    D: Deserializer<'de>,
27
3
    T: DeserializeOwned,
28
3
{
29
3
    deserializer.deserialize_str(JsonStringVisitor(PhantomData))
30
3
}
Unexecuted instantiation: obws::serde::json_string::deserialize::<_, _>
Unexecuted instantiation: obws::serde::json_string::deserialize::<serde::de::value::SeqAccessDeserializer<serde_test::de::DeserializerSeqVisitor>, obws::serde::json_string::tests::Inner>
Unexecuted instantiation: obws::serde::json_string::deserialize::<serde::de::value::MapAccessDeserializer<serde_test::de::DeserializerMapVisitor>, obws::serde::json_string::tests::Inner>
obws::serde::json_string::deserialize::<&mut serde_test::de::Deserializer, obws::serde::json_string::tests::Inner>
Line
Count
Source
24
3
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
25
3
where
26
3
    D: Deserializer<'de>,
27
3
    T: DeserializeOwned,
28
3
{
29
3
    deserializer.deserialize_str(JsonStringVisitor(PhantomData))
30
3
}
31
32
struct JsonStringVisitor<T>(PhantomData<T>);
33
34
impl<'de, T> Visitor<'de> for JsonStringVisitor<T>
35
where
36
    T: DeserializeOwned,
37
{
38
    type Value = T;
39
40
0
    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
41
0
        formatter.write_str("string value that contains JSON")
42
0
    }
Unexecuted instantiation: <obws::serde::json_string::JsonStringVisitor<_> as serde::de::Visitor>::expecting
Unexecuted instantiation: <obws::serde::json_string::JsonStringVisitor<obws::serde::json_string::tests::Inner> as serde::de::Visitor>::expecting
43
44
3
    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
45
3
    where
46
3
        E: de::Error,
47
3
    {
48
3
        serde_json::from_str(v).map_err(|e| 
de::Error::custom(Error::InvalidJson(e))1
)
Unexecuted instantiation: <obws::serde::json_string::JsonStringVisitor<_> as serde::de::Visitor>::visit_str::<_>::{closure#0}
<obws::serde::json_string::JsonStringVisitor<obws::serde::json_string::tests::Inner> as serde::de::Visitor>::visit_str::<serde_test::error::Error>::{closure#0}
Line
Count
Source
48
1
        serde_json::from_str(v).map_err(|e| de::Error::custom(Error::InvalidJson(e)))
49
3
    }
Unexecuted instantiation: <obws::serde::json_string::JsonStringVisitor<_> as serde::de::Visitor>::visit_str::<_>
<obws::serde::json_string::JsonStringVisitor<obws::serde::json_string::tests::Inner> as serde::de::Visitor>::visit_str::<serde_test::error::Error>
Line
Count
Source
44
3
    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
45
3
    where
46
3
        E: de::Error,
47
3
    {
48
3
        serde_json::from_str(v).map_err(|e| de::Error::custom(Error::InvalidJson(e)))
49
3
    }
50
}
51
52
#[cfg(test)]
53
mod tests {
54
    use serde::{Deserialize, Serialize};
55
    use serde_test::{assert_de_tokens_error, assert_tokens, Token};
56
57
6
    #[derive(
Debug0
,
PartialEq2
,
Seri1
aliz1
e1
,
D4
eserialize)]
Unexecuted instantiation: <obws::serde::json_string::tests::Inner as core::cmp::PartialEq>::ne
<obws::serde::json_string::tests::Inner as core::cmp::PartialEq>::eq
Line
Count
Source
57
2
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
Unexecuted instantiation: <<obws::serde::json_string::tests::Inner as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::expecting
Unexecuted instantiation: <<obws::serde::json_string::tests::Inner as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::visit_u64::<_>
Unexecuted instantiation: <<obws::serde::json_string::tests::Inner as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::visit_bytes::<_>
Unexecuted instantiation: <<obws::serde::json_string::tests::Inner as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::expecting
<<obws::serde::json_string::tests::Inner as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::visit_str::<serde_json::error::Error>
Line
Count
Source
57
2
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
<<obws::serde::json_string::tests::Inner as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<serde_json::de::MapAccess<serde_json::read::StrRead>>
Line
Count
Source
57
4
    #[derive(Debug, PartialEq, Serialize, 
D2
eserialize)]
<obws::serde::json_string::tests::Inner as serde::de::Deserialize>::deserialize::<&mut serde_json::de::Deserializer<serde_json::read::StrRead>>
Line
Count
Source
57
3
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
<<obws::serde::json_string::tests::Inner as serde::de::Deserialize>::deserialize::__Field as serde::de::Deserialize>::deserialize::<serde_json::de::MapKey<serde_json::read::StrRead>>
Line
Count
Source
57
2
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
Unexecuted instantiation: <<obws::serde::json_string::tests::Inner as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::<serde_json::de::SeqAccess<serde_json::read::StrRead>>
58
    struct Inner {
59
        value: u32,
60
    }
61
62
12
    #[derive(
Debug0
,
PartialEq2
,
Seri1
aliz1
e1
,
D9
ese
rializ0
e)]
Unexecuted instantiation: <obws::serde::json_string::tests::SimpleStruct as core::cmp::PartialEq>::ne
<obws::serde::json_string::tests::SimpleStruct as core::cmp::PartialEq>::eq
Line
Count
Source
62
2
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
<obws::serde::json_string::tests::SimpleStruct as serde::ser::Serialize>::serialize::<&mut serde_test::ser::Serializer>
Line
Count
Source
62
1
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
<<obws::serde::json_string::tests::SimpleStruct as serde::ser::Serialize>::serialize::__SerializeWith as serde::ser::Serialize>::serialize::<&mut serde_test::ser::Serializer>
Line
Count
Source
62
1
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
<<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__Field as serde::de::Deserialize>::deserialize::<&mut serde_test::de::Deserializer>
Line
Count
Source
62
3
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
Unexecuted instantiation: <<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::expecting
Unexecuted instantiation: <<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::visit_bytes::<serde_test::error::Error>
Unexecuted instantiation: <<<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::__DeserializeWith as serde::de::Deserialize>::deserialize::<&mut serde_test::de::Deserializer>
Unexecuted instantiation: <<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__Field as serde::de::Deserialize>::deserialize::<serde::de::value::StrDeserializer<serde_test::error::Error>>
Unexecuted instantiation: <<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__Field as serde::de::Deserialize>::deserialize::<serde::de::value::U32Deserializer<serde_test::error::Error>>
<<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::visit_str::<serde_test::error::Error>
Line
Count
Source
62
3
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
Unexecuted instantiation: <<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<serde_test::de::EnumMapVisitor>
Unexecuted instantiation: <<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__Field as serde::de::Deserialize>::deserialize::<serde_test::de::BytesDeserializer>
Unexecuted instantiation: <<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::expecting
Unexecuted instantiation: <<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::<serde_test::de::DeserializerSeqVisitor>
Unexecuted instantiation: <<<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::__DeserializeWith as serde::de::Deserialize>::deserialize::<serde::de::value::SeqAccessDeserializer<serde_test::de::DeserializerSeqVisitor>>
<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::<&mut serde_test::de::Deserializer>
Line
Count
Source
62
3
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
<<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<serde_test::de::DeserializerMapVisitor>
Line
Count
Source
62
6
    #[derive(Debug, PartialEq, Serialize, 
D3
eserialize)]
Unexecuted instantiation: <<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::<serde_test::de::DeserializerSeqVisitor>::{closure#0}
Unexecuted instantiation: <<<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::__DeserializeWith as serde::de::Deserialize>::deserialize::<serde::de::value::MapAccessDeserializer<serde_test::de::DeserializerMapVisitor>>
<<<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::__DeserializeWith as serde::de::Deserialize>::deserialize::<&mut serde_test::de::Deserializer>
Line
Count
Source
62
3
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
Unexecuted instantiation: <<obws::serde::json_string::tests::SimpleStruct as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::visit_u64::<serde_test::error::Error>
63
    struct SimpleStruct {
64
        #[serde(with = "super")]
65
        inner: Inner,
66
    }
67
68
1
    #[test]
69
1
    fn roundtrip() {
70
1
        assert_tokens(
71
1
            &SimpleStruct {
72
1
                inner: Inner { value: 5 },
73
1
            },
74
1
            &[
75
1
                Token::Struct {
76
1
                    name: "SimpleStruct",
77
1
                    len: 1,
78
1
                },
79
1
                Token::Str("inner"),
80
1
                Token::Str(r#"{"value":5}"#),
81
1
                Token::StructEnd,
82
1
            ],
83
1
        )
84
1
    }
85
86
1
    #[test]
87
1
    fn deser() {
88
1
        assert_de_tokens_error::<SimpleStruct>(
89
1
            &[
90
1
                Token::Struct {
91
1
                    name: "SimpleStruct",
92
1
                    len: 1,
93
1
                },
94
1
                Token::Str("inner"),
95
1
                Token::Str(""),
96
1
                Token::StructEnd,
97
1
            ],
98
1
            "failed deserializing JSON string",
99
1
        );
100
1
    }
101
}