History

More message parsing tests, now with legit failure… (#198)

…showing that we as always want some greedy parsing based on parsers, not based on input.

Sebastian Thiel committed 5 years ago

Commit 625be8d1 parent 6150b2d4 files · +56 −29Browse files

Showing the changes to git-object/src/commit (1 of 4 files). Show all

Unified · Split

git-object/src/commit/message.rs+25−11
@@ -1,15 +1,30 @@
1−use crate::bstr::{BStr, BString, ByteSlice, ByteVec};
2−use crate::commit::MessageRef;
31 use std::borrow::Cow;
42
3+use crate::{
4+ bstr::{BStr, BString, ByteSlice, ByteVec},
5+ commit::MessageRef,
6+};
7+
58 mod decode {
6− use crate::bstr::BStr;
7− use nom::error::ParseError;
8− use nom::IResult;
9+ use nom::{
10+ branch::alt,
11+ bytes::complete::{tag, take_while},
12+ combinator::map,
13+ error::ParseError,
14+ sequence::{pair, terminated},
15+ IResult,
16+ };
17+
18+ use crate::bstr::{BStr, ByteSlice};
919
20+ pub fn newline<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
21+ alt((tag(b"\r\n"), tag(b"\n")))(i)
22+ }
1023 /// Returns title and body, without separator
11− pub fn bytes<'a, E: ParseError<&'a [u8]>>(_i: &'a [u8]) -> IResult<&[u8], (&'a BStr, &'a BStr), E> {
12− todo!("actual decoding")
24+ pub fn bytes<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&[u8], &'a BStr, E> {
25+ map(terminated(take_while(|c| c != b'\n'), pair(newline, newline)), |t| {
26+ t.as_bstr()
27+ })(i)
1328 }
1429 }
1530
@@ -18,10 +33,9 @@ impl<'a> MessageRef<'a> { · 2 unmodified lines
1833 ///
1934 /// Note that this cannot fail as everything will be interpreted as title if there is no body separator.
2035 pub fn from_bytes(input: &'a [u8]) -> Self {
21− let (rest, (title, body)) = decode::bytes::<()>(input)
22− .map(|(i, (title, body))| (i, (title, Some(body))))
23− .unwrap_or_else(|_| (&[], (input.as_bstr(), None)));
24− debug_assert!(rest.is_empty(), "all consuming message parsing");
36+ let (title, body) = decode::bytes::<()>(input)
37+ .map(|(body, title)| (title, Some(body.as_bstr())))
38+ .unwrap_or_else(|_| (input.as_bstr(), None));
2539 MessageRef { title, body }
2640 }
2741 /// Produce a short commit summary for the message title.