From 5dfe3e7328d456aa2deb857faffdeeffa42c73d6 Mon Sep 17 00:00:00 2001 From: Chipnertkj <32001812+chipnertkj@users.noreply.github.com> Date: Thu, 7 Dec 2023 02:01:45 +0100 Subject: [PATCH] Make color use serde, and update serialization to be derived. --- Cargo.toml | 8 +-- color/Cargo.toml | 6 ++- color/lib.rs | 123 +++++------------------------------------------ src/color.rs | 2 + 4 files changed, 23 insertions(+), 116 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c7beb4c1..ea04cb43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "cssparser" version = "0.33.0" -authors = [ "Simon Sapin " ] +authors = ["Simon Sapin "] description = "Rust implementation of CSS Syntax Level 3" documentation = "https://docs.rs/cssparser/" @@ -20,11 +20,11 @@ difference = "2.0" encoding_rs = "0.8" [dependencies] -cssparser-macros = {path = "./macros", version = "0.6.1"} +cssparser-macros = { path = "./macros", version = "0.6.1" } dtoa-short = "0.3" itoa = "1.0" -phf = {version = "0.11.2", features = ["macros"]} -serde = {version = "1.0", optional = true} +phf = { version = "0.11.2", features = ["macros"] } +serde = { version = "1.0", features = ["derive"], optional = true } smallvec = "1.0" [features] diff --git a/color/Cargo.toml b/color/Cargo.toml index d1e315a8..47544815 100644 --- a/color/Cargo.toml +++ b/color/Cargo.toml @@ -12,7 +12,11 @@ edition = "2021" path = "lib.rs" [dependencies] -cssparser = { version = "0.33", path = ".." } +cssparser = { path = ".." } +serde = { version = "1.0", features = ["derive"], optional = true } + +[features] +serde = ["cssparser/serde", "dep:serde"] [dev-dependencies] serde_json = "1.0.25" diff --git a/color/lib.rs b/color/lib.rs index 1c52e38d..cf30fd5b 100644 --- a/color/lib.rs +++ b/color/lib.rs @@ -16,8 +16,6 @@ use cssparser::color::{ PredefinedColorSpace, OPAQUE, }; use cssparser::{match_ignore_ascii_case, CowRcStr, ParseError, Parser, ToCss, Token}; -#[cfg(feature = "serde")] -use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::f32::consts::PI; use std::fmt; use std::str::FromStr; @@ -55,10 +53,8 @@ where let token = input.next()?; match *token { Token::Hash(ref value) | Token::IDHash(ref value) => { - parse_hash_color(value.as_bytes()).map(|(r, g, b, a)| { - P::Output::from_rgba(r, g, b, a) - }) - }, + parse_hash_color(value.as_bytes()).map(|(r, g, b, a)| P::Output::from_rgba(r, g, b, a)) + } Token::Ident(ref value) => parse_color_keyword(value), Token::Function(ref name) => { let name = name.clone(); @@ -506,6 +502,7 @@ fn normalize_hue(hue: f32) -> f32 { } /// A color with red, green, blue, and alpha components, in a byte each. +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[derive(Clone, Copy, PartialEq, Debug)] pub struct RgbaLegacy { /// The red component. @@ -544,27 +541,6 @@ impl RgbaLegacy { } } -#[cfg(feature = "serde")] -impl Serialize for RgbaLegacy { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - (self.red, self.green, self.blue, self.alpha).serialize(serializer) - } -} - -#[cfg(feature = "serde")] -impl<'de> Deserialize<'de> for RgbaLegacy { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let (r, g, b, a) = Deserialize::deserialize(deserializer)?; - Ok(RgbaLegacy::new(r, g, b, a)) - } -} - impl ToCss for RgbaLegacy { fn to_css(&self, dest: &mut W) -> fmt::Result where @@ -588,6 +564,7 @@ impl ToCss for RgbaLegacy { /// Color specified by hue, saturation and lightness components. #[derive(Clone, Copy, PartialEq, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Hsl { /// The hue component. pub hue: Option, @@ -632,29 +609,9 @@ impl ToCss for Hsl { } } -#[cfg(feature = "serde")] -impl Serialize for Hsl { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - (self.hue, self.saturation, self.lightness, self.alpha).serialize(serializer) - } -} - -#[cfg(feature = "serde")] -impl<'de> Deserialize<'de> for Hsl { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let (lightness, a, b, alpha) = Deserialize::deserialize(deserializer)?; - Ok(Self::new(lightness, a, b, alpha)) - } -} - /// Color specified by hue, whiteness and blackness components. #[derive(Clone, Copy, PartialEq, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Hwb { /// The hue component. pub hue: Option, @@ -699,32 +656,12 @@ impl ToCss for Hwb { } } -#[cfg(feature = "serde")] -impl Serialize for Hwb { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - (self.hue, self.whiteness, self.blackness, self.alpha).serialize(serializer) - } -} - -#[cfg(feature = "serde")] -impl<'de> Deserialize<'de> for Hwb { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let (lightness, whiteness, blackness, alpha) = Deserialize::deserialize(deserializer)?; - Ok(Self::new(lightness, whiteness, blackness, alpha)) - } -} - // NOTE: LAB and OKLAB is not declared inside the [impl_lab_like] macro, // because it causes cbindgen to ignore them. /// Color specified by lightness, a- and b-axis components. #[derive(Clone, Copy, PartialEq, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Lab { /// The lightness component. pub lightness: Option, @@ -738,6 +675,7 @@ pub struct Lab { /// Color specified by lightness, a- and b-axis components. #[derive(Clone, Copy, PartialEq, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Oklab { /// The lightness component. pub lightness: Option, @@ -768,27 +706,6 @@ macro_rules! impl_lab_like { } } - #[cfg(feature = "serde")] - impl Serialize for $cls { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - (self.lightness, self.a, self.b, self.alpha).serialize(serializer) - } - } - - #[cfg(feature = "serde")] - impl<'de> Deserialize<'de> for $cls { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let (lightness, a, b, alpha) = Deserialize::deserialize(deserializer)?; - Ok(Self::new(lightness, a, b, alpha)) - } - } - impl ToCss for $cls { fn to_css(&self, dest: &mut W) -> fmt::Result where @@ -816,6 +733,7 @@ impl_lab_like!(Oklab, "oklab"); /// Color specified by lightness, chroma and hue components. #[derive(Clone, Copy, PartialEq, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Lch { /// The lightness component. pub lightness: Option, @@ -829,6 +747,7 @@ pub struct Lch { /// Color specified by lightness, chroma and hue components. #[derive(Clone, Copy, PartialEq, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Oklch { /// The lightness component. pub lightness: Option, @@ -859,27 +778,6 @@ macro_rules! impl_lch_like { } } - #[cfg(feature = "serde")] - impl Serialize for $cls { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - (self.lightness, self.chroma, self.hue, self.alpha).serialize(serializer) - } - } - - #[cfg(feature = "serde")] - impl<'de> Deserialize<'de> for $cls { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let (lightness, chroma, hue, alpha) = Deserialize::deserialize(deserializer)?; - Ok(Self::new(lightness, chroma, hue, alpha)) - } - } - impl ToCss for $cls { fn to_css(&self, dest: &mut W) -> fmt::Result where @@ -905,6 +803,7 @@ impl_lch_like!(Oklch, "oklch"); /// A color specified by the color() function. /// #[derive(Clone, Copy, PartialEq, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct ColorFunction { /// The color space for this color. pub color_space: PredefinedColorSpace, @@ -966,6 +865,8 @@ impl ToCss for ColorFunction { /// /// #[derive(Clone, Copy, PartialEq, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "serde", serde(tag = "type"))] pub enum Color { /// The 'currentcolor' keyword. CurrentColor, diff --git a/src/color.rs b/src/color.rs index d5f9a5c0..f4d11809 100644 --- a/src/color.rs +++ b/src/color.rs @@ -77,6 +77,8 @@ pub fn serialize_color_alpha( /// A Predefined color space specified in: /// #[derive(Clone, Copy, PartialEq, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "serde", serde(tag = "type"))] pub enum PredefinedColorSpace { /// Srgb,