static_graph/parser/
path.rs

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