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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use crate::DiagramIdToken;
use crate::{wr, ParseContainer};
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::bytes::complete::take_while1;
use nom::sequence::tuple;
use nom::IResult;

/// `"foo.puml"` or `"bar.iuml!buz"`
#[derive(Clone, Debug)]
pub struct IncludeSpecifierToken {
    filepath: ParseContainer,
    id: Option<DiagramIdToken>,
}

impl IncludeSpecifierToken {
    pub fn parse(input: ParseContainer) -> IResult<ParseContainer, (Vec<ParseContainer>, Self)> {
        let (rest, (raws, specifier)) = parse_specifier_part(input)?;

        Ok((rest, (raws, specifier)))
    }

    pub fn filepath(&self) -> &str {
        self.filepath.as_str()
    }

    pub fn index(&self) -> Option<usize> {
        self.id.as_ref().and_then(|x| x.id().parse().ok())
    }

    pub fn id(&self) -> Option<&str> {
        self.id.as_ref().map(|x| x.id())
    }
}

type ParsedResult = IResult<ParseContainer, (Vec<ParseContainer>, IncludeSpecifierToken)>;

/// parse like `"foo.puml"` or `"bar.iuml!buz"`
fn parse_specifier_part(input: ParseContainer) -> ParsedResult {
    alt((parse_file_with_id, parse_only_file))(input)
}

/// parse like `"foo.puml"`
///
/// TODO: accept escaped space charactor: like `"foo\ bar.puml"` ("\ "?)
fn parse_only_file(input: ParseContainer) -> ParsedResult {
    let (rest, filepath) = wr!(take_while1(|c: char| {
        !['!', ' ', '\t', '\n', '\r'].contains(&c)
    }))(input)?;

    let parsed_raw = Vec::from([filepath.clone()]);
    let parsed = IncludeSpecifierToken { filepath, id: None };
    Ok((rest, (parsed_raw, parsed)))
}

/// parse like `"bar.iuml!buz"`
fn parse_file_with_id(input: ParseContainer) -> ParsedResult {
    let (input, ((file_raw, file), tag, (id_raw, id))) =
        tuple((parse_only_file, wr!(tag("!")), DiagramIdToken::parse))(input)?;
    let parsed_raw = file_raw.into_iter().chain([tag, id_raw]).collect();
    let parsed = IncludeSpecifierToken {
        filepath: file.filepath,
        id: Some(id),
    };

    Ok((input, (parsed_raw, parsed)))
}

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

    #[test]
    fn test_parse() -> anyhow::Result<()> {
        let testdata = "foo.puml";
        let (rest, (parsed, include)) = IncludeSpecifierToken::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(parsed.join(""), testdata);
        assert_eq!(include.filepath(), "foo.puml");
        assert!(include.id().is_none());

        let testdata = "foo.puml!bar";
        let (rest, (parsed, include)) = IncludeSpecifierToken::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(parsed.join(""), testdata);
        assert_eq!(include.filepath(), "foo.puml");
        assert_eq!(include.id(), Some("bar"));

        Ok(())
    }
}