Rect now uses it's textual representation for (de)serializing.

This commit is contained in:
Filipe Rodrigues 2022-12-04 04:48:15 +00:00
parent 67266f28f9
commit dec01d514f
2 changed files with 22 additions and 22 deletions

View File

@ -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": {

View File

@ -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<P, S = P> {
/// Position
pub pos: Point2<P>,
@ -131,3 +130,22 @@ impl fmt::Display for Rect<i32, u32> {
Ok(())
}
}
impl<'de> serde::Deserialize<'de> for Rect<i32, u32> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<i32, u32> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.to_string())
}
}