cosmic::cosmic_theme::palette::serde

Function deserialize_as_array

source
pub fn deserialize_as_array<'de, T, D>(
    deserializer: D,
) -> Result<T, <D as Deserializer<'de>>::Error>
where T: ArrayCast, <T as ArrayCast>::Array: Deserialize<'de>, D: Deserializer<'de>,
Expand description

Deserialize a value from an array of its components.

use serde::Deserialize;
use palette::{Srgb, Srgba};

#[derive(Deserialize, PartialEq, Debug)]
struct MyColors {
    #[serde(deserialize_with = "palette::serde::deserialize_as_array")]
    opaque: Srgb,
    #[serde(deserialize_with = "palette::serde::deserialize_as_array")]
    transparent: Srgba,
}

let my_colors = MyColors {
    opaque: Srgb::new(0.6, 0.8, 0.3),
    transparent: Srgba::new(0.6, 0.8, 0.3, 0.5),
};

let json = r#"{"opaque":[0.6,0.8,0.3],"transparent":[0.6,0.8,0.3,0.5]}"#;
assert_eq!(
    serde_json::from_str::<MyColors>(json).unwrap(),
    my_colors
);