14
14
/// The opaque alpha value of 1.0.
15
15
pub const OPAQUE : f32 = 1.0 ;
16
16
17
- use crate :: ToCss ;
17
+ use crate :: { BasicParseError , Parser , ToCss , Token } ;
18
18
use std:: fmt;
19
- use std:: str:: FromStr ;
20
19
21
20
/// Clamp a 0..1 number to a 0..255 range to u8.
22
21
///
@@ -99,36 +98,21 @@ pub enum PredefinedColorSpace {
99
98
}
100
99
101
100
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 ( ) ;
119
104
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( ) ) ) ) ,
132
116
} )
133
117
}
134
118
}
@@ -138,7 +122,16 @@ impl ToCss for PredefinedColorSpace {
138
122
where
139
123
W : fmt:: Write ,
140
124
{
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
+ } )
142
135
}
143
136
}
144
137
0 commit comments