tailwind_ast/utils/
mod.rs

1use nom::{
2    bytes::complete::tag,
3    character::complete::{char, digit1},
4    combinator::{map_res, opt, recognize},
5    sequence::tuple,
6    IResult,
7};
8use std::str::FromStr;
9#[cfg(test)]
10mod tests;
11
12/// `\d+`
13pub fn parse_integer<T>(input: &str) -> IResult<&str, T>
14where
15    T: FromStr,
16{
17    map_res(recognize(digit1), str::parse)(input)
18}
19///
20pub fn parse_i_px_maybe<T>(input: &str) -> IResult<&str, T>
21where
22    T: FromStr,
23{
24    let (rest, (i, _)) = tuple((parse_integer, opt(tag("px"))))(input)?;
25    Ok((rest, i))
26}
27
28/// `\d+\.\d+`
29pub fn parse_f32(input: &str) -> IResult<&str, f32> {
30    let float1 = tuple((digit1, opt(tuple((tag("."), digit1)))));
31    map_res(recognize(float1), str::parse)(input)
32}
33///
34pub fn parse_f_percent(input: &str) -> IResult<&str, f32> {
35    let (rest, (f, _)) = tuple((parse_f32, char('%')))(input)?;
36    Ok((rest, f))
37}
38
39/// `\d+\/\d+`
40pub fn parse_fraction(input: &str) -> IResult<&str, (usize, usize)> {
41    let (rest, (a, _, b)) = tuple((parse_integer, tag("/"), parse_integer))(input)?;
42    Ok((rest, (a, b)))
43}
44
45/// 100(/50)?
46#[inline]
47pub fn parse_fraction_maybe(input: &str) -> IResult<&str, (usize, Option<usize>)> {
48    let (rest, (a, b)) = tuple((parse_integer, opt(tuple((tag("/"), parse_integer)))))(input)?;
49    Ok((rest, (a, b.map(|s| s.1))))
50}
51
52/// #50d71e
53pub fn parser_color_hex() {}
54
55// fn hex3() {
56//
57// }
58//
59// fn hex6() {
60//
61// }
62//
63// fn hex8() {
64//
65// }