Skip to content

Commit

Permalink
Make color use serde, and update serialization to be derived.
Browse files Browse the repository at this point in the history
  • Loading branch information
chipnertkj authored and emilio committed Apr 6, 2024
1 parent e89af28 commit 5dfe3e7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 116 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "cssparser"
version = "0.33.0"
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]
authors = ["Simon Sapin <simon.sapin@exyr.org>"]

description = "Rust implementation of CSS Syntax Level 3"
documentation = "https://docs.rs/cssparser/"
Expand All @@ -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]
Expand Down
6 changes: 5 additions & 1 deletion color/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
123 changes: 12 additions & 111 deletions color/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -544,27 +541,6 @@ impl RgbaLegacy {
}
}

#[cfg(feature = "serde")]
impl Serialize for RgbaLegacy {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
(self.red, self.green, self.blue, self.alpha).serialize(serializer)
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for RgbaLegacy {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<W>(&self, dest: &mut W) -> fmt::Result
where
Expand All @@ -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<f32>,
Expand Down Expand Up @@ -632,29 +609,9 @@ impl ToCss for Hsl {
}
}

#[cfg(feature = "serde")]
impl Serialize for Hsl {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
(self.hue, self.saturation, self.lightness, self.alpha).serialize(serializer)
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Hsl {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<f32>,
Expand Down Expand Up @@ -699,32 +656,12 @@ impl ToCss for Hwb {
}
}

#[cfg(feature = "serde")]
impl Serialize for Hwb {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
(self.hue, self.whiteness, self.blackness, self.alpha).serialize(serializer)
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Hwb {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<f32>,
Expand All @@ -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<f32>,
Expand Down Expand Up @@ -768,27 +706,6 @@ macro_rules! impl_lab_like {
}
}

#[cfg(feature = "serde")]
impl Serialize for $cls {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
(self.lightness, self.a, self.b, self.alpha).serialize(serializer)
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for $cls {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<W>(&self, dest: &mut W) -> fmt::Result
where
Expand Down Expand Up @@ -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<f32>,
Expand All @@ -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<f32>,
Expand Down Expand Up @@ -859,27 +778,6 @@ macro_rules! impl_lch_like {
}
}

#[cfg(feature = "serde")]
impl Serialize for $cls {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
(self.lightness, self.chroma, self.hue, self.alpha).serialize(serializer)
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for $cls {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<W>(&self, dest: &mut W) -> fmt::Result
where
Expand All @@ -905,6 +803,7 @@ impl_lch_like!(Oklch, "oklch");
/// A color specified by the color() function.
/// <https://drafts.csswg.org/css-color-4/#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,
Expand Down Expand Up @@ -966,6 +865,8 @@ impl ToCss for ColorFunction {
///
/// <https://drafts.csswg.org/css-color-4/#color-type>
#[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,
Expand Down
2 changes: 2 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ pub fn serialize_color_alpha(
/// A Predefined color space specified in:
/// <https://drafts.csswg.org/css-color-4/#predefined>
#[derive(Clone, Copy, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
pub enum PredefinedColorSpace {
/// <https://drafts.csswg.org/css-color-4/#predefined-sRGB>
Srgb,
Expand Down

0 comments on commit 5dfe3e7

Please sign in to comment.