plantuml_parser/dsl/line/
block_comment_open.rs

1use crate::{ParseContainer, ParseResult, wr};
2use nom::Parser;
3use nom::branch::alt;
4use nom::bytes::complete::{tag, take_till};
5use nom::character::complete::{line_ending, space0};
6use nom::combinator::eof;
7
8/// A token sequence that is an open [`BlockComment`][`crate::BlockComment`] line.
9///
10/// * `"/' begin\n"`
11///
12/// # Examples
13///
14/// ```
15/// use plantuml_parser::{BlockCommentOpenLine, ParseContainer};
16///
17/// # fn main() -> anyhow::Result<()> {
18/// let input = "/' begin\n";
19/// let (rest, (raws, _token)) = BlockCommentOpenLine::parse(input.into())?;
20/// let combined_raw: ParseContainer = raws.into();
21/// assert_eq!(rest, "");
22/// assert_eq!(combined_raw, "/' begin\n");
23/// # Ok(())
24/// # }
25/// ```
26#[derive(Clone, Debug)]
27pub struct BlockCommentOpenLine;
28
29impl BlockCommentOpenLine {
30    /// Tries to parse [`BlockCommentOpenLine`]. (e.g. `"/' begin\n"`.)
31    pub fn parse(input: ParseContainer) -> ParseResult<Self> {
32        let (rest, parsed) = (
33            wr!(space0),
34            wr!(tag("/'")),
35            wr!(take_till(|c| c == '\n' || c == '\r')),
36            alt((wr!(eof), wr!(line_ending))),
37        )
38            .parse(input)?;
39
40        let parsed = Vec::from(<[ParseContainer; 4]>::from(parsed));
41        Ok((rest, (parsed.into(), Self)))
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_0() -> anyhow::Result<()> {
51        let input = ParseContainer::from(
52            r#"/'
53                comment
54            '/"#,
55        );
56
57        let (rest0, (parsed0, _)) = BlockCommentOpenLine::parse(input.clone())?;
58        println!("rest0 = {rest0}");
59        println!("parsed0 = {parsed0}");
60        assert_eq!(rest0, "                comment\n            '/");
61        assert_eq!(parsed0, "/'\n");
62
63        Ok(())
64    }
65
66    #[test]
67    fn test_1() -> anyhow::Result<()> {
68        let input = ParseContainer::from(
69            r#"/' begin
70                comment
71            end '/"#,
72        );
73
74        let (rest0, (parsed0, _)) = BlockCommentOpenLine::parse(input.clone())?;
75        println!("rest0 = {rest0}");
76        println!("parsed0 = {parsed0}");
77        assert_eq!(rest0, "                comment\n            end '/");
78        assert_eq!(parsed0, "/' begin\n");
79
80        Ok(())
81    }
82
83    #[test]
84    fn test_2() -> anyhow::Result<()> {
85        let inputs = [
86            "/'",
87            "/'\n",
88            "/' begin",
89            "/' begin\n",
90            "/' begin \t",
91            "/' begin \t\n",
92            "  \t /' begin \t",
93            " \t   /' begin \t\n",
94            "/'/' begin\n",
95            "/' /' begin\n",
96            "/' /' /' begin\n",
97        ];
98
99        for input in inputs.into_iter() {
100            let container = ParseContainer::from(input);
101            let (rest0, (parsed0, _)) = BlockCommentOpenLine::parse(container)?;
102            println!("rest0 = {rest0}");
103            println!("parsed0 = {parsed0}");
104            assert_eq!(rest0, "");
105            assert_eq!(parsed0, input);
106        }
107
108        Ok(())
109    }
110}