From 42abfed3fa02cbd4d705131cccd81ab0c1fd492b Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 18 Mar 2020 14:35:53 +0100 Subject: [PATCH] library: light: directional: add deserialization --- src/light/directional_light.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/light/directional_light.rs b/src/light/directional_light.rs index 9c937d6..0168340 100644 --- a/src/light/directional_light.rs +++ b/src/light/directional_light.rs @@ -1,10 +1,12 @@ use super::{Light, SpatialLight}; use crate::core::LinearColor; use crate::{Point, Vector}; +use serde::Deserialize; /// Represent a light emanating from a far away source, with parallel rays on all points. -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Deserialize)] pub struct DirectionalLight { + #[serde(deserialize_with = "crate::serialize::vector_normalizer")] direction: Vector, color: LinearColor, } @@ -63,4 +65,14 @@ mod test { let expected = (Vector::new(-1., 0., 0.), std::f32::INFINITY); assert_eq!(ans, expected) } + + #[test] + fn deserialization_works() { + let yaml = "{direction: [1.0, 0.0, 0.0], color: {r: 1.0, g: 0.5, b: 0.2}}"; + let light: DirectionalLight = serde_yaml::from_str(yaml).unwrap(); + assert_eq!( + light, + DirectionalLight::new(Vector::new(1., 0., 0.), LinearColor::new(1., 0.5, 0.2)) + ) + } }