Skip to content

Commit 48e517b

Browse files
committed
Use Result/Err(()) instead of Option/None to indicate a parse error.
1 parent 329ee2f commit 48e517b

File tree

4 files changed

+56
-56
lines changed

4 files changed

+56
-56
lines changed

color.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ pub enum Color {
3131
}
3232

3333

34-
// Return None on invalid/unsupported value (not a color)
34+
/// Return `Err(())` on invalid or unsupported value (not a color).
3535
impl Color {
36-
pub fn parse(component_value: &ComponentValue) -> Option<Color> {
36+
pub fn parse(component_value: &ComponentValue) -> Result<Color, ()> {
3737
match *component_value {
3838
Hash(ref value) | IDHash(ref value) => parse_color_hash(value.as_slice()),
3939
Ident(ref value) => parse_color_keyword(value.as_slice()),
4040
Function(ref name, ref arguments)
4141
=> parse_color_function(name.as_slice(), arguments.as_slice()),
42-
_ => None
42+
_ => Err(())
4343
}
4444
}
4545
}
4646

4747

4848
#[inline]
49-
fn parse_color_keyword(value: &str) -> Option<Color> {
49+
fn parse_color_keyword(value: &str) -> Result<Color, ()> {
5050
let lower_value = value.to_ascii_lower();
5151
let (r, g, b) = match lower_value.as_slice() {
5252
"black" => (0., 0., 0.),
@@ -199,33 +199,33 @@ fn parse_color_keyword(value: &str) -> Option<Color> {
199199
"whitesmoke" => (245., 245., 245.),
200200
"yellowgreen" => (154., 205., 50.),
201201

202-
"transparent" => return Some(RGBA(RGBA { red: 0., green: 0., blue: 0., alpha: 0. })),
203-
"currentcolor" => return Some(CurrentColor),
204-
_ => return None,
202+
"transparent" => return Ok(RGBA(RGBA { red: 0., green: 0., blue: 0., alpha: 0. })),
203+
"currentcolor" => return Ok(CurrentColor),
204+
_ => return Err(()),
205205
};
206-
Some(RGBA(RGBA { red: r / 255., green: g / 255., blue: b / 255., alpha: 1. }))
206+
Ok(RGBA(RGBA { red: r / 255., green: g / 255., blue: b / 255., alpha: 1. }))
207207
}
208208

209209

210210
#[inline]
211-
fn parse_color_hash(value: &str) -> Option<Color> {
211+
fn parse_color_hash(value: &str) -> Result<Color, ()> {
212212
macro_rules! from_hex(
213213
($c: expr) => {{
214214
let c = $c;
215215
match c {
216216
'0' .. '9' => c as u8 - ('0' as u8),
217217
'a' .. 'f' => c as u8 - ('a' as u8) + 10,
218218
'A' .. 'F' => c as u8 - ('A' as u8) + 10,
219-
_ => return None // Not a valid color
219+
_ => return Err(()) // Not a valid color
220220
}
221221
}};
222222
)
223223
macro_rules! to_rgba(
224224
($r: expr, $g: expr, $b: expr,) => {
225-
Some(RGBA(RGBA { red: $r as f32 / 255.,
226-
green: $g as f32 / 255.,
227-
blue: $b as f32 / 255.,
228-
alpha: 1. }))
225+
Ok(RGBA(RGBA { red: $r as f32 / 255.,
226+
green: $g as f32 / 255.,
227+
blue: $b as f32 / 255.,
228+
alpha: 1. }))
229229
};
230230
)
231231

@@ -240,14 +240,14 @@ fn parse_color_hash(value: &str) -> Option<Color> {
240240
from_hex!(value.char_at(1)) * 17,
241241
from_hex!(value.char_at(2)) * 17,
242242
),
243-
_ => None
243+
_ => Err(())
244244
}
245245
}
246246

247247

248248
#[inline]
249249
fn parse_color_function(name: &str, arguments: &[ComponentValue])
250-
-> Option<Color> {
250+
-> Result<Color, ()> {
251251
let lower_name = name.to_ascii_lower();
252252
let lower_name = lower_name.as_slice();
253253

@@ -256,28 +256,28 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue])
256256
else if "rgb" == lower_name { (true, false) }
257257
else if "hsl" == lower_name { (false, false) }
258258
else if "hsla" == lower_name { (false, true) }
259-
else { return None };
259+
else { return Err(()) };
260260

261261
let mut iter = arguments.skip_whitespace();
262262
macro_rules! expect_comma(
263-
() => ( match iter.next() { Some(&Comma) => {}, _ => { return None } } );
263+
() => ( match iter.next() { Some(&Comma) => {}, _ => { return Err(()) } } );
264264
)
265265
macro_rules! expect_percentage(
266266
() => ( match iter.next() {
267267
Some(&Percentage(ref v)) => v.value,
268-
_ => return None,
268+
_ => return Err(()),
269269
});
270270
)
271271
macro_rules! expect_integer(
272272
() => ( match iter.next() {
273273
Some(&Number(ref v)) if v.int_value.is_some() => v.value,
274-
_ => return None,
274+
_ => return Err(()),
275275
});
276276
)
277277
macro_rules! expect_number(
278278
() => ( match iter.next() {
279279
Some(&Number(ref v)) => v.value,
280-
_ => return None,
280+
_ => return Err(()),
281281
});
282282
)
283283

@@ -301,7 +301,7 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue])
301301
expect_comma!();
302302
blue = (expect_percentage!() / 100.) as f32;
303303
}
304-
_ => return None
304+
_ => return Err(())
305305
};
306306
} else {
307307
let hue = expect_number!() / 360.;
@@ -336,8 +336,8 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue])
336336
1.
337337
};
338338
if iter.next().is_none() {
339-
Some(RGBA(RGBA { red: red, green: green, blue: blue, alpha: alpha }))
339+
Ok(RGBA(RGBA { red: red, green: green, blue: blue, alpha: alpha }))
340340
} else {
341-
None
341+
Err(())
342342
}
343343
}

