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,37 +98,24 @@ 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 ( ( ) ) ,
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
+ }
133
119
}
134
120
}
135
121
@@ -138,7 +124,16 @@ impl ToCss for PredefinedColorSpace {
138
124
where
139
125
W : fmt:: Write ,
140
126
{
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
+ } )
142
137
}
143
138
}
144
139
0 commit comments