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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! This module contains a few serialization shims for types from `palette` crate.
//! It is required due to buggy default implementation of those types.
//! See this issue for more details: https://github.com/Ogeon/palette/issues/130
//! When above issue will be resolved, this can probably be removed.
#![allow(clippy::many_single_char_names)]
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Srgb serialization shim.
/// ```
/// # use serde::{Serialize, Deserialize};
/// #[derive(Serialize, Deserialize)]
/// struct MyType(
///     #[serde(with="amethyst_rendy::serde_shim::srgb")]
///     pub palette::Srgb
/// );
/// ```
pub mod srgb {
    use super::*;
    #[derive(Serialize, Deserialize)]
    struct Srgb(f32, f32, f32);

    /// Serialize Srgba type as tuple struct with three floats
    pub fn serialize<S: Serializer>(x: &palette::Srgb, s: S) -> Result<S::Ok, S::Error> {
        let (r, g, b) = x.into_components();
        Srgb(r, g, b).serialize(s)
    }

    /// Deserialize Srgba type as tuple struct with three floats
    pub fn deserialize<'de, D: Deserializer<'de>>(de: D) -> Result<palette::Srgb, D::Error> {
        let t = Srgb::deserialize(de)?;
        Ok(palette::Srgb::new(t.0, t.1, t.2))
    }
}

/// Srgba serialization shim.
/// ```
/// # use serde::{Serialize, Deserialize};
/// #[derive(Serialize, Deserialize)]
/// struct MyType(
///     #[serde(with="amethyst_rendy::serde_shim::srgba")]
///     pub palette::Srgba
/// );
/// ```
pub mod srgba {
    use super::*;
    #[derive(Serialize, Deserialize)]
    struct Srgba(f32, f32, f32, f32);

    /// Serialize Srgba type as tuple struct with four floats
    pub fn serialize<S: Serializer>(x: &palette::Srgba, s: S) -> Result<S::Ok, S::Error> {
        let (r, g, b, a) = x.into_components();
        Srgba(r, g, b, a).serialize(s)
    }

    /// Deserialize Srgba type as tuple struct with four floats
    pub fn deserialize<'de, D: Deserializer<'de>>(de: D) -> Result<palette::Srgba, D::Error> {
        let t = Srgba::deserialize(de)?;
        Ok(palette::Srgba::new(t.0, t.1, t.2, t.3))
    }
}

#[cfg(test)]
mod test {
    use serde::{Deserialize, Serialize};

    #[derive(Serialize, Deserialize)]
    struct MySrgbaWrapper(#[serde(with = "crate::serde_shim::srgba")] palette::Srgba);

    #[derive(Serialize, Deserialize)]
    struct MySrgbWrapper(#[serde(with = "crate::serde_shim::srgb")] palette::Srgb);

    #[test]
    fn deserialize_srgba() {
        let de: MySrgbaWrapper =
            ron::de::from_str("MySrgbaWrapper(Srgba(0.1, 0.2, 0.3, 0.4))").unwrap();
        assert_eq!(de.0, palette::Srgba::new(0.1, 0.2, 0.3, 0.4))
    }
    #[test]
    fn deserialize_srgb() {
        let de: MySrgbWrapper = ron::de::from_str("MySrgbWrapper(Srgb(0.1, 0.2, 0.3))").unwrap();
        assert_eq!(de.0, palette::Srgb::new(0.1, 0.2, 0.3))
    }
    #[test]
    fn serialize_srgb() {
        let val = MySrgbWrapper(palette::Srgb::new(0.1, 0.2, 0.3));
        assert_eq!(
            ron::ser::to_string_pretty(&val, Default::default()).unwrap(),
            "((0.1, 0.2, 0.3))"
        )
    }
    #[test]
    fn serialize_srgba() {
        let val = MySrgbaWrapper(palette::Srgba::new(0.1, 0.2, 0.3, 0.4));
        assert_eq!(
            ron::ser::to_string_pretty(&val, Default::default()).unwrap(),
            "((0.1, 0.2, 0.3, 0.4))"
        )
    }
}