Showing the changes to git-object/src (2 of 2 files). Show all
Unified · Split
| @@ -34,27 +34,19 @@ fn is_hex_digit_lc(b: u8) -> bool { · 33 unmodified lines | |||
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | pub(crate) fn parse(i: &[u8]) -> IResult<&[u8], Tag, Error> { | 36 | pub(crate) fn parse(i: &[u8]) -> IResult<&[u8], Tag, Error> { |
| 37 | let (i, target) = parse_oneline_header(b"object")(i) | 37 | let (i, target) = |
| 38 | .map_err(Error::context("object <40 lowercase hex char>"))?; | 38 | parse_oneline_header(i, b"object", take_while_m_n(40usize, 40, is_hex_digit_lc)) |
| 39 | // let (i, target) = terminated( | 39 | .map_err(Error::context("object <40 lowercase hex char>"))?; |
| 40 | // preceded( | ||
| 41 | // tag(b"object "), | ||
| 42 | // take_while_m_n(40usize, 40, is_hex_digit_lc), | ||
| 43 | // ), | ||
| 44 | // tag(NL), | ||
| 45 | // )(i) | ||
| 46 | // .map_err(Error::context("object <40 lowercase hex char>"))?; | ||
| 47 | 40 | ||
| 48 | let (i, kind) = terminated(preceded(tag(b"type "), take_while1(is_alphabetic)), tag(NL))(i) | 41 | let (i, kind) = parse_oneline_header(i, b"type", take_while1(is_alphabetic)) |
| 49 | .map_err(Error::context("type <object kind>"))?; | 42 | .map_err(Error::context("type <object kind>"))?; |
| 50 | let kind = | 43 | let kind = |
| 51 | crate::Kind::from_bytes(kind).map_err(|e| nom::Err::Error(Error::ParseKindError(e)))?; | 44 | crate::Kind::from_bytes(kind).map_err(|e| nom::Err::Error(Error::ParseKindError(e)))?; |
| 52 | 45 | ||
| 53 | let (i, tag_version) = | 46 | let (i, tag_version) = parse_oneline_header(i, b"tag", take_while1(|b| b != NL[0])) |
| 54 | terminated(preceded(tag(b"tag "), take_while1(|b| b != NL[0])), tag(NL))(i) | 47 | .map_err(Error::context("tag <version>"))?; |
| 55 | .map_err(Error::context("tag <version>"))?; | ||
| 56 | 48 | ||
| 57 | let (i, signature) = terminated(preceded(tag(b"tagger "), parse_signature), tag(NL))(i) | 49 | let (i, signature) = parse_oneline_header(i, b"tagger", parse_signature) |
| 58 | .map_err(Error::context("tagger <signature>"))?; | 50 | .map_err(Error::context("tagger <signature>"))?; |
| 59 | let (i, (message, pgp_signature)) = all_consuming(parse_message)(i)?; | 51 | let (i, (message, pgp_signature)) = all_consuming(parse_message)(i)?; |
| 60 | Ok(( | 52 | Ok(( |
| @@ -14,15 +14,15 @@ use nom::{ · 13 unmodified lines | |||
| 14 | pub(crate) const NL: &[u8] = b"\n"; | 14 | pub(crate) const NL: &[u8] = b"\n"; |
| 15 | pub(crate) const SPACE: &[u8] = b" "; | 15 | pub(crate) const SPACE: &[u8] = b" "; |
| 16 | 16 | ||
| 17 | pub(crate) fn parse_oneline_header( | 17 | pub(crate) fn parse_oneline_header<'a, T>( |
| 18 | i: &'a [u8], | ||
| 18 | name: &'static [u8], | 19 | name: &'static [u8], |
| 19 | ) -> impl Fn(&[u8]) -> IResult<&[u8], &[u8], Error> { | 20 | parse_value: impl Fn(&'a [u8]) -> IResult<&'a [u8], T, Error>, |
| 20 | move |i: &[u8]| { | 21 | ) -> IResult<&'a [u8], T, Error> { |
| 21 | terminated( | 22 | terminated( |
| 22 | preceded(terminated(tag(name), tag(SPACE)), take_until(NL)), | 23 | preceded(terminated(tag(name), tag(SPACE)), parse_value), |
| 23 | tag(NL), | 24 | tag(NL), |
| 24 | )(i) | 25 | )(i) |
| 25 | } | ||
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | pub(crate) fn parse_signature(i: &[u8]) -> IResult<&[u8], Signature, Error> { | 28 | pub(crate) fn parse_signature(i: &[u8]) -> IResult<&[u8], Signature, Error> { |