tf_parser/parser/
stipple_parser.rs

1use super::base_parser::{boolean_number, positive_number, qstring, ws};
2
3use crate::{model::TfStipple, TfRes};
4use nom::{
5    bytes::complete::tag,
6    error::context,
7    multi::separated_list1,
8    sequence::{delimited, preceded, tuple},
9};
10
11fn pattern_list(input: &str) -> TfRes<&str, Vec<bool>> {
12    delimited(
13        ws(tag("(")),
14        separated_list1(tag(","), boolean_number),
15        ws(tag(")")),
16    )(input)
17}
18
19pub fn stipple_parser(input: &str) -> TfRes<&str, TfStipple> {
20    context(
21        "Stipple Section",
22        tuple((
23            preceded(ws(tag("Stipple")), qstring),
24            delimited(
25                ws(tag("{")),
26                tuple((
27                    preceded(tuple((ws(tag("width")), ws(tag("=")))), positive_number),
28                    preceded(tuple((ws(tag("height")), ws(tag("=")))), positive_number),
29                    preceded(tuple((ws(tag("pattern")), ws(tag("=")))), pattern_list),
30                )),
31                ws(tag("}")),
32            ),
33        )),
34    )(input)
35    .map(|(res, (stipple_name, data))| {
36        (
37            res,
38            TfStipple {
39                name: stipple_name.to_string(),
40                width: data.0,
41                height: data.1,
42                pattern: data.2,
43            },
44        )
45    })
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    #[test]
52    fn test_stipple() {
53        let input = "Stipple       \"rhidot\" {
54        width           = 16
55        height          = 16
56        pattern         = (0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
57                       1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
58                       0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
59                       1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
60                       0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
61                       1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
62                       0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
63                       1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
64                       0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
65                       1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
66                       0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
67                       1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
68                       0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
69                       1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
70                       0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
71                       1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0) 
72}";
73        let (_, _) = stipple_parser(input).unwrap();
74    }
75}