plantuml_parser/dsl/token/
title.rs1use crate::dsl::token::shared::{Comment, Delimiter};
2use crate::{ParseContainer, ParseResult, wr, wr2};
3use nom::Parser;
4use nom::bytes::complete::tag_no_case;
5
6#[derive(Clone, Debug)]
24pub struct TitleToken {
25 title: Comment,
26 delimiter: Delimiter,
27}
28
29impl TitleToken {
30 pub fn parse(input: ParseContainer) -> ParseResult<Self> {
32 let (rest, (title_tag, (delimiter_raw, delimiter), (title_raw, title))) = (
33 wr!(tag_no_case("title")),
34 wr2!(Delimiter::parse),
35 wr2!(Comment::parse),
36 )
37 .parse(input)?;
38
39 let ret0 = ParseContainer::from(vec![title_tag, delimiter_raw, title_raw]);
40 let ret1 = Self { title, delimiter };
41
42 Ok((rest, (ret0, ret1)))
43 }
44
45 pub fn title(&self) -> &str {
47 self.title.as_str()
48 }
49
50 pub fn delimiter(&self) -> Option<&str> {
52 self.delimiter.delimiter()
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_parse() -> anyhow::Result<()> {
62 let testdata = "title EXAMPLE TITLE";
63 let (rest, (parsed, token)) = TitleToken::parse(testdata.into())?;
64 assert_eq!(rest, "");
65 assert_eq!(testdata, parsed);
66 assert_eq!("EXAMPLE TITLE", token.title());
67 assert_eq!(None, token.delimiter());
68
69 Ok(())
70 }
71
72 #[test]
73 fn test_parse_with_trailing_spaces() -> anyhow::Result<()> {
74 let testdata = "title EXAMPLE \t TITLE \t";
75 let (rest, (parsed, token)) = TitleToken::parse(testdata.into())?;
76 assert_eq!(rest, " \t");
77 assert_eq!("title EXAMPLE \t TITLE", parsed);
78 assert_eq!("EXAMPLE \t TITLE", token.title());
79 assert_eq!(None, token.delimiter());
80
81 Ok(())
82 }
83
84 #[test]
85 fn test_parse_case_insensitive() -> anyhow::Result<()> {
86 let testdata = "Title EXAMPLE TITLE";
87 let (rest, (parsed, token)) = TitleToken::parse(testdata.into())?;
88 assert_eq!(rest, "");
89 assert_eq!(testdata, parsed);
90 assert_eq!("EXAMPLE TITLE", token.title());
91 assert_eq!(None, token.delimiter());
92
93 let testdata = "TITLE EXAMPLE TITLE";
94 let (rest, (parsed, token)) = TitleToken::parse(testdata.into())?;
95 assert_eq!(rest, "");
96 assert_eq!(testdata, parsed);
97 assert_eq!("EXAMPLE TITLE", token.title());
98 assert_eq!(None, token.delimiter());
99
100 let testdata = "tiTle EXAMPLE TITLE";
101 let (rest, (parsed, token)) = TitleToken::parse(testdata.into())?;
102 assert_eq!(rest, "");
103 assert_eq!(testdata, parsed);
104 assert_eq!("EXAMPLE TITLE", token.title());
105 assert_eq!(None, token.delimiter());
106
107 Ok(())
108 }
109
110 #[test]
111 fn test_parse_with_delimiter() -> anyhow::Result<()> {
112 let testdata = "title : EXAMPLE TITLE";
113 let (rest, (parsed, token)) = TitleToken::parse(testdata.into())?;
114 assert_eq!(rest, "");
115 assert_eq!(testdata, parsed);
116 assert_eq!("EXAMPLE TITLE", token.title());
117 assert_eq!(Some(":"), token.delimiter());
118
119 let testdata = "title: \tEXAMPLE TITLE";
120 let (rest, (parsed, token)) = TitleToken::parse(testdata.into())?;
121 assert_eq!(rest, "");
122 assert_eq!(testdata, parsed);
123 assert_eq!("EXAMPLE TITLE", token.title());
124 assert_eq!(Some(":"), token.delimiter());
125
126 let testdata = "title \t:EXAMPLE TITLE";
127 let (rest, (parsed, token)) = TitleToken::parse(testdata.into())?;
128 assert_eq!(rest, "");
129 assert_eq!(testdata, parsed);
130 assert_eq!("EXAMPLE TITLE", token.title());
131 assert_eq!(Some(":"), token.delimiter());
132
133 let testdata = "title:EXAMPLE TITLE";
134 let (rest, (parsed, token)) = TitleToken::parse(testdata.into())?;
135 assert_eq!(rest, "");
136 assert_eq!(testdata, parsed);
137 assert_eq!("EXAMPLE TITLE", token.title());
138 assert_eq!(Some(":"), token.delimiter());
139
140 Ok(())
141 }
142}