Coverage Report

Created: 2022-07-04 16:17

src/serde/rgba8_inverse.rs
Line
Count
Source (jump to first uncovered line)
1
use std::fmt;
2
3
use rgb::RGBA8;
4
use serde::{
5
    de::{self, Deserializer, Visitor},
6
    ser::Serializer,
7
};
8
9
0
#[derive(Debug, thiserror::Error)]
Unexecuted instantiation: <obws::serde::rgba8_inverse::Error as core::fmt::Debug>::fmt
Unexecuted instantiation: <obws::serde::rgba8_inverse::Error as core::fmt::Debug>::fmt
Unexecuted instantiation: <obws::serde::rgba8_inverse::Error as std::error::Error>::source
Unexecuted instantiation: <obws::serde::rgba8_inverse::Error as core::fmt::Display>::fmt
Unexecuted instantiation: <obws::serde::rgba8_inverse::Error as std::error::Error>::source
Unexecuted instantiation: <obws::serde::rgba8_inverse::Error as core::fmt::Display>::fmt
10
enum Error {
11
    #[error("value is too large for an u32: {0}")]
12
    ValueTooLarge(#[source] std::num::TryFromIntError),
13
}
14
15
1
pub fn serialize<S>(value: &RGBA8, serializer: S) -> Result<S::Ok, S::Error>
16
1
where
17
1
    S: Serializer,
18
1
{
19
1
    let abgr =
20
1
        (value.a as u32) << 24 | (value.b as u32) << 16 | (value.g as u32) << 8 | (value.r as u32);
21
1
    serializer.serialize_u32(abgr)
22
1
}
Unexecuted instantiation: obws::serde::rgba8_inverse::serialize::<_>
obws::serde::rgba8_inverse::serialize::<&mut serde_test::ser::Serializer>
Line
Count
Source
15
1
pub fn serialize<S>(value: &RGBA8, serializer: S) -> Result<S::Ok, S::Error>
16
1
where
17
1
    S: Serializer,
18
1
{
19
1
    let abgr =
20
1
        (value.a as u32) << 24 | (value.b as u32) << 16 | (value.g as u32) << 8 | (value.r as u32);
21
1
    serializer.serialize_u32(abgr)
22
1
}
23
24
#[allow(dead_code)]
25
2
pub fn deserialize<'de, D>(deserializer: D) -> Result<RGBA8, D::Error>
26
2
where
27
2
    D: Deserializer<'de>,
28
2
{
29
2
    deserializer.deserialize_u32(Rgba8InverseVisitor)
30
2
}
Unexecuted instantiation: obws::serde::rgba8_inverse::deserialize::<_>
obws::serde::rgba8_inverse::deserialize::<&mut serde_test::de::Deserializer>
Line
Count
Source
25
2
pub fn deserialize<'de, D>(deserializer: D) -> Result<RGBA8, D::Error>
26
2
where
27
2
    D: Deserializer<'de>,
28
2
{
29
2
    deserializer.deserialize_u32(Rgba8InverseVisitor)
30
2
}
Unexecuted instantiation: obws::serde::rgba8_inverse::deserialize::<serde::de::value::MapAccessDeserializer<serde_test::de::DeserializerMapVisitor>>
Unexecuted instantiation: obws::serde::rgba8_inverse::deserialize::<serde::de::value::SeqAccessDeserializer<serde_test::de::DeserializerSeqVisitor>>
31
32
struct Rgba8InverseVisitor;
33
34
impl<'de> Visitor<'de> for Rgba8InverseVisitor {
35
    type Value = RGBA8;
36
37
0
    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
38
0
        formatter.write_str("RGBA color encoded as u32 integer in reverse order")
39
0
    }
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::expecting
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::expecting
40
41
0
    fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
42
0
    where
43
0
        E: de::Error,
44
0
    {
45
0
        u32::try_from(v)
46
0
            .map_err(|e| de::Error::custom(Error::ValueTooLarge(e)))
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::visit_i64::<_>::{closure#0}
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::visit_i64::<serde_test::error::Error>::{closure#0}
47
0
            .and_then(|v| self.visit_u32(v))
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::visit_i64::<_>::{closure#1}
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::visit_i64::<serde_test::error::Error>::{closure#1}
48
0
    }
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::visit_i64::<_>
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::visit_i64::<serde_test::error::Error>
49
50
2
    fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
51
2
    where
52
2
        E: de::Error,
53
2
    {
54
2
        Ok(RGBA8 {
55
2
            r: (v & 0xff) as u8,
56
2
            g: (v >> 8 & 0xff) as u8,
57
2
            b: (v >> 16 & 0xff) as u8,
58
2
            a: (v >> 24 & 0xff) as u8,
59
2
        })
60
2
    }
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::visit_u32::<_>
<obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::visit_u32::<serde_test::error::Error>
Line
Count
Source
50
2
    fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
51
2
    where
52
2
        E: de::Error,
53
2
    {
54
2
        Ok(RGBA8 {
55
2
            r: (v & 0xff) as u8,
56
2
            g: (v >> 8 & 0xff) as u8,
57
2
            b: (v >> 16 & 0xff) as u8,
58
2
            a: (v >> 24 & 0xff) as u8,
59
2
        })
60
2
    }
61
62
0
    fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
63
0
    where
64
0
        E: de::Error,
65
0
    {
66
0
        u32::try_from(v)
67
0
            .map_err(|e| de::Error::custom(Error::ValueTooLarge(e)))
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::visit_u64::<_>::{closure#0}
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::visit_u64::<serde_test::error::Error>::{closure#0}
68
0
            .and_then(|v| self.visit_u32(v))
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::visit_u64::<_>::{closure#1}
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::visit_u64::<serde_test::error::Error>::{closure#1}
69
0
    }
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::visit_u64::<_>
Unexecuted instantiation: <obws::serde::rgba8_inverse::Rgba8InverseVisitor as serde::de::Visitor>::visit_u64::<serde_test::error::Error>
70
}
71
72
#[cfg(test)]
73
mod tests {
74
    use rgb::RGBA8;
75
    use serde::{Deserialize, Serialize};
76
    use serde_test::{assert_tokens, Token};
77
78
8
    #[derive(
Debug0
,
PartialEq2
,
Seri1
aliz1
e1
,
D6
ese
rializ0
e)]
Unexecuted instantiation: <obws::serde::rgba8_inverse::tests::SimpleDuration as core::cmp::PartialEq>::ne
<obws::serde::rgba8_inverse::tests::SimpleDuration as core::cmp::PartialEq>::eq
Line
Count
Source
78
2
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
<obws::serde::rgba8_inverse::tests::SimpleDuration as serde::ser::Serialize>::serialize::<&mut serde_test::ser::Serializer>
Line
Count
Source
78
1
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
<<obws::serde::rgba8_inverse::tests::SimpleDuration as serde::ser::Serialize>::serialize::__SerializeWith as serde::ser::Serialize>::serialize::<&mut serde_test::ser::Serializer>
Line
Count
Source
78
1
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
Unexecuted instantiation: <<obws::serde::rgba8_inverse::tests::SimpleDuration as serde::de::Deserialize>::deserialize::__Field as serde::de::Deserialize>::deserialize::<serde::de::value::StrDeserializer<serde_test::error::Error>>
<<obws::serde::rgba8_inverse::tests::SimpleDuration as serde::de::Deserialize>::deserialize::__Field as serde::de::Deserialize>::deserialize::<&mut serde_test::de::Deserializer>
Line
Count
Source
78
2
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
Unexecuted instantiation: <<<obws::serde::rgba8_inverse::tests::SimpleDuration 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::rgba8_inverse::tests::SimpleDuration as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::visit_str::<serde_test::error::Error>
Line
Count
Source
78
2
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
<<<obws::serde::rgba8_inverse::tests::SimpleDuration 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
78
2
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
Unexecuted instantiation: <<obws::serde::rgba8_inverse::tests::SimpleDuration as serde::de::Deserialize>::deserialize::__Field as serde::de::Deserialize>::deserialize::<serde_test::de::BytesDeserializer>
Unexecuted instantiation: <<obws::serde::rgba8_inverse::tests::SimpleDuration as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::expecting
Unexecuted instantiation: <<obws::serde::rgba8_inverse::tests::SimpleDuration as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::<serde_test::de::DeserializerSeqVisitor>::{closure#0}
Unexecuted instantiation: <<<obws::serde::rgba8_inverse::tests::SimpleDuration 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::rgba8_inverse::tests::SimpleDuration as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<serde_test::de::EnumMapVisitor>
Unexecuted instantiation: <<obws::serde::rgba8_inverse::tests::SimpleDuration as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::expecting
<obws::serde::rgba8_inverse::tests::SimpleDuration as serde::de::Deserialize>::deserialize::<&mut serde_test::de::Deserializer>
Line
Count
Source
78
2
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
<<obws::serde::rgba8_inverse::tests::SimpleDuration as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_map::<serde_test::de::DeserializerMapVisitor>
Line
Count
Source
78
4
    #[derive(Debug, PartialEq, Serialize, 
D2
eserialize)]
Unexecuted instantiation: <<<obws::serde::rgba8_inverse::tests::SimpleDuration 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>>
Unexecuted instantiation: <<obws::serde::rgba8_inverse::tests::SimpleDuration as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::visit_bytes::<serde_test::error::Error>
Unexecuted instantiation: <<obws::serde::rgba8_inverse::tests::SimpleDuration as serde::de::Deserialize>::deserialize::__Visitor as serde::de::Visitor>::visit_seq::<serde_test::de::DeserializerSeqVisitor>
Unexecuted instantiation: <<obws::serde::rgba8_inverse::tests::SimpleDuration as serde::de::Deserialize>::deserialize::__FieldVisitor as serde::de::Visitor>::visit_u64::<serde_test::error::Error>
Unexecuted instantiation: <<obws::serde::rgba8_inverse::tests::SimpleDuration as serde::de::Deserialize>::deserialize::__Field as serde::de::Deserialize>::deserialize::<serde::de::value::U32Deserializer<serde_test::error::Error>>
79
    struct SimpleDuration {
80
        #[serde(with = "super")]
81
        value: RGBA8,
82
    }
83
84
1
    #[test]
85
1
    fn roundtrip() {
86
1
        assert_tokens(
87
1
            &SimpleDuration {
88
1
                value: RGBA8::new(1, 2, 3, 4),
89
1
            },
90
1
            &[
91
1
                Token::Struct {
92
1
                    name: "SimpleDuration",
93
1
                    len: 1,
94
1
                },
95
1
                Token::Str("value"),
96
1
                Token::U32(0x04030201),
97
1
                Token::StructEnd,
98
1
            ],
99
1
        );
100
1
    }
101
}