Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address code review items.
  • Loading branch information
tiaanl committed Jan 23, 2023
commit 20826df169774901ccd4222ae3584d791d0926cd
39 changes: 17 additions & 22 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use std::f32::consts::PI;
use std::fmt;
use std::str::FromStr;

use super::{BasicParseError, ParseError, Parser, ToCss, Token};

Expand Down Expand Up @@ -388,19 +389,19 @@ impl PredefinedColorSpace {
}
}

impl std::str::FromStr for PredefinedColorSpace {
impl FromStr for PredefinedColorSpace {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s.to_lowercase().as_str() {
Ok(match_ignore_ascii_case! { s,
"srgb" => PredefinedColorSpace::Srgb,
"srgb-linear" => PredefinedColorSpace::SrgbLinear,
"display-p3" => PredefinedColorSpace::DisplayP3,
"a98-rgb" => PredefinedColorSpace::A98Rgb,
"prophoto-rgb" => PredefinedColorSpace::ProphotoRgb,
"rec2020" => PredefinedColorSpace::Rec2020,
"xyz-d50" => PredefinedColorSpace::XyzD50,
"xyz-d65" => PredefinedColorSpace::XyzD65,
"xyz" | "xyz-d65" => PredefinedColorSpace::XyzD65,

_ => return Err(()),
})
Expand All @@ -422,10 +423,7 @@ impl<'de> Deserialize<'de> for PredefinedColorSpace {
where
D: Deserializer<'de>,
{
use std::str::FromStr;

let s: &str = Deserialize::deserialize(deserializer)?;

PredefinedColorSpace::from_str(s)
.map_err(|_| serde::de::Error::custom("invalid predefined color space"))
}
Expand Down Expand Up @@ -477,7 +475,14 @@ impl Serialize for ColorFunction {
where
S: Serializer,
{
(self.color_space, self.c1, self.c2, self.c3, self.alpha).serialize(serializer)
(
self.color_space.as_str(),
self.c1,
self.c2,
self.c3,
self.alpha,
)
.serialize(serializer)
}
}

Expand All @@ -499,11 +504,11 @@ impl ToCss for ColorFunction {
{
dest.write_str("color(")?;
self.color_space.to_css(dest)?;
dest.write_str(" ")?;
dest.write_char(' ')?;
self.c1.to_css(dest)?;
dest.write_str(" ")?;
dest.write_char(' ')?;
self.c2.to_css(dest)?;
dest.write_str(" ")?;
dest.write_char(' ')?;
self.c3.to_css(dest)?;

serialize_alpha(dest, self.alpha, false)?;
Expand Down Expand Up @@ -1252,18 +1257,8 @@ where
let location = arguments.current_source_location();
let color_space = arguments.try_parse(|i| {
let ident = i.expect_ident()?;
Ok(match_ignore_ascii_case! {
ident,
"srgb" => PredefinedColorSpace::Srgb,
"srgb-linear" => PredefinedColorSpace::SrgbLinear,
"display-p3" => PredefinedColorSpace::DisplayP3,
"a98-rgb" => PredefinedColorSpace::A98Rgb,
"prophoto-rgb" => PredefinedColorSpace::ProphotoRgb,
"rec2020" => PredefinedColorSpace::Rec2020,
"xyz-d50" => PredefinedColorSpace::XyzD50,
"xyz" | "xyz-d65" => PredefinedColorSpace::XyzD65,
_ => return Err(location.new_unexpected_token_error(Token::Ident(ident.clone())))
})
PredefinedColorSpace::from_str(ident)
.map_err(|_| location.new_unexpected_token_error(Token::Ident(ident.clone())))
})?;

macro_rules! parse_component {
Expand Down