From 803b34641d5861b62b1a73e4048d571166d51d19 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 18 Mar 2020 14:38:17 +0100 Subject: [PATCH] library: material: uniform: add deserialization --- src/material/uniform.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/material/uniform.rs b/src/material/uniform.rs index 3bc9b74..3cb7b8b 100644 --- a/src/material/uniform.rs +++ b/src/material/uniform.rs @@ -1,9 +1,10 @@ use super::Material; use crate::core::color::LinearColor; use crate::Point2D; +use serde::Deserialize; /// A material with the same characteristics on all points. -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Deserialize)] pub struct UniformMaterial { diffuse: LinearColor, specular: LinearColor, @@ -85,4 +86,22 @@ mod test { let mat = simple_material(); assert!(mat.reflectivity(Point2D::origin()) - 0.5 < std::f32::EPSILON) } + + #[test] + fn deserialization_works() { + let yaml = r#" + diffuse: {r: 1.0, g: 0.5, b: 0.25} + specular: {r: 0.25, g: 0.125, b: 0.75} + reflectivity: 0.25 + "#; + let material: UniformMaterial = serde_yaml::from_str(yaml).unwrap(); + assert_eq!( + material, + UniformMaterial::new( + LinearColor::new(1., 0.5, 0.25), + LinearColor::new(0.25, 0.125, 0.75), + 0.25 + ) + ) + } }