static_graph/parser/
graph.rs

1use nom::{bytes::complete::tag, combinator::map, sequence::tuple, IResult};
2
3use super::{blank, ident::Ident, Parser};
4
5#[derive(Debug, Clone)]
6pub struct Graph {
7    pub name: Ident,
8    pub entry_node: Ident,
9}
10
11impl<'a> Parser<'a> for Graph {
12    fn parse(input: &'a str) -> IResult<&'a str, Self> {
13        map(
14            tuple((
15                tag("graph"),
16                blank,
17                Ident::parse,
18                tag("("),
19                Ident::parse,
20                tag(")"),
21            )),
22            |(_, _, name, _, entry, _)| Graph {
23                name,
24                entry_node: entry,
25            },
26        )(input)
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn test_graph() {
36        let input = "graph Foo(Bar)";
37        match super::Graph::parse(input) {
38            Ok((remain, graph)) => {
39                assert_eq!(remain, "");
40                assert_eq!(graph.name.0, "Foo");
41                assert_eq!(graph.entry_node.0, "Bar");
42            }
43            Err(e) => panic!("Error: {e:?}"),
44        }
45    }
46}