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