Ensure metadata title and desc aren't tags

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-01-13 19:57:52 +01:00
parent 015a6c97e3
commit 5293048580
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -171,12 +171,14 @@ fn parse_meta_tags(s: Span) -> IResult<Span, Vec<String>> {
}
fn parse_meta(s: Span) -> IResult<Span, Metadata> {
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::<String>)
);
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::<String>).with_tags(vec![String::from("tag1")])
);
}
#[test]
fn test_client() {
let buf1 = "[Client] louise\n\tjid: louise@localhost\n\tpassword: password\n";