Skip to content

Commit 70265fa

Browse files
committed
Make PredefinedColorSpace follow the convention of parse() and ToCss
Also bump version to 0.34, because this is a breaking change.
1 parent 8f13fc9 commit 70265fa

File tree

4 files changed

+32
-44
lines changed

4 files changed

+32
-44
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
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

color/lib.rs

Lines changed: 1 addition & 8 deletions
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
///
@@ -401,13 +400,7 @@ fn parse_color_with_color_space<'i, 't, P>(
401400
where
402401
P: ColorParser<'i>,
403402
{
404-
let color_space = {
405-
let location = arguments.current_source_location();
406-
407-
let ident = arguments.expect_ident()?;
408-
PredefinedColorSpace::from_str(ident)
409-
.map_err(|_| location.new_unexpected_token_error(Token::Ident(ident.clone())))?
410-
};
403+
let color_space = PredefinedColorSpace::parse(arguments)?;
411404

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

color/tests.rs

Lines changed: 1 addition & 1 deletion
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
}

src/color.rs

Lines changed: 28 additions & 33 deletions
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,37 +98,24 @@ 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(()),
132-
})
105+
match *input.next()? {
106+
ref t @ Token::Ident(ref ident) => 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(t.clone())),
116+
}),
117+
ref t => return Err(location.new_basic_unexpected_token_error(t.clone())),
118+
}
133119
}
134120
}
135121

@@ -138,7 +124,16 @@ impl ToCss for PredefinedColorSpace {
138124
where
139125
W: fmt::Write,
140126
{
141-
dest.write_str(self.as_str())
127+
dest.write_str(match self {
128+
Self::Srgb => "srgb",
129+
Self::SrgbLinear => "srgb-linear",
130+
Self::DisplayP3 => "display-p3",
131+
Self::A98Rgb => "a98-rgb",
132+
Self::ProphotoRgb => "prophoto-rgb",
133+
Self::Rec2020 => "rec2020",
134+
Self::XyzD50 => "xyz-d50",
135+
Self::XyzD65 => "xyz-d65",
136+
})
142137
}
143138
}
144139

0 commit comments

Comments
 (0)