Skip to content

Commit

Permalink
Make PredefinedColorSpace follow the convention of parse() and ToCss (#…
Browse files Browse the repository at this point in the history
…386)

Also bump version to 0.34, because this is a breaking change.
  • Loading branch information
tiaanl authored Apr 16, 2024
1 parent ca69d95 commit cda06ee
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cssparser"
version = "0.33.0"
version = "0.34.0"
authors = ["Simon Sapin <[email protected]>"]

description = "Rust implementation of CSS Syntax Level 3"
Expand All @@ -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"

Expand Down
9 changes: 1 addition & 8 deletions color/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion color/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
}
Expand Down
57 changes: 25 additions & 32 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<Self, BasicParseError<'i>> {
let location = input.current_source_location();

fn from_str(s: &str) -> Result<Self, Self::Err> {
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()))),
})
}
}
Expand All @@ -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",
})
}
}

Expand Down

0 comments on commit cda06ee

Please sign in to comment.