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
use nom::{bytes::complete::tag, combinator::map, sequence::tuple, IResult};
use super::{blank, ident::Ident, Parser};
#[derive(Debug, Clone)]
pub struct Graph {
pub name: Ident,
pub entry_node: Ident,
}
impl Parser for Graph {
fn parse(input: &str) -> IResult<&str, Self> {
map(
tuple((
tag("graph"),
blank,
Ident::parse,
tag("("),
Ident::parse,
tag(")"),
)),
|(_, _, name, _, entry, _)| Graph {
name,
entry_node: entry,
},
)(input)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_graph() {
let input = "graph Foo(Bar)";
match super::Graph::parse(input) {
Ok((remain, graph)) => {
assert_eq!(remain, "");
assert_eq!(graph.name.0, "Foo");
assert_eq!(graph.entry_node.0, "Bar");
}
Err(e) => panic!("Error: {e:?}"),
}
}
}