Debian Codegraph @

debian/rust-vendor/winnow/examples/css/parser.rs

raw
1 use winnow::combinator::seq;
2 use winnow::prelude::*;
3 use winnow::token::take_while;
4 use winnow::Result;
5
6 #[derive(Debug, Eq, PartialEq)]
7 pub(crate) struct Color {
8 pub(crate) red: u8,
9 pub(crate) green: u8,
10 pub(crate) blue: u8,
11 }
12
13 impl std::str::FromStr for Color {
14 // The error must be owned
15 type Err = String;
16
17 fn from_str(s: &str) -> Result<Self, Self::Err> {
18 hex_color.parse(s).map_err(|e| e.to_string())
19 }
20 }
21
22 pub(crate) fn hex_color(input: &mut &str) -> Result<Color> {
23 seq!(Color {
24 _: '#',
25 red: hex_primary,
26 green: hex_primary,
27 blue: hex_primary
28 })
29 .parse_next(input)
30 }
31
32 fn hex_primary(input: &mut &str) -> Result<u8> {
33 take_while(2, |c: char| c.is_ascii_hexdigit())
34 .try_map(|input| u8::from_str_radix(input, 16))
35 .parse_next(input)
36 }