History

refactor

Sebastian Thiel committed 6 years ago

Commit fb287af1 parent 4498dff2 files · +15 −8Browse files

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

Unified · Split

2 files
  1. git-object/src/parsed/mod.rs +5 −1
  2. git-object/src/parsed/tag.rs +10 −7
git-object/src/parsed/mod.rs+5−1
@@ -33,13 +33,17 @@ quick_error! { · 32 unmodified lines
3333 }
3434
3535 impl Error {
36− pub fn parse_context(mut self, ctx: &'static str) -> Self{
36+ fn set_parse_context(mut self, ctx: &'static str) -> Self {
3737 match self {
3838 Error::Nom(_, ref mut message) => *message = ctx,
3939 _ => {}
4040 };
4141 self
4242 }
43+
44+ fn context(msg: &'static str) -> impl Fn(nom::Err<Self>) -> nom::Err<Self> {
45+ move |e: nom::Err<Self>| e.map(|e| e.set_parse_context(msg))
46+ }
4347 }
4448
4549 impl ParseError<&[u8]> for Error {
git-object/src/parsed/tag.rs+10−7
@@ -6,10 +6,10 @@ use crate::{parsed::Signature, Time}; · 5 unmodified lines
66 use bstr::{BStr, ByteSlice};
77 use btoi::btoi;
88 use hex::FromHex;
9−use nom::{bytes::complete::tag, IResult, };
10−use nom::sequence::{tuple, preceded, terminated};
119 use nom::bytes::complete::{take, take_while, take_while_m_n};
1210 use nom::combinator::map;
11+use nom::sequence::{preceded, terminated, tuple};
12+use nom::{bytes::complete::tag, IResult};
1313
1414 #[derive(PartialEq, Eq, Debug, Hash)]
1515 pub struct Tag<'data> {
@@ -75,10 +75,6 @@ fn parse_signature(d: &[u8]) -> Result<Signature, Error> { · 59 unmodified lines
7575 })
7676 }
7777
78−fn context(msg: &'static str) -> impl Fn(nom::Err<Error>) -> nom::Err<Error> {
79− move |e: nom::Err<Error>| e.map(|e| e.parse_context(msg))
80−}
81−
8278 fn is_hex_digit_lc(b: u8) -> bool {
8379 match b {
8480 b'0'..=b'9' => true,
@@ -89,7 +85,14 @@ fn is_hex_digit_lc(b: u8) -> bool { · 4 unmodified lines
8985
9086 pub(crate) fn parse_tag_nom(i: &[u8]) -> IResult<&[u8], Tag, Error> {
9187 const NL: &[u8] = b"\n";
92− let (i, _) = terminated(preceded(tag(b"object "), take_while_m_n(40usize, 40, is_hex_digit_lc)), tag(NL))(i).map_err(context("object <40 lowercase hex char>"))?;
88+ let (i, _) = terminated(
89+ preceded(
90+ tag(b"object "),
91+ take_while_m_n(40usize, 40, is_hex_digit_lc),
92+ ),
93+ tag(NL),
94+ )(i)
95+ .map_err(Error::context("object <40 lowercase hex char>"))?;
9396 unimplemented!("parse message nom")
9497 }
9598