From dec01d514f9f0b40d0ffd975082b2908ef237ee9 Mon Sep 17 00:00:00 2001 From: Filipe Rodrigues Date: Sun, 4 Dec 2022 04:48:15 +0000 Subject: [PATCH] `Rect` now uses it's textual representation for (de)serializing. --- profile.json | 22 ++-------------------- zsw-util/src/rect.rs | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/profile.json b/profile.json index b1ee16e..ff9fa4d 100644 --- a/profile.json +++ b/profile.json @@ -4,16 +4,7 @@ "panels_shader": { "FadeWhite": { "strength": 10 } }, "panels": [ { - "geometry": { - "pos": { - "x": 0, - "y": 312 - }, - "size": { - "x": 1360, - "y": 768 - } - }, + "geometry": "1360x768+0+312", "duration": 1800, "fade_point": 1440, "parallax": { @@ -22,16 +13,7 @@ } }, { - "geometry": { - "pos": { - "x": 1360, - "y": 0 - }, - "size": { - "x": 1920, - "y": 1080 - } - }, + "geometry": "1920x1080+1360+0", "duration": 1800, "fade_point": 1440, "parallax": { diff --git a/zsw-util/src/rect.rs b/zsw-util/src/rect.rs index 7e3e204..c91f77d 100644 --- a/zsw-util/src/rect.rs +++ b/zsw-util/src/rect.rs @@ -4,12 +4,11 @@ use { anyhow::Context, cgmath::{num_traits::Num, Point2, Vector2}, - std::{error::Error, fmt}, + std::{borrow::Cow, error::Error, fmt}, }; /// A rectangle #[derive(PartialEq, Eq, Clone, Copy, Debug)] -#[derive(serde::Serialize, serde::Deserialize)] // TODO: serialize to string pub struct Rect { /// Position pub pos: Point2

, @@ -131,3 +130,22 @@ impl fmt::Display for Rect { Ok(()) } } + +impl<'de> serde::Deserialize<'de> for Rect { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let s = Cow::deserialize(deserializer)?; + Self::parse_from_geometry(&s).map_err(serde::de::Error::custom) + } +} + +impl serde::Serialize for Rect { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_str(&self.to_string()) + } +}