nth.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ use std::ascii::StrAsciiExt;
77
use ast::*;
88

99

10-
/// Parse the An+B notation, as found in the ``:nth-child()`` selector.
10+
/// Parse the *An+B* notation, as found in the `:nth-child()` selector.
1111
/// The input is typically the arguments of a function component value.
12-
/// Return Some((A, B)), or None for a syntax error.
13-
pub fn parse_nth(input: &[ComponentValue]) -> Option<(i32, i32)> {
12+
/// Return `Ok((A, B))`, or `Err(())` for a syntax error.
13+
pub fn parse_nth(input: &[ComponentValue]) -> Result<(i32, i32), ()> {
1414
let iter = &mut input.skip_whitespace();
1515
match iter.next() {
1616
Some(&Number(ref value)) => match value.int_value {
1717
Some(b) => parse_end(iter, 0, b as i32),
18-
_ => None,
18+
_ => Err(()),
1919
},
2020
Some(&Dimension(ref value, ref unit)) => match value.int_value {
2121
Some(a) => {
@@ -26,11 +26,11 @@ pub fn parse_nth(input: &[ComponentValue]) -> Option<(i32, i32)> {
2626
"n-" => parse_signless_b(iter, a as i32, -1),
2727
_ => match parse_n_dash_digits(unit) {
2828
Some(b) => parse_end(iter, a as i32, b),
29-
_ => None
29+
_ => Err(())
3030
},
3131
}
3232
},
33-
_ => None,
33+
_ => Err(()),
3434
},
3535
Some(&Ident(ref value)) => {
3636
let ident = value.as_slice().to_ascii_lower();
@@ -44,11 +44,11 @@ pub fn parse_nth(input: &[ComponentValue]) -> Option<(i32, i32)> {
4444
"-n-" => parse_signless_b(iter, -1, -1),
4545
_ if ident.starts_with("-") => match parse_n_dash_digits(ident.slice_from(1)) {
4646
Some(b) => parse_end(iter, -1, b),
47-
_ => None
47+
_ => Err(())
4848
},
4949
_ => match parse_n_dash_digits(ident) {
5050
Some(b) => parse_end(iter, 1, b),
51-
_ => None
51+
_ => Err(())
5252
},
5353
}
5454
},
@@ -61,47 +61,47 @@ pub fn parse_nth(input: &[ComponentValue]) -> Option<(i32, i32)> {
6161
"n-" => parse_signless_b(iter, 1, -1),
6262
_ => match parse_n_dash_digits(ident) {
6363
Some(b) => parse_end(iter, 1, b),
64-
_ => None
64+
_ => Err(())
6565
},
6666
}
6767
},
68-
_ => None
68+
_ => Err(())
6969
},
70-
_ => None
70+
_ => Err(())
7171
}
7272
}
7373

7474

75-
type Nth = Option<(i32, i32)>;
75+
type Nth = Result<(i32, i32), ()>;
7676
type Iter<'a> = SkipWhitespaceIterator<'a>;
7777

7878
fn parse_b(iter: &mut Iter, a: i32) -> Nth {
7979
match iter.next() {
80-
None => Some((a, 0)),
80+
None => Ok((a, 0)),
8181
Some(&Delim('+')) => parse_signless_b(iter, a, 1),
8282
Some(&Delim('-')) => parse_signless_b(iter, a, -1),
8383
Some(&Number(ref value)) => match value.int_value {
8484
Some(b) if has_sign(value) => parse_end(iter, a, b as i32),
85-
_ => None,
85+
_ => Err(()),
8686
},
87-
_ => None
87+
_ => Err(())
8888
}
8989
}
9090

