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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
mod empty;
mod end;
mod include;
mod start;

pub use empty::EmptyLine;
pub use end::EndLine;
pub use include::IncludeLine;
pub use start::StartLine;

use crate::{wr, ParseContainer};
use nom::branch::alt;
use nom::bytes::complete::take_till;
use nom::character::complete::line_ending;
use nom::combinator::eof;
use nom::sequence::tuple;
use nom::IResult;

#[derive(Clone, Debug)]
pub struct PlantUmlLine {
    content: PlantUmlLineKind,
    raw: ParseContainer,
}

#[derive(Clone, Debug)]
pub enum PlantUmlLineKind {
    /// `@start[a-z]*(id=MY_OWN_ID)` | `@start[a-z]* MY_OWN_ID` (e.g. `@startuml`)
    Start(StartLine),

    /// `@end[a-z]*` (e.g. `@enduml`)
    End(EndLine),

    /// `!include <filepath>` | `!include <filepath>!<id>` (e.g. `!include foo.puml!1`)
    Include(IncludeLine),

    /// empty line
    Empty,

    /// others
    Others,
}

impl PlantUmlLine {
    pub fn parse(input: ParseContainer) -> IResult<ParseContainer, Self> {
        let (raw, content) = PlantUmlLineKind::parse(input.clone())?;

        let (rest, raw) = input.split_at(raw.len());
        let ret = (rest, Self { content, raw });
        Ok(ret)
    }

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

    pub fn start(&self) -> Option<&StartLine> {
        if let PlantUmlLineKind::Start(x) = &self.content {
            Some(x)
        } else {
            None
        }
    }

    pub fn end(&self) -> Option<&EndLine> {
        if let PlantUmlLineKind::End(x) = &self.content {
            Some(x)
        } else {
            None
        }
    }

    pub fn include(&self) -> Option<&IncludeLine> {
        if let PlantUmlLineKind::Include(x) = &self.content {
            Some(x)
        } else {
            None
        }
    }

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

    pub fn empty(&self) -> Option<()> {
        if let PlantUmlLineKind::Empty = &self.content {
            Some(())
        } else {
            None
        }
    }
}

impl PlantUmlLineKind {
    fn parse(input: ParseContainer) -> IResult<ParseContainer, Self> {
        if let Ok((_, (raws, content))) = StartLine::parse(input.clone()) {
            let content = PlantUmlLineKind::Start(content);
            return Ok((raws.into(), content));
        }

        if let Ok((_, (raws, content))) = EndLine::parse(input.clone()) {
            let content = PlantUmlLineKind::End(content);
            return Ok((raws.into(), content));
        }

        if let Ok((_, (raws, content))) = IncludeLine::parse(input.clone()) {
            let content = PlantUmlLineKind::Include(content);
            return Ok((raws.into(), content));
        }

        if let Ok((_, (raws, _))) = EmptyLine::parse(input.clone()) {
            let content = PlantUmlLineKind::Empty;
            return Ok((raws.into(), content));
        }

        let (_, raws) = Self::parse_others(input.clone())?;
        let content = PlantUmlLineKind::Others;
        Ok((raws.into(), content))
    }

    fn parse_others(input: ParseContainer) -> IResult<ParseContainer, Vec<ParseContainer>> {
        let (rest, (not_end, end)) = tuple((
            wr!(take_till(|c| c == '\n' || c == '\r')),
            alt((wr!(eof), wr!(line_ending))),
        ))(input)?;
        Ok((rest, vec![not_end, end]))
    }
}

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

    #[test]
    fn test_parse_start_line() -> anyhow::Result<()> {
        let testdata = "@startuml\n";
        let (rest, parsed) = PlantUmlLine::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(testdata, parsed.raw);
        assert!(parsed.start().is_some());

        let testdata = " \t@startuml\n";
        let (rest, parsed) = PlantUmlLine::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(testdata, parsed.raw);
        assert!(parsed.start().is_some());

        let testdata = "@startuml \t\n";
        let (rest, parsed) = PlantUmlLine::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(testdata, parsed.raw);
        assert!(parsed.start().is_some());

        let testdata = " \t@startuml \t\n";
        let (rest, parsed) = PlantUmlLine::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(testdata, parsed.raw);
        assert!(parsed.start().is_some());

        let testdata = " \t@startuml id_foo\t\n";
        let (rest, parsed) = PlantUmlLine::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(testdata, parsed.raw);
        assert!(parsed.start().is_some());

        let testdata = " \t@startuml(id=id_bar)\t\n";
        let (rest, parsed) = PlantUmlLine::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(testdata, parsed.raw);
        assert!(parsed.start().is_some());

        Ok(())
    }

    #[test]
    fn test_parse_end_line() -> anyhow::Result<()> {
        let testdata = "@enduml\n";
        let (rest, parsed) = PlantUmlLine::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(testdata, parsed.raw);
        assert!(parsed.end().is_some());

        let testdata = " \t@enduml\n";
        let (rest, parsed) = PlantUmlLine::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(testdata, parsed.raw);
        assert!(parsed.end().is_some());

        let testdata = "@enduml \t\n";
        let (rest, parsed) = PlantUmlLine::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(testdata, parsed.raw);
        assert!(parsed.end().is_some());

        let testdata = " \t@enduml \t\n";
        let (rest, parsed) = PlantUmlLine::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(testdata, parsed.raw);
        assert!(parsed.end().is_some());

        Ok(())
    }

    #[test]
    fn test_parse_include_line() -> anyhow::Result<()> {
        let testdata = "!include foo.puml\n";
        let (rest, parsed) = PlantUmlLine::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(testdata, parsed.raw);
        assert!(parsed.include().is_some());

        let testdata = " !include foo.puml!1\n";
        let (rest, parsed) = PlantUmlLine::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(testdata, parsed.raw);
        assert!(parsed.include().is_some());

        let testdata = "\t!include foo.puml!bar \n";
        let (rest, parsed) = PlantUmlLine::parse(testdata.into())?;
        assert_eq!(rest, "");
        assert_eq!(testdata, parsed.raw);
        assert!(parsed.include().is_some());

        Ok(())
    }
}