History

refactor

Sebastian Thiel committed 6 years ago

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

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
33}33}
34 34
35impl Error {35impl Error {
36 pub fn parse_context(mut self, ctx: &'static str) -> Self{36 fn set_parse_context(mut self, ctx: &'static str) -> Self {
37 match self {37 match self {
38 Error::Nom(_, ref mut message) => *message = ctx,38 Error::Nom(_, ref mut message) => *message = ctx,
39 _ => {}39 _ => {}
40 };40 };
41 self41 self
42 }42 }
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 }
43}47}
44 48
45impl ParseError<&[u8]> for Error {49impl ParseError<&[u8]> for Error {
git-object/src/parsed/tag.rs+10−7
@@ -6,10 +6,10 @@ use crate::{parsed::Signature, Time}; · 5 unmodified lines
6use bstr::{BStr, ByteSlice};6use bstr::{BStr, ByteSlice};
7use btoi::btoi;7use btoi::btoi;
8use hex::FromHex;8use hex::FromHex;
9use nom::{bytes::complete::tag, IResult, };
10use nom::sequence::{tuple, preceded, terminated};
11use nom::bytes::complete::{take, take_while, take_while_m_n};9use nom::bytes::complete::{take, take_while, take_while_m_n};
12use nom::combinator::map;10use nom::combinator::map;
11use nom::sequence::{preceded, terminated, tuple};
12use nom::{bytes::complete::tag, IResult};
13 13
14#[derive(PartialEq, Eq, Debug, Hash)]14#[derive(PartialEq, Eq, Debug, Hash)]
15pub struct Tag<'data> {15pub struct Tag<'data> {
@@ -75,10 +75,6 @@ fn parse_signature(d: &[u8]) -> Result<Signature, Error> { · 59 unmodified lines
75 })75 })
76}76}
77 77
78fn 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
82fn is_hex_digit_lc(b: u8) -> bool {78fn is_hex_digit_lc(b: u8) -> bool {
83 match b {79 match b {
84 b'0'..=b'9' => true,80 b'0'..=b'9' => true,
@@ -89,7 +85,14 @@ fn is_hex_digit_lc(b: u8) -> bool { · 4 unmodified lines
89 85
90pub(crate) fn parse_tag_nom(i: &[u8]) -> IResult<&[u8], Tag, Error> {86pub(crate) fn parse_tag_nom(i: &[u8]) -> IResult<&[u8], Tag, Error> {
91 const NL: &[u8] = b"\n";87 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>"))?;
93 unimplemented!("parse message nom")96 unimplemented!("parse message nom")
94}97}
95 98