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
use std::sync::Arc;

use nom::{
    bytes::complete::tag,
    combinator::{map, opt},
    multi::separated_list1,
    sequence::tuple,
    IResult,
};

use super::{blank, ident::Ident, Parser};

#[derive(Debug, Clone)]
pub struct Path {
    pub segments: Arc<[Ident]>,
}

impl Parser for Path {
    fn parse(input: &str) -> IResult<&str, Self> {
        map(
            separated_list1(tuple((opt(blank), tag("::"), opt(blank))), Ident::parse),
            |idents| Path {
                segments: Arc::from(idents),
            },
        )(input)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_path() {
        let input = "Foo::Bar::Baz";
        match super::Path::parse(input) {
            Ok((remain, path)) => {
                assert_eq!(remain, "");
                assert_eq!(path.segments.len(), 3);
                assert_eq!(path.segments[0].0, "Foo");
                assert_eq!(path.segments[1].0, "Bar");
                assert_eq!(path.segments[2].0, "Baz");
            }
            Err(e) => panic!("Error: {e:?}"),
        }
    }
}