Skip to content

Instantly share code, notes, and snippets.

@Geal
Created February 10, 2018 22:02
Show Gist options
  • Save Geal/5d792f2533bc7d1cd47bfca1ef9bf7fc to your computer and use it in GitHub Desktop.
Save Geal/5d792f2533bc7d1cd47bfca1ef9bf7fc to your computer and use it in GitHub Desktop.
#[macro_use]
extern crate nom;
use nom::is_hex_digit;
#[derive(Debug,PartialEq)]
pub struct Color {
pub red: u8,
pub green: u8,
pub blue: u8,
}
named!(hex_primary<u8>,
flat_map!(take_while_m_n!(2, 2, is_hex_digit), parse_to!(u8))
);
named!(hex_color<Color>,
do_parse!(
tag!("#") >>
red: hex_primary >>
green: hex_primary >>
blue: hex_primary >>
(Color { red, green, blue })
)
);
#[test]
fn parse_color() {
assert_eq!(hex_color(&b"#2F14DF"[..]), Ok((&b""[..], Color {
red: 47,
green: 20,
blue: 223,
})));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment