Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5dfe3e7

Browse files
chipnertkjemilio
authored andcommittedApr 6, 2024··
Make color use serde, and update serialization to be derived.
1 parent e89af28 commit 5dfe3e7

File tree

4 files changed

+23
-116
lines changed

4 files changed

+23
-116
lines changed
 

‎Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "cssparser"
33
version = "0.33.0"
4-
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]
4+
authors = ["Simon Sapin <simon.sapin@exyr.org>"]
55

66
description = "Rust implementation of CSS Syntax Level 3"
77
documentation = "https://docs.rs/cssparser/"
@@ -20,11 +20,11 @@ difference = "2.0"
2020
encoding_rs = "0.8"
2121

2222
[dependencies]
23-
cssparser-macros = {path = "./macros", version = "0.6.1"}
23+
cssparser-macros = { path = "./macros", version = "0.6.1" }
2424
dtoa-short = "0.3"
2525
itoa = "1.0"
26-
phf = {version = "0.11.2", features = ["macros"]}
27-
serde = {version = "1.0", optional = true}
26+
phf = { version = "0.11.2", features = ["macros"] }
27+
serde = { version = "1.0", features = ["derive"], optional = true }
2828
smallvec = "1.0"
2929

3030
[features]

‎color/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ edition = "2021"
1212
path = "lib.rs"
1313

1414
[dependencies]
15-
cssparser = { version = "0.33", path = ".." }
15+
cssparser = { path = ".." }
16+
serde = { version = "1.0", features = ["derive"], optional = true }
17+
18+
[features]
19+
serde = ["cssparser/serde", "dep:serde"]
1620

1721
[dev-dependencies]
1822
serde_json = "1.0.25"

‎color/lib.rs

