diff --git a/Cargo.toml b/Cargo.toml index 6630f7f4..f783f19f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cssparser" -version = "0.33.0" +version = "0.34.0" authors = ["Simon Sapin "] description = "Rust implementation of CSS Syntax Level 3" @@ -23,7 +23,7 @@ encoding_rs = "0.8" cssparser-macros = { path = "./macros", version = "0.6.1" } dtoa-short = "0.3" itoa = "1.0" -phf = { version = "0.11.2", features = ["macros"] } +phf = { version = "0.11.2", features = ["macros"] } serde = { version = "1.0", features = ["derive"], optional = true } smallvec = "1.0" diff --git a/color/lib.rs b/color/lib.rs index 83d0f279..5b116c23 100644 --- a/color/lib.rs +++ b/color/lib.rs @@ -18,7 +18,6 @@ use cssparser::color::{ use cssparser::{match_ignore_ascii_case, CowRcStr, ParseError, Parser, ToCss, Token}; use std::f32::consts::PI; use std::fmt; -use std::str::FromStr; /// Return the named color with the given name. /// @@ -402,13 +401,7 @@ fn parse_color_with_color_space<'i, 't, P>( where P: ColorParser<'i>, { - let color_space = { - let location = arguments.current_source_location(); - - let ident = arguments.expect_ident()?; - PredefinedColorSpace::from_str(ident) - .map_err(|_| location.new_unexpected_token_error(Token::Ident(ident.clone())))? - }; + let color_space = PredefinedColorSpace::parse(arguments)?; let (c1, c2, c3, alpha) = parse_components( color_parser, diff --git a/color/tests.rs b/color/tests.rs index 85eb430e..d8d8e5d4 100644 --- a/color/tests.rs +++ b/color/tests.rs @@ -215,7 +215,7 @@ impl ToJson for Color { Color::Oklab(ref c) => json!([c.lightness, c.a, c.b, c.alpha]), Color::Oklch(ref c) => json!([c.lightness, c.chroma, c.hue, c.alpha]), Color::ColorFunction(ref c) => { - json!([c.color_space.as_str(), c.c1, c.c2, c.c3, c.alpha]) + json!([c.color_space.to_css_string(), c.c1, c.c2, c.c3, c.alpha]) } } } diff --git a/src/color.rs b/src/color.rs index e3ae926d..978936e0 100644 --- a/src/color.rs +++ b/src/color.rs @@ -14,9 +14,8 @@ /// The opaque alpha value of 1.0. pub const OPAQUE: f32 = 1.0; -use crate::ToCss; +use crate::{BasicParseError, Parser, ToCss, Token}; use std::fmt; -use std::str::FromStr; /// Clamp a 0..1 number to a 0..255 range to u8. /// @@ -99,36 +98,21 @@ pub enum PredefinedColorSpace { } impl PredefinedColorSpace { - /// Returns the string value of the predefined color space. - pub fn as_str(&self) -> &str { - match self { - PredefinedColorSpace::Srgb => "srgb", - PredefinedColorSpace::SrgbLinear => "srgb-linear", - PredefinedColorSpace::DisplayP3 => "display-p3", - PredefinedColorSpace::A98Rgb => "a98-rgb", - PredefinedColorSpace::ProphotoRgb => "prophoto-rgb", - PredefinedColorSpace::Rec2020 => "rec2020", - PredefinedColorSpace::XyzD50 => "xyz-d50", - PredefinedColorSpace::XyzD65 => "xyz-d65", - } - } -} - -impl FromStr for PredefinedColorSpace { - type Err = (); + /// Parse a PredefinedColorSpace from the given input. + pub fn parse<'i>(input: &mut Parser<'i, '_>) -> Result> { + let location = input.current_source_location(); - fn from_str(s: &str) -> Result { - 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" | "xyz-d65" => PredefinedColorSpace::XyzD65, - - _ => return Err(()), + let ident = input.expect_ident()?; + Ok(match_ignore_ascii_case! { ident, + "srgb" => Self::Srgb, + "srgb-linear" => Self::SrgbLinear, + "display-p3" => Self::DisplayP3, + "a98-rgb" => Self::A98Rgb, + "prophoto-rgb" => Self::ProphotoRgb, + "rec2020" => Self::Rec2020, + "xyz-d50" => Self::XyzD50, + "xyz" | "xyz-d65" => Self::XyzD65, + _ => return Err(location.new_basic_unexpected_token_error(Token::Ident(ident.clone()))), }) } } @@ -138,7 +122,16 @@ impl ToCss for PredefinedColorSpace { where W: fmt::Write, { - dest.write_str(self.as_str()) + dest.write_str(match self { + Self::Srgb => "srgb", + Self::SrgbLinear => "srgb-linear", + Self::DisplayP3 => "display-p3", + Self::A98Rgb => "a98-rgb", + Self::ProphotoRgb => "prophoto-rgb", + Self::Rec2020 => "rec2020", + Self::XyzD50 => "xyz-d50", + Self::XyzD65 => "xyz-d65", + }) } }