1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use super::*;
pub fn parse_integer<T>(input: &str) -> IResult<&str, T>
where
T: FromStr,
{
map_res(recognize(digit1), str::parse)(input)
}
pub fn parse_i_px_maybe<T>(input: &str) -> IResult<&str, T>
where
T: FromStr,
{
let (rest, (i, _)) = tuple((parse_integer, opt(tag("px"))))(input)?;
Ok((rest, i))
}
#[test]
fn test_isize() {
assert_eq!(parse_integer("0"), Ok(("", 0isize)));
assert_eq!(parse_integer("42"), Ok(("", 42isize)));
}
#[test]
fn test_usize() {
assert_eq!(parse_integer("0"), Ok(("", 0usize)));
assert_eq!(parse_integer("42"), Ok(("", 42usize)));
}
pub fn parse_f32(input: &str) -> IResult<&str, f32> {
let float1 = tuple((digit1, opt(tuple((tag("."), digit1)))));
map_res(recognize(float1), str::parse)(input)
}
pub fn parse_f_percent(input: &str) -> IResult<&str, f32> {
let (rest, (f, _)) = tuple((parse_f32, char('%')))(input)?;
Ok((rest, f))
}
#[test]
fn test_f32() {
assert_eq!(parse_f32("0"), Ok(("", 0.0)));
assert_eq!(parse_f32("42"), Ok(("", 42.0)));
assert_eq!(parse_f32("99.99"), Ok(("", 99.99)));
}
pub fn parse_fraction(input: &str) -> IResult<&str, (usize, usize)> {
let (rest, (a, _, b)) = tuple((parse_integer, tag("/"), parse_integer))(input)?;
Ok((rest, (a, b)))
}
#[test]
fn test_fraction() {
assert_eq!(parse_fraction("1/12"), Ok(("", (1, 12))));
assert_eq!(parse_fraction("12/2"), Ok(("", (12, 2))));
assert_eq!(parse_fraction("12/24"), Ok(("", (12, 24))));
}
pub fn parser_color_hex() {}