…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.
Showing the changes to git-object/src/commit (1 of 1 file). Show all
Unified · Split
| @@ -7,6 +7,32 @@ use crate::{ · 6 unmodified lines | ||
| 7 | 7 | |
| 8 | 8 | mod decode { |
| 9 | 9 | 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 | + } | |
| 10 | 36 | |
| 11 | 37 | /// Returns title and body, without separator |
| 12 | 38 | pub fn bytes(i: &[u8]) -> (&BStr, Option<&BStr>) { |