+12-111
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use cssparser::color::{
1616
PredefinedColorSpace, OPAQUE,
1717
};
1818
use cssparser::{match_ignore_ascii_case, CowRcStr, ParseError, Parser, ToCss, Token};
19-
#[cfg(feature = "serde")]
20-
use serde::{Deserialize, Deserializer, Serialize, Serializer};
2119
use std::f32::consts::PI;
2220
use std::fmt;
2321
use std::str::FromStr;
@@ -55,10 +53,8 @@ where
5553
let token = input.next()?;
5654
match *token {
5755
Token::Hash(ref value) | Token::IDHash(ref value) => {
58-
parse_hash_color(value.as_bytes()).map(|(r, g, b, a)| {
59-
P::Output::from_rgba(r, g, b, a)
60-
})
61-
},
56+
parse_hash_color(value.as_bytes()).map(|(r, g, b, a)| P::Output::from_rgba(r, g, b, a))
57+
}
6258
Token::Ident(ref value) => parse_color_keyword(value),
6359
Token::Function(ref name) => {
6460
let name = name.clone();
@@ -506,6 +502,7 @@ fn normalize_hue(hue: f32) -> f32 {
506502
}
507503

508504
/// A color with red, green, blue, and alpha components, in a byte each.
505+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
509506
#[derive(Clone, Copy, PartialEq, Debug)]
510507
pub struct RgbaLegacy {
511508
/// The red component.
@@ -544,27 +541,6 @@ impl RgbaLegacy {
544541
}
545542
}
546543

547-
#[cfg(feature = "serde")]
548-
impl Serialize for RgbaLegacy {
549-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
550-
where
551-
S: Serializer,
552-
{
553-
(self.red, self.green, self.blue, self.alpha).serialize(serializer)
554-
}
555-
}
556-
557-
#[cfg(feature = "serde")]
558-
impl<'de> Deserialize<'de> for RgbaLegacy {
559-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
560-
where
561-
D: Deserializer<'de>,
562-
{
563-
let (r, g, b, a) = Deserialize::deserialize(deserializer)?;
564-
Ok(RgbaLegacy::new(r, g, b, a))
565-
}
566-
}
567-
568544
impl ToCss for RgbaLegacy {
569545
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
570546
where
@@ -588,6 +564,7 @@ impl ToCss for RgbaLegacy {
588564

589565
/// Color specified by hue, saturation and lightness components.
590566
#[derive(Clone, Copy, PartialEq, Debug)]
567+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
591568
pub struct Hsl {
592569
/// The hue component.
593570
pub hue: Option<f32>,
@@ -632,29 +609,9 @@ impl ToCss for Hsl {
632609
}
633610
}
634611

635-
#[cfg(feature = "serde")]
636-
impl Serialize for Hsl {
637-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
638-
where
639-
S: Serializer,
640-
{
641-
(self.hue, self.saturation, self.lightness, self.alpha).serialize(serializer)
642-
}
643-
}
644-
645-
#[cfg(feature = "serde")]
646-
impl<'de> Deserialize<'de> for Hsl {
647-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
648-
where
649-
D: Deserializer<'de>,
650-
{
651-
let (lightness, a, b, alpha) = Deserialize::deserialize(deserializer)?;
652-
Ok(Self::new(lightness, a, b, alpha))
653-
}
654-
}
655-
656612
/// Color specified by hue, whiteness and blackness components.
657613
#[derive(Clone, Copy, PartialEq, Debug)]
614+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
658615
pub struct Hwb {
659616
/// The hue component.
660617
pub hue: Option<f32>,
@@ -699,32 +656,12 @@ impl ToCss for Hwb {
699656
}
700657
}
701658

702-
#[cfg(feature = "serde")]
703-
impl Serialize for Hwb {
704-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
705-
where
706-
S: Serializer,
707-
{
708-
(self.hue, self.whiteness, self.blackness, self.alpha).serialize(serializer)
709-
}
710-
}
711-
712-
#[cfg(feature = "serde")]
713-
impl<'de> Deserialize<'de> for Hwb {
714-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
715-
where
716-
D: Deserializer<'de>,
717-
{
718-
let (lightness, whiteness, blackness, alpha) = Deserialize::deserialize(deserializer)?;
719-
Ok(Self::new(lightness, whiteness, blackness, alpha))
720-
}
721-
}
722-
723659
// NOTE: LAB and OKLAB is not declared inside the [impl_lab_like] macro,
724660
// because it causes cbindgen to ignore them.
725661

726662
/// Color specified by lightness, a- and b-axis components.
727663
#[derive(Clone, Copy, PartialEq, Debug)]
664+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
728665
pub struct Lab {
729666
/// The lightness component.
730667
pub lightness: Option<f32>,
@@ -738,6 +675,7 @@ pub struct Lab {
738675

739676
/// Color specified by lightness, a- and b-axis components.
740677
#[derive(Clone, Copy, PartialEq, Debug)]
678+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
741679
pub struct Oklab {
742680
/// The lightness component.
743681
pub lightness: Option<f32>,
@@ -768,27 +706,6 @@ macro_rules! impl_lab_like {
768706
}
769707
}
770708

771-
#[cfg(feature = "serde")]
772-
impl Serialize for $cls {
773-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
774-
where
775-
S: Serializer,
776-
{
777-
(self.lightness, self.a, self.b, self.alpha).serialize(serializer)
778-
}
779-
}
780-
781-
#[cfg(feature = "serde")]
782-
impl<'de> Deserialize<'de> for $cls {
783-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
784-
where
785-
D: Deserializer<'de>,
786-
{
787-
let (lightness, a, b, alpha) = Deserialize::deserialize(deserializer)?;
788-
Ok(Self::new(lightness, a, b, alpha))
789-
}
790-
}
791-
792709
impl ToCss for $cls {
793710
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
794711
where
@@ -816,6 +733,7 @@ impl_lab_like!(Oklab, "oklab");
816733

817734
/// Color specified by lightness, chroma and hue components.
818735
#[derive(Clone, Copy, PartialEq, Debug)]
736+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
819737
pub struct Lch {
820738
/// The lightness component.
821739
pub lightness: Option<f32>,
@@ -829,6 +747,7 @@ pub struct Lch {
829747

830748
/// Color specified by lightness, chroma and hue components.
831749
#[derive(Clone, Copy, PartialEq, Debug)]
750+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
832751
pub struct Oklch {
833752
/// The lightness component.
834753
pub lightness: Option<f32>,
@@ -859,27 +778,6 @@ macro_rules! impl_lch_like {
859778
}
860779
}
861780

862-
#[cfg(feature = "serde")]
863-
impl Serialize for $cls {
864-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
865-
where
866-
S: Serializer,
867-
{
868-
(self.lightness, self.chroma, self.hue, self.alpha).serialize(serializer)
869-
}
870-
}
871-
872-
#[cfg(feature = "serde")]
873-
impl<'de> Deserialize<'de> for $cls {
874-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
875-
where
876-
D: Deserializer<'de>,
877-
{
878-
let (lightness, chroma, hue, alpha) = Deserialize::deserialize(deserializer)?;
879-
Ok(Self::new(lightness, chroma, hue, alpha))
880-
}
881-
}
882-
883781
impl ToCss for $cls {
884782
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
885783
where
@@ -905,6 +803,7 @@ impl_lch_like!(Oklch, "oklch");
905803
/// A color specified by the color() function.
906804
/// <https://drafts.csswg.org/css-color-4/#color-function>
907805
#[derive(Clone, Copy, PartialEq, Debug)]
806+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
908807
pub struct ColorFunction {
909808
/// The color space for this color.
910809
pub color_space: PredefinedColorSpace,
@@ -966,6 +865,8 @@ impl ToCss for ColorFunction {
966865
///
967866
/// <https://drafts.csswg.org/css-color-4/#color-type>
968867
#[derive(Clone, Copy, PartialEq, Debug)]
868+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
869+
#[cfg_attr(feature = "serde", serde(tag = "type"))]
969870
pub enum Color {
970871
/// The 'currentcolor' keyword.
971872
CurrentColor,

‎src/color.rs

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ pub fn serialize_color_alpha(
7777
/// A Predefined color space specified in:
7878
/// <https://drafts.csswg.org/css-color-4/#predefined>
7979
#[derive(Clone, Copy, PartialEq, Debug)]
80+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
81+
#[cfg_attr(feature = "serde", serde(tag = "type"))]
8082
pub enum PredefinedColorSpace {
8183
/// <https://drafts.csswg.org/css-color-4/#predefined-sRGB>
8284
Srgb,

0 commit comments

Comments
 (0)
Please sign in to comment.