Skip to content

Commit cda06ee

Browse files
authored
Make PredefinedColorSpace follow the convention of parse() and ToCss (#386)
Also bump version to 0.34, because this is a breaking change.
1 parent ca69d95 commit cda06ee

File tree

4 files changed

+29
-43
lines changed

4 files changed

+29
-43
lines changed

Diff for: Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cssparser"
3-
version = "0.33.0"
3+
version = "0.34.0"
44
authors = ["Simon Sapin <[email protected]>"]
55

66
description = "Rust implementation of CSS Syntax Level 3"
@@ -23,7 +23,7 @@ encoding_rs = "0.8"
2323
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"] }
26+
phf = { version = "0.11.2", features = ["macros"] }
2727
serde = { version = "1.0", features = ["derive"], optional = true }
2828
smallvec = "1.0"
2929

Diff for: color/lib.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use cssparser::color::{
1818
use cssparser::{match_ignore_ascii_case, CowRcStr, ParseError, Parser, ToCss, Token};
1919
use std::f32::consts::PI;
2020
use std::fmt;
21-
use std::str::FromStr;
2221

2322
/// Return the named color with the given name.
2423
///
@@ -402,13 +401,7 @@ fn parse_color_with_color_space<'i, 't, P>(
402401
where
403402
P: ColorParser<'i>,
404403
{
405-
let color_space = {
406-
let location = arguments.current_source_location();
407-
408-
let ident = arguments.expect_ident()?;
409-
PredefinedColorSpace::from_str(ident)
410-
.map_err(|_| location.new_unexpected_token_error(Token::Ident(ident.clone())))?
411-
};
404+
let color_space = PredefinedColorSpace::parse(arguments)?;
412405

413406
let (c1, c2, c3, alpha) = parse_components(
414407
color_parser,

Diff for: color/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl ToJson for Color {
215215
Color::Oklab(ref c) => json!([c.lightness, c.a, c.b, c.alpha]),
216216
Color::Oklch(ref c) => json!([c.lightness, c.chroma, c.hue, c.alpha]),
217217
Color::ColorFunction(ref c) => {
218-
json!([c.color_space.as_str(), c.c1, c.c2, c.c3, c.alpha])
218+
json!([c.color_space.to_css_string(), c.c1, c.c2, c.c3, c.alpha])
219219
}
220220
}
221221
}

Diff for: src/color.rs

+25-32
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
/// The opaque alpha value of 1.0.
1515
pub const OPAQUE: f32 = 1.0;
1616

17-
use crate::ToCss;
17+
use crate::{BasicParseError, Parser, ToCss, Token};
1818
use std::fmt;
19-
use std::str::FromStr;
2019

2120
/// Clamp a 0..1 number to a 0..255 range to u8.
2221
///
@@ -99,36 +98,21 @@ pub enum PredefinedColorSpace {
9998
}
10099

101100
impl PredefinedColorSpace {
102-
/// Returns the string value of the predefined color space.
103-
pub fn as_str(&self) -> &str {
104-
match self {
105-
PredefinedColorSpace::Srgb => "srgb",
106-
PredefinedColorSpace::SrgbLinear => "srgb-linear",
107-
PredefinedColorSpace::DisplayP3 => "display-p3",
108-
PredefinedColorSpace::A98Rgb => "a98-rgb",
109-
PredefinedColorSpace::ProphotoRgb => "prophoto-rgb",
110-
PredefinedColorSpace::Rec2020 => "rec2020",
111-
PredefinedColorSpace::XyzD50 => "xyz-d50",
112-
PredefinedColorSpace::XyzD65 => "xyz-d65",
113-
}
114-
}
115-
}
116-
117-
impl FromStr for PredefinedColorSpace {
118-
type Err = ();
101+
/// Parse a PredefinedColorSpace from the given input.
102+
pub fn parse<'i>(input: &mut Parser<'i, '_>) -> Result<Self, BasicParseError<'i>> {
103+
let location = input.current_source_location();
119104

120-
fn from_str(s: &str) -> Result<Self, Self::Err> {
121-
Ok(match_ignore_ascii_case! { s,
122-
"srgb" => PredefinedColorSpace::Srgb,
123-
"srgb-linear" => PredefinedColorSpace::SrgbLinear,
124-
"display-p3" => PredefinedColorSpace::DisplayP3,
125-
"a98-rgb" => PredefinedColorSpace::A98Rgb,
126-
"prophoto-rgb" => PredefinedColorSpace::ProphotoRgb,
127-
"rec2020" => PredefinedColorSpace::Rec2020,
128-
"xyz-d50" => PredefinedColorSpace::XyzD50,
129-
"xyz" | "xyz-d65" => PredefinedColorSpace::XyzD65,
130-
131-
_ => return Err(()),
105+
let ident = input.expect_ident()?;
106+
Ok(match_ignore_ascii_case! { ident,
107+
"srgb" => Self::Srgb,
108+
"srgb-linear" => Self::SrgbLinear,
109+
"display-p3" => Self::DisplayP3,
110+
"a98-rgb" => Self::A98Rgb,
111+
"prophoto-rgb" => Self::ProphotoRgb,
112+
"rec2020" => Self::Rec2020,
113+
"xyz-d50" => Self::XyzD50,
114+
"xyz" | "xyz-d65" => Self::XyzD65,
115+
_ => return Err(location.new_basic_unexpected_token_error(Token::Ident(ident.clone()))),
132116
})
133117
}
134118
}
@@ -138,7 +122,16 @@ impl ToCss for PredefinedColorSpace {
138122
where
139123
W: fmt::Write,
140124
{
141-
dest.write_str(self.as_str())
125+
dest.write_str(match self {
126+
Self::Srgb => "srgb",
127+
Self::SrgbLinear => "srgb-linear",
128+
Self::DisplayP3 => "display-p3",
129+
Self::A98Rgb => "a98-rgb",
130+
Self::ProphotoRgb => "prophoto-rgb",
131+
Self::Rec2020 => "rec2020",
132+
Self::XyzD50 => "xyz-d50",
133+
Self::XyzD65 => "xyz-d65",
134+
})
142135
}
143136
}
144137

0 commit comments

Comments
 (0)