Sha256: ee0c553b9e68729a311f00cb49366cda96731d5d69d4d7454160371e01ab30e4

Contents?: true

Size: 822 Bytes

Versions: 8

Compression:

Stored size: 822 Bytes

Contents

use winnow::combinator::seq;
use winnow::prelude::*;
use winnow::token::take_while;

#[derive(Debug, Eq, PartialEq)]
pub(crate) struct Color {
    pub(crate) red: u8,
    pub(crate) green: u8,
    pub(crate) blue: u8,
}

impl std::str::FromStr for Color {
    // The error must be owned
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        hex_color.parse(s).map_err(|e| e.to_string())
    }
}

pub(crate) fn hex_color(input: &mut &str) -> PResult<Color> {
    seq!(Color {
        _: '#',
        red: hex_primary,
        green: hex_primary,
        blue: hex_primary
    })
    .parse_next(input)
}

fn hex_primary(input: &mut &str) -> PResult<u8> {
    take_while(2, |c: char| c.is_ascii_hexdigit())
        .try_map(|input| u8::from_str_radix(input, 16))
        .parse_next(input)
}

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/winnow-0.6.24/examples/css/parser.rs
wasmtime-28.0.0 ./ext/cargo-vendor/winnow-0.6.22/examples/css/parser.rs
wasmtime-27.0.0 ./ext/cargo-vendor/winnow-0.6.20/examples/css/parser.rs
wasmtime-26.0.0 ./ext/cargo-vendor/winnow-0.6.20/examples/css/parser.rs
wasmtime-25.0.2 ./ext/cargo-vendor/winnow-0.6.18/examples/css/parser.rs
wasmtime-25.0.1 ./ext/cargo-vendor/winnow-0.6.18/examples/css/parser.rs
wasmtime-25.0.0 ./ext/cargo-vendor/winnow-0.6.18/examples/css/parser.rs
wasmtime-24.0.0 ./ext/cargo-vendor/winnow-0.6.18/examples/css/parser.rs