9191
fn parse_signless_b(iter: &mut Iter, a: i32, b_sign: i32) -> Nth {
9292
match iter.next() {
9393
Some(&Number(ref value)) => match value.int_value {
9494
Some(b) if !has_sign(value) => parse_end(iter, a, b_sign * (b as i32)),
95-
_ => None,
95+
_ => Err(()),
9696
},
97-
_ => None
97+
_ => Err(())
9898
}
9999
}
100100

101101
fn parse_end(iter: &mut Iter, a: i32, b: i32) -> Nth {
102102
match iter.next() {
103-
None => Some((a, b)),
104-
Some(_) => None,
103+
None => Ok((a, b)),
104+
Some(_) => Err(()),
105105
}
106106
}
107107

tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn stylesheet_from_bytes() {
199199
fn run_color_tests(json_data: &str, to_json: |result: Option<Color>| -> json::Json) {
200200
run_json_tests(json_data, |input| {
201201
match parse_one_component_value(tokenize(input)) {
202-
Ok(component_value) => to_json(Color::parse(&component_value)),
202+
Ok(component_value) => to_json(Color::parse(&component_value).ok()),
203203
Err(_reason) => json::Null,
204204
}
205205
});
@@ -235,28 +235,28 @@ fn color3_keywords() {
235235
#[bench]
236236
fn bench_color_lookup_red(b: &mut test::Bencher) {
237237
let ident = parse_one_component_value(tokenize("red")).unwrap();
238-
b.iter(|| assert!(Color::parse(&ident).is_some()));
238+
b.iter(|| assert!(Color::parse(&ident).is_ok()));
239239
}
240240

241241

242242
#[bench]
243243
fn bench_color_lookup_lightgoldenrodyellow(b: &mut test::Bencher) {
244244
let ident = parse_one_component_value(tokenize("lightgoldenrodyellow")).unwrap();
245-
b.iter(|| assert!(Color::parse(&ident).is_some()));
245+
b.iter(|| assert!(Color::parse(&ident).is_ok()));
246246
}
247247

248248

249249
#[bench]
250250
fn bench_color_lookup_fail(b: &mut test::Bencher) {
251251
let ident = parse_one_component_value(tokenize("lightgoldenrodyellowbazinga")).unwrap();
252-
b.iter(|| assert!(Color::parse(&ident).is_none()));
252+
b.iter(|| assert!(Color::parse(&ident).is_err()));
253253
}
254254

255255

256256
#[test]
257257
fn nth() {
258258
run_json_tests(include_str!("css-parsing-tests/An+B.json"), |input| {
259-
parse_nth(tokenize(input).map(|(c, _)| c).collect::<Vec<ComponentValue>>().as_slice())
259+
parse_nth(tokenize(input).map(|(c, _)| c).collect::<Vec<ComponentValue>>().as_slice()).ok()
260260
});
261261
}
262262

tokenizer.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,14 @@ fn consume_block_with_location(tokenizer: &mut Tokenizer, ending_token: Componen
307307

308308
fn consume_string(tokenizer: &mut Tokenizer, single_quote: bool) -> ComponentValue {
309309
match consume_quoted_string(tokenizer, single_quote) {
310-
Some(value) => String(value),
311-
None => BadString
310+
Ok(value) => String(value),
311+
Err(()) => BadString
312312
}
313313
}
314314

315315

316-
// Return None on syntax error (ie. unescaped newline)
317-
fn consume_quoted_string(tokenizer: &mut Tokenizer, single_quote: bool) -> Option<String> {
316+
/// Return `Err(())` on syntax error (ie. unescaped newline)
317+
fn consume_quoted_string(tokenizer: &mut Tokenizer, single_quote: bool) -> Result<String, ()> {
318318
tokenizer.position += 1; // Skip the initial quote
319319
let mut string = String::new();
320320
while !tokenizer.is_eof() {
@@ -323,7 +323,7 @@ fn consume_quoted_string(tokenizer: &mut Tokenizer, single_quote: bool) -> Optio
323323
'\'' if single_quote => break,
324324
'\n' => {
325325
tokenizer.position -= 1;
326-
return None;
326+
return Err(());
327327
},
328328
'\\' => {
329329
if !tokenizer.is_eof() {
@@ -338,7 +338,7 @@ fn consume_quoted_string(tokenizer: &mut Tokenizer, single_quote: bool) -> Optio
338338
c => string.push_char(c),
339339
}
340340
}
341-
Some(string)
341+
Ok(string)
342342
}
343343

344344

@@ -475,8 +475,8 @@ fn consume_url(tokenizer: &mut Tokenizer) -> ComponentValue {
475475

476476
fn consume_quoted_url(tokenizer: &mut Tokenizer, single_quote: bool) -> ComponentValue {
477477
match consume_quoted_string(tokenizer, single_quote) {
478-
Some(value) => consume_url_end(tokenizer, value),
479-
None => consume_bad_url(tokenizer),
478+
Ok(value) => consume_url_end(tokenizer, value),
479+
Err(()) => consume_bad_url(tokenizer),
480480
}
481481
}
482482

0 commit comments

Comments
 (0)