From 529304858073dc20795bcc54bf0f52c33aacea87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Fri, 13 Jan 2023 19:57:52 +0100 Subject: [PATCH] Ensure metadata title and desc aren't tags MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/lib.rs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1308059..202b26d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -171,12 +171,14 @@ fn parse_meta_tags(s: Span) -> IResult> { } fn parse_meta(s: Span) -> IResult { - let (s, (_pos, title)) = - tuple((position, delimited(tag("#"), take_until("\n"), tag("\n"))))(s)?; + let (s, (_pos, title)) = tuple(( + position, + delimited(tag("#"), take_while1(|c| c != '#' && c != '\n'), tag("\n")), + ))(s)?; let optdesc = opt(tuple(( position, - delimited(tag("#"), take_until("\n"), tag("\n")), + delimited(tag("#"), take_while1(|c| c != '#' && c != '\n'), tag("\n")), )))(s)?; let mut desc: Option<&str> = None; @@ -387,6 +389,31 @@ mod tests { ); } + #[test] + fn test_meta() { + let buf1 = "# Title.\n"; + let buf2 = "#Foo\n# Desc\n"; + let buf3 = "#Foo\n# Desc\n## tag1\n"; + let buf4 = "#Foo\n## tag1\n"; + + assert_eq!( + parse_meta(buf1.into()).unwrap().1, + Metadata::new("Title.", None::) + ); + assert_eq!( + parse_meta(buf2.into()).unwrap().1, + Metadata::new("Foo", Some("Desc")) + ); + assert_eq!( + parse_meta(buf3.into()).unwrap().1, + Metadata::new("Foo", Some("Desc")).with_tags(vec![String::from("tag1")]) + ); + assert_eq!( + parse_meta(buf4.into()).unwrap().1, + Metadata::new("Foo", None::).with_tags(vec![String::from("tag1")]) + ); + } + #[test] fn test_client() { let buf1 = "[Client] louise\n\tjid: louise@localhost\n\tpassword: password\n";