Coverage Report

Created: 2022-07-04 16:17

src/serde/bitflags_u8.rs
Line
Count
Source (jump to first uncovered line)
1
use std::{
2
    fmt::{self, Display},
3
    marker::PhantomData,
4
};
5
6
use serde::{
7
    de::{self, Deserializer, Visitor},
8
    ser::Serializer,
9
};
10
11
0
#[derive(Debug, thiserror::Error)]
Unexecuted instantiation: <obws::serde::bitflags_u8::Error as core::fmt::Debug>::fmt
Unexecuted instantiation: <obws::serde::bitflags_u8::Error as core::fmt::Debug>::fmt
Unexecuted instantiation: <obws::serde::bitflags_u8::Error as core::fmt::Display>::fmt
Unexecuted instantiation: <obws::serde::bitflags_u8::Error as std::error::Error>::source
Unexecuted instantiation: <obws::serde::bitflags_u8::Error as std::error::Error>::source
Unexecuted instantiation: <obws::serde::bitflags_u8::Error as core::fmt::Display>::fmt
12
enum Error {
13
    #[error("value is too large for an u8: {0}")]
14
    ValueTooLarge(#[source] std::num::TryFromIntError),
15
    #[error("conversion from integer failed: {0}")]
16
    IntConversionFailed(String),
17
}
18
19
1
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
20
1
where
21
1
    S: Serializer,
22
1
    T: Into<u8> + Copy,
23
1
{
24
1
    serializer.serialize_u8((*value).into())
25
1
}
Unexecuted instantiation: obws::serde::bitflags_u8::serialize::<_, _>
obws::serde::bitflags_u8::serialize::<&mut serde_test::ser::Serializer, obws::serde::bitflags_u8::tests::Flags>
Line
Count
Source
19
1
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
20
1
where
21
1
    S: Serializer,
22
1
    T: Into<u8> + Copy,
23
1
{
24
1
    serializer.serialize_u8((*value).into())
25
1
}
26
27
4
pub fn deserialize<'de, D, T, TE>(deserializer: D) -> Result<T, D::Error>
28
4
where
29
4
    D: Deserializer<'de>,
30
4
    T: TryFrom<u8, Error = TE>,
31
4
    TE: Display,
32
4
{
33
4
    deserializer.deserialize_u8(BitflagsU8Visitor { flags: PhantomData })
34
4
}
Unexecuted instantiation: obws::serde::bitflags_u8::deserialize::<serde::__private::de::content::ContentDeserializer<serde_json::error::Error>, obws::common::Alignment, obws::Error>
obws::serde::bitflags_u8::deserialize::<serde_json::value::Value, obws::common::Alignment, obws::Error>
Line
Count
Source
27
2
pub fn deserialize<'de, D, T, TE>(deserializer: D) -> Result<T, D::Error>
28
2
where
29
2
    D: Deserializer<'de>,
30
2
    T: TryFrom<u8, Error = TE>,
31
2
    TE: Display,
32
2
{
33
2
    deserializer.deserialize_u8(BitflagsU8Visitor { flags: PhantomData })
34
2
}
Unexecuted instantiation: obws::serde::bitflags_u8::deserialize::<_, _, _>
obws::serde::bitflags_u8::deserialize::<&mut serde_test::de::Deserializer, obws::serde::bitflags_u8::tests::Flags, obws::Error>
Line
Count
Source
27
2
pub fn deserialize<'de, D, T, TE>(deserializer: D) -> Result<T, D::Error>
28
2
where
29
2
    D: Deserializer<'de>,
30
2
    T: TryFrom<u8, Error = TE>,
31
2
    TE: Display,
32
2
{
33
2
    deserializer.deserialize_u8(BitflagsU8Visitor { flags: PhantomData })
34
2
}
Unexecuted instantiation: obws::serde::bitflags_u8::deserialize::<serde::de::value::MapAccessDeserializer<serde_test::de::DeserializerMapVisitor>, obws::serde::bitflags_u8::tests::Flags, obws::Error>
Unexecuted instantiation: obws::serde::bitflags_u8::deserialize::<serde::de::value::SeqAccessDeserializer<serde_test::de::DeserializerSeqVisitor>, obws::serde::bitflags_u8::tests::Flags, obws::Error>
35
36
struct BitflagsU8Visitor<T, TE> {
37
    flags: PhantomData<(T, TE)>,
38
}
39
40
impl<'de, T, TE> Visitor<'de> for BitflagsU8Visitor<T, TE>
41
where
42
    T: TryFrom<u8, Error = TE>,
43
    TE: Display,
44
{
45
    type Value = T;
46
47
0
    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
48
0
        formatter.write_str("bitflags encoded as u8 integer")
49
0
    }
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<obws::common::Alignment, obws::Error> as serde::de::Visitor>::expecting
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<_, _> as serde::de::Visitor>::expecting
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<obws::serde::bitflags_u8::tests::Flags, obws::Error> as serde::de::Visitor>::expecting
50
51
0
    fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
52
0
    where
53
0
        E: de::Error,
54
0
    {
55
0
        u8::try_from(v)
56
0
            .map_err(|e| de::Error::custom(Error::ValueTooLarge(e)))
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<obws::common::Alignment, obws::Error> as serde::de::Visitor>::visit_i64::<serde_json::error::Error>::{closure#0}
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<_, _> as serde::de::Visitor>::visit_i64::<_>::{closure#0}
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<obws::serde::bitflags_u8::tests::Flags, obws::Error> as serde::de::Visitor>::visit_i64::<serde_test::error::Error>::{closure#0}
57
0
            .and_then(|v| self.visit_u8(v))
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<obws::common::Alignment, obws::Error> as serde::de::Visitor>::visit_i64::<serde_json::error::Error>::{closure#1}
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<_, _> as serde::de::Visitor>::visit_i64::<_>::{closure#1}
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<obws::serde::bitflags_u8::tests::Flags, obws::Error> as serde::de::Visitor>::visit_i64::<serde_test::error::Error>::{closure#1}
58
0
    }
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<obws::common::Alignment, obws::Error> as serde::de::Visitor>::visit_i64::<serde_json::error::Error>
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<_, _> as serde::de::Visitor>::visit_i64::<_>
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<obws::serde::bitflags_u8::tests::Flags, obws::Error> as serde::de::Visitor>::visit_i64::<serde_test::error::Error>
59
60
4
    fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
61
4
    where
62
4
        E: de::Error,
63
4
    {
64
4
        T::try_from(v).map_err(|e| 
de::Error::custom(Error::IntConversionFailed(e.to_string()))0
)
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<obws::common::Alignment, obws::Error> as serde::de::Visitor>::visit_u8::<serde_json::error::Error>::{closure#0}
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<_, _> as serde::de::Visitor>::visit_u8::<_>::{closure#0}
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<obws::serde::bitflags_u8::tests::Flags, obws::Error> as serde::de::Visitor>::visit_u8::<serde_test::error::Error>::{closure#0}
65
4
    }
<obws::serde::bitflags_u8::BitflagsU8Visitor<obws::common::Alignment, obws::Error> as serde::de::Visitor>::visit_u8::<serde_json::error::Error>
Line
Count
Source
60
2
    fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
61
2
    where
62
2
        E: de::Error,
63
2
    {
64
2
        T::try_from(v).map_err(|e| de::Error::custom(Error::IntConversionFailed(e.to_string())))
65
2
    }
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<_, _> as serde::de::Visitor>::visit_u8::<_>
<obws::serde::bitflags_u8::BitflagsU8Visitor<obws::serde::bitflags_u8::tests::Flags, obws::Error> as serde::de::Visitor>::visit_u8::<serde_test::error::Error>
Line
Count
Source
60
2
    fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
61
2
    where
62
2
        E: de::Error,
63
2
    {
64
2
        T::try_from(v).map_err(|e| de::Error::custom(Error::IntConversionFailed(e.to_string())))
65
2
    }
66
67
2
    fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
68
2
    where
69
2
        E: de::Error,
70
2
    {
71
2
        u8::try_from(v)
72
2
            .map_err(|e| 
de::Error::custom(Error::ValueTooLarge(e))0
)
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<obws::common::Alignment, obws::Error> as serde::de::Visitor>::visit_u64::<serde_json::error::Error>::{closure#0}
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<_, _> as serde::de::Visitor>::visit_u64::<_>::{closure#0}
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<obws::serde::bitflags_u8::tests::Flags, obws::Error> as serde::de::Visitor>::visit_u64::<serde_test::error::Error>::{closure#0}
73
2
            .and_then(|v| self.visit_u8(v))
<obws::serde::bitflags_u8::BitflagsU8Visitor<obws::common::Alignment, obws::Error> as serde::de::Visitor>::visit_u64::<serde_json::error::Error>::{closure#1}
Line
Count
Source
73
2
            .and_then(|v| self.visit_u8(v))
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<_, _> as serde::de::Visitor>::visit_u64::<_>::{closure#1}
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<obws::serde::bitflags_u8::tests::Flags, obws::Error> as serde::de::Visitor>::visit_u64::<serde_test::error::Error>::{closure#1}
74
2
    }
<obws::serde::bitflags_u8::BitflagsU8Visitor<obws::common::Alignment, obws::Error> as serde::de::Visitor>::visit_u64::<serde_json::error::Error>
Line
Count
Source
67
2
    fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
68
2
    where
69
2
        E: de::Error,
70
2
    {
71
2
        u8::try_from(v)
72
2
            .map_err(|e| de::Error::custom(Error::ValueTooLarge(e)))
73
2
            .and_then(|v| self.visit_u8(v))
74
2
    }
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<_, _> as serde::de::Visitor>::visit_u64::<_>
Unexecuted instantiation: <obws::serde::bitflags_u8::BitflagsU8Visitor<obws::serde::bitflags_u8::tests::Flags, obws::Error> as serde::de::Visitor>::visit_u64::<serde_test::error::Error>
75
}
76
77
#[cfg(test)]
78
mod tests {
79
    use bitflags::bitflags;
80
    use serde::{Deserialize, Serialize};
81
    use serde_test::{assert_tokens, Token};
82
83
    bitflags! {
84
        struct Flags: u8 {
85
            const ONE = 1;
86
            const TWO = 2;
87
        }
88
    }
89
90
    impl From<Flags> for u8 {
91
1
        fn from(value: Flags) -> Self {
92
1
            value.bits
93
1
        }
94
    }
95
96
    impl TryFrom<u8> for Flags {
97
        type Error = crate::Error;
98
99
2
        fn try_from(value: u8) -> Result<Self, Self::Error> {
100
2
            Self::from_bits(value).ok_or(Self::Error::UnknownFlags(value))
101
2
        }
102
    }
103
104
8
    #[derive(
Debug0
,
PartialEq2
,
Seri1
aliz1
e1
,
D6
ese
rializ0
e)]
Unexecuted instantiation: <obws::serde::bitflags_u8::tests::SimpleFlags as core::cmp::PartialEq>::ne
<obws::serde::bitflags_u8::tests::SimpleFlags as core::cmp::PartialEq>::eq
Line
Count
Source
104
2
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
<obws::serde::bitflags_u8::tests::SimpleFlags as serde::ser::Serialize>::serialize::<&mut serde_test::ser::Serializer>
Line
Count
Source
104
1
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
<<obws::serde::bitflags_u8::tests::SimpleFlags as serde::ser::Serialize>::serialize::__SerializeWith as serde::ser::Serialize>::serialize::<&mut serde_test::ser::Serializer>
Line
Count
Source
104
1
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
Unexecuted instantiation: <<obws::serde::bitflags_u8::tests::SimpleFlags as serde::de::Deserialize>::deserialize::__Field as serde::de::Deserialize>::deserialize::<serde::de::value::U32Deserializer<serde_test::error::Error>>
Unexecuted instantiation: <<obws::serde::bitflags_u8::tests::SimpleFlags as serde::de::Deserialize>::deserialize::__Field as serde::de::Deserialize>::deserialize::<serde_test::de::BytesDeserializer>
<<obws::serde::bitflags_u8::tests::SimpleFlags as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::visit_str::<serde_test::error::Error>
Line
Count
Source
104
2
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
<<obws::serde::bitflags_u8::tests::SimpleFlags as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<serde_test::de::DeserializerMapVisitor>
Line
Count
Source
104
4
    #[derive(Debug, PartialEq, Serialize, 
D2
eserialize)]
Unexecuted instantiation: <<obws::serde::bitflags_u8::tests::SimpleFlags as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::expecting
Unexecuted instantiation: <<<obws::serde::bitflags_u8::tests::SimpleFlags 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::bitflags_u8::tests::SimpleFlags 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::bitflags_u8::tests::SimpleFlags as serde::de::Deserialize>::deserialize::<&mut serde_test::de::Deserializer>
Line
Count
Source
104
2
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
Unexecuted instantiation: <<<obws::serde::bitflags_u8::tests::SimpleFlags 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::bitflags_u8::tests::SimpleFlags 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
104
2
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
<<obws::serde::bitflags_u8::tests::SimpleFlags as serde::de::Deserialize>::deserialize::__Field as serde::de::Deserialize>::deserialize::<&mut serde_test::de::Deserializer>
Line
Count
Source
104
2
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
Unexecuted instantiation: <<obws::serde::bitflags_u8::tests::SimpleFlags as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<serde_test::de::EnumMapVisitor>
Unexecuted instantiation: <<obws::serde::bitflags_u8::tests::SimpleFlags as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::<serde_test::de::DeserializerSeqVisitor>
Unexecuted instantiation: <<obws::serde::bitflags_u8::tests::SimpleFlags as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::visit_u64::<serde_test::error::Error>
Unexecuted instantiation: <<obws::serde::bitflags_u8::tests::SimpleFlags as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::visit_bytes::<serde_test::error::Error>
Unexecuted instantiation: <<obws::serde::bitflags_u8::tests::SimpleFlags as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::<serde_test::de::DeserializerSeqVisitor>::{closure#0}
Unexecuted instantiation: <<obws::serde::bitflags_u8::tests::SimpleFlags as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::expecting
Unexecuted instantiation: <<obws::serde::bitflags_u8::tests::SimpleFlags as serde::de::Deserialize>::deserialize::__Field as serde::de::Deserialize>::deserialize::<serde::de::value::StrDeserializer<serde_test::error::Error>>
105
    struct SimpleFlags {
106
        #[serde(with = "super")]
107
        value: Flags,
108
    }
109
110
1
    #[test]
111
1
    fn roundtrip() {
112
1
        assert_tokens(
113
1
            &SimpleFlags {
114
1
                value: Flags::ONE | Flags::TWO,
115
1
            },
116
1
            &[
117
1
                Token::Struct {
118
1
                    name: "SimpleFlags",
119
1
                    len: 1,
120
1
                },
121
1
                Token::Str("value"),
122
1
                Token::U8(3),
123
1
                Token::StructEnd,
124
1
            ],
125
1
        );
126
1
    }
127
}