Skip to content

Commit 1ec47de

Browse files
committed
sketch nom version of the message parser… (#198)
…to simply allow for proper, non-hacky parsing and learn what it takes to do that. nom parsers are always hard to get started with but are best in readability and after all, a skill to hone and get better at.
1 parent c332b8f commit 1ec47de

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

git-object/src/commit/message.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@ use crate::{
77

88
mod decode {
99
use crate::bstr::{BStr, ByteSlice};
10+
use nom::branch::alt;
11+
use nom::bytes::complete::tag;
12+
use nom::combinator::opt;
13+
use nom::error::ParseError;
14+
use nom::sequence::{pair, terminated};
15+
use nom::IResult;
16+
17+
fn newline<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
18+
alt((tag(b"\r\n"), tag(b"\n")))(i)
19+
}
20+
21+
fn subject<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
22+
todo!("subject")
23+
}
24+
25+
/// Parse a signature from the bytes input `i` using `nom`.
26+
pub fn nomfoo<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&'a BStr, Option<&'a BStr>), E> {
27+
let (rest, subject) = opt(terminated(subject, pair(newline, newline)))(i)?;
28+
Ok((
29+
&[],
30+
match subject {
31+
Some(subject) => (subject.as_bstr(), Some(rest.as_bstr())),
32+
None => (i.as_bstr(), None),
33+
},
34+
))
35+
}
1036

1137
/// Returns title and body, without separator
1238
pub fn bytes(i: &[u8]) -> (&BStr, Option<&BStr>) {

0 commit comments

Comments
 (0)