Skip to content

Commit

Permalink
Resolve remaining winnow 0.3 deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 2, 2023
1 parent 5aa61db commit 1080166
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 91 deletions.
2 changes: 1 addition & 1 deletion gix-actor/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{signature::decode, Identity, IdentityRef};

impl<'a> IdentityRef<'a> {
/// Deserialize an identity from the given `data`.
pub fn from_bytes<E>(data: &'a [u8]) -> Result<Self, winnow::Err<E>>
pub fn from_bytes<E>(data: &'a [u8]) -> Result<Self, winnow::error::ErrMode<E>>
where
E: winnow::error::ParseError<&'a [u8]> + winnow::error::ContextError<&'a [u8]>,
{
Expand Down
28 changes: 14 additions & 14 deletions gix-actor/src/signature/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ pub(crate) mod function {
use std::cell::RefCell;
use winnow::{
branch::alt,
bytes::complete::{take, take_until, take_while_m_n},
character::is_digit,
bytes::{take, take_until0, take_while_m_n},
error::{ContextError, ParseError},
multi::many1_count,
multi::many1,
prelude::*,
sequence::{terminated, tuple},
sequence::terminated,
stream::AsChar,
};

use crate::{IdentityRef, SignatureRef};
Expand All @@ -26,31 +26,31 @@ pub(crate) mod function {
identity,
b" ",
(|i| {
terminated(take_until(SPACE), take(1usize))(i).and_then(|(i, v)| {
terminated(take_until0(SPACE), take(1usize))(i).and_then(|(i, v)| {
btoi::<SecondsSinceUnixEpoch>(v)
.map(|v| (i, v))
.map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))
.map_err(|_| winnow::error::ErrMode::from_error_kind(i, winnow::error::ErrorKind::MapRes))
})
})
.context("<timestamp>"),
alt((
many1_count(b"-").map(|_| *tzsign.borrow_mut() = b'-'), // TODO: this should be a non-allocating consumer of consecutive tags
many1_count(b"+").map(|_| *tzsign.borrow_mut() = b'+'),
many1(b"-").map(|_: ()| *tzsign.borrow_mut() = b'-'), // TODO: this should be a non-allocating consumer of consecutive tags
many1(b"+").map(|_: ()| *tzsign.borrow_mut() = b'+'),
))
.context("+|-"),
(|i| {
take_while_m_n(2usize, 2, is_digit)(i).and_then(|(i, v)| {
take_while_m_n(2usize, 2, AsChar::is_dec_digit)(i).and_then(|(i, v)| {
btoi::<OffsetInSeconds>(v)
.map(|v| (i, v))
.map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))
.map_err(|_| winnow::error::ErrMode::from_error_kind(i, winnow::error::ErrorKind::MapRes))
})
})
.context("HH"),
(|i| {
take_while_m_n(1usize, 2, is_digit)(i).and_then(|(i, v)| {
take_while_m_n(1usize, 2, AsChar::is_dec_digit)(i).and_then(|(i, v)| {
btoi::<OffsetInSeconds>(v)
.map(|v| (i, v))
.map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))
.map_err(|_| winnow::error::ErrMode::from_error_kind(i, winnow::error::ErrorKind::MapRes))
})
})
.context("MM"),
Expand Down Expand Up @@ -82,8 +82,8 @@ pub(crate) mod function {
i: &'a [u8],
) -> IResult<&'a [u8], IdentityRef<'a>, E> {
let (i, (name, email)) = (
terminated(take_until(&b" <"[..]), take(2usize)).context("<name>"),
terminated(take_until(&b">"[..]), take(1usize)).context("<email>"),
terminated(take_until0(&b" <"[..]), take(2usize)).context("<name>"),
terminated(take_until0(&b">"[..]), take(1usize)).context("<email>"),
)
.context("<name> <<email>>")
.parse_next(i)?;
Expand Down
2 changes: 1 addition & 1 deletion gix-actor/src/signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod _ref {

impl<'a> SignatureRef<'a> {
/// Deserialize a signature from the given `data`.
pub fn from_bytes<E>(data: &'a [u8]) -> Result<SignatureRef<'a>, winnow::Err<E>>
pub fn from_bytes<E>(data: &'a [u8]) -> Result<SignatureRef<'a>, winnow::error::ErrMode<E>>
where
E: winnow::error::ParseError<&'a [u8]> + winnow::error::ContextError<&'a [u8]>,
{
Expand Down
19 changes: 12 additions & 7 deletions gix-object/src/commit/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ use std::borrow::Cow;
use smallvec::SmallVec;
use winnow::{
branch::alt,
bytes::complete::{is_not, tag},
combinator::{all_consuming, opt},
bytes::{tag, take_till1},
combinator::{eof, opt},
error::{ContextError, ParseError},
multi::many0,
prelude::*,
sequence::terminated,
};

use crate::{parse, parse::NL, BStr, ByteSlice, CommitRef};

pub fn message<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a BStr, E> {
if i.is_empty() {
// newline + [message]
return Err(winnow::Err::from_error_kind(i, winnow::error::ErrorKind::Eof)
.map(|err: E| err.add_context(i, "newline + <message>")));
return Err(
winnow::error::ErrMode::from_error_kind(i, winnow::error::ErrorKind::Eof)
.map(|err: E| err.add_context(i, "newline + <message>")),
);
}
let (i, _) = tag(NL)
.context("a newline separates headers from the message")
Expand All @@ -39,16 +42,18 @@ pub fn commit<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
let (i, committer) = (|i| parse::header_field(i, b"committer", parse::signature))
.context("committer <signature>")
.parse_next(i)?;
let (i, encoding) = opt(|i| parse::header_field(i, b"encoding", is_not(NL)))
let (i, encoding) = opt(|i| parse::header_field(i, b"encoding", take_till1(NL)))
.context("encoding <encoding>")
.parse_next(i)?;
let (i, extra_headers) = many0(alt((
parse::any_header_field_multi_line.map(|(k, o)| (k.as_bstr(), Cow::Owned(o))),
|i| parse::any_header_field(i, is_not(NL)).map(|(i, (k, o))| (i, (k.as_bstr(), Cow::Borrowed(o.as_bstr())))),
|i| {
parse::any_header_field(i, take_till1(NL)).map(|(i, (k, o))| (i, (k.as_bstr(), Cow::Borrowed(o.as_bstr()))))
},
)))
.context("<field> <single-line|multi-line>")
.parse_next(i)?;
let (i, message) = all_consuming(message)(i)?;
let (i, message) = terminated(message, eof)(i)?;

Ok((
i,
Expand Down
8 changes: 4 additions & 4 deletions gix-object/src/commit/message/body.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::ops::Deref;

use winnow::{
bytes::complete::take_until1,
combinator::all_consuming,
bytes::take_until1,
combinator::eof,
error::{ErrorKind, ParseError},
sequence::terminated,
IResult,
Expand Down Expand Up @@ -35,7 +35,7 @@ pub struct TrailerRef<'a> {
fn parse_single_line_trailer<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&'a BStr, &'a BStr), E> {
let (value, token) = terminated(take_until1(b":".as_ref()), b": ")(i.trim_end())?;
if token.trim_end().len() != token.len() || value.trim_start().len() != value.len() {
Err(winnow::Err::from_error_kind(i, ErrorKind::Fail).cut())
Err(winnow::error::ErrMode::from_error_kind(i, ErrorKind::Fail).cut())
} else {
Ok((&[], (token.as_bstr(), value.as_bstr())))
}
Expand All @@ -51,7 +51,7 @@ impl<'a> Iterator for Trailers<'a> {
for line in self.cursor.lines_with_terminator() {
self.cursor = &self.cursor[line.len()..];
if let Some(trailer) =
all_consuming(parse_single_line_trailer::<()>)(line)
terminated(parse_single_line_trailer::<()>, eof)(line)
.ok()
.map(|(_, (token, value))| TrailerRef {
token: token.trim().as_bstr(),
Expand Down
4 changes: 2 additions & 2 deletions gix-object/src/commit/message/decode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use winnow::{branch::alt, bytes::complete::take_till1, combinator::all_consuming, error::ParseError, prelude::*};
use winnow::{branch::alt, bytes::take_till1, combinator::eof, error::ParseError, prelude::*, sequence::terminated};

use crate::bstr::{BStr, ByteSlice};

Expand Down Expand Up @@ -46,5 +46,5 @@ fn subject_and_body<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8

/// Returns title and body, without separator
pub fn message(input: &[u8]) -> (&BStr, Option<&BStr>) {
all_consuming(subject_and_body::<()>)(input).expect("cannot fail").1
terminated(subject_and_body::<()>, eof)(input).expect("cannot fail").1
}
11 changes: 6 additions & 5 deletions gix-object/src/commit/ref_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use bstr::BStr;
use gix_hash::{oid, ObjectId};
use winnow::{
branch::alt,
bytes::complete::is_not,
combinator::{all_consuming, opt},
bytes::take_till1,
combinator::{eof, opt},
prelude::*,
sequence::terminated,
};

use crate::{bstr::ByteSlice, commit::decode, parse, parse::NL, CommitRefIter};
Expand Down Expand Up @@ -173,7 +174,7 @@ impl<'a> CommitRefIter<'a> {
)
}
Encoding => {
let (i, encoding) = opt(|i| parse::header_field(i, b"encoding", is_not(NL)))
let (i, encoding) = opt(|i| parse::header_field(i, b"encoding", take_till1(NL)))
.context("encoding <encoding>")
.parse_next(i)?;
*state = State::ExtraHeaders;
Expand All @@ -186,7 +187,7 @@ impl<'a> CommitRefIter<'a> {
let (i, extra_header) = opt(alt((
|i| parse::any_header_field_multi_line(i).map(|(i, (k, o))| (i, (k.as_bstr(), Cow::Owned(o)))),
|i| {
parse::any_header_field(i, is_not(NL))
parse::any_header_field(i, take_till1(NL))
.map(|(i, (k, o))| (i, (k.as_bstr(), Cow::Borrowed(o.as_bstr()))))
},
)))
Expand All @@ -201,7 +202,7 @@ impl<'a> CommitRefIter<'a> {
}
}
Message => {
let (i, message) = all_consuming(decode::message)(i)?;
let (i, message) = terminated(decode::message, eof)(i)?;
debug_assert!(
i.is_empty(),
"we should have consumed all data - otherwise iter may go forever"
Expand Down
30 changes: 16 additions & 14 deletions gix-object/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,20 @@ pub mod decode {
pub inner: ParseErrorOwned,
}

impl<'a> From<winnow::Err<ParseError<'a>>> for Error {
fn from(v: winnow::Err<ParseError<'a>>) -> Self {
impl<'a> From<winnow::error::ErrMode<ParseError<'a>>> for Error {
fn from(v: winnow::error::ErrMode<ParseError<'a>>) -> Self {
Error {
inner: match v {
winnow::Err::Backtrack(err) | winnow::Err::Cut(err) => winnow::error::VerboseError {
errors: err
.errors
.into_iter()
.map(|(i, v)| (i.as_bstr().to_owned(), v))
.collect(),
},
winnow::Err::Incomplete(_) => unreachable!("we don't have streaming parsers"),
winnow::error::ErrMode::Backtrack(err) | winnow::error::ErrMode::Cut(err) => {
winnow::error::VerboseError {
errors: err
.errors
.into_iter()
.map(|(i, v)| (i.as_bstr().to_owned(), v))
.collect(),
}
}
winnow::error::ErrMode::Incomplete(_) => unreachable!("we don't have streaming parsers"),
},
}
}
Expand Down Expand Up @@ -321,12 +323,12 @@ pub mod decode {
pub inner: ParseErrorOwned,
}

impl<'a> From<winnow::Err<ParseError<'a>>> for Error {
fn from(v: winnow::Err<ParseError<'a>>) -> Self {
impl<'a> From<winnow::error::ErrMode<ParseError<'a>>> for Error {
fn from(v: winnow::error::ErrMode<ParseError<'a>>) -> Self {
Error {
inner: match v {
winnow::Err::Backtrack(err) | winnow::Err::Cut(err) => err,
winnow::Err::Incomplete(_) => unreachable!("we don't have streaming parsers"),
winnow::error::ErrMode::Backtrack(err) | winnow::error::ErrMode::Cut(err) => err,
winnow::error::ErrMode::Incomplete(_) => unreachable!("we don't have streaming parsers"),
},
}
}
Expand Down
19 changes: 12 additions & 7 deletions gix-object/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bstr::{BStr, BString, ByteVec};
use winnow::{
bytes::complete::{is_not, take_until, take_while_m_n},
bytes::{take_till1, take_until0, take_while_m_n},
combinator::peek,
error::{ContextError, ParseError},
multi::many1_count,
multi::many1,
prelude::*,
sequence::{preceded, terminated},
};
Expand All @@ -18,8 +18,13 @@ pub(crate) fn any_header_field_multi_line<'a, E: ParseError<&'a [u8]> + ContextE
i: &'a [u8],
) -> IResult<&'a [u8], (&'a [u8], BString), E> {
let (i, (k, o)) = peek((
terminated(is_not(SPACE_OR_NL), SPACE),
(is_not(NL), NL, many1_count(terminated((SPACE, take_until(NL)), NL))).recognize(),
terminated(take_till1(SPACE_OR_NL), SPACE),
(
take_till1(NL),
NL,
many1(terminated((SPACE, take_until0(NL)), NL)).map(|()| ()),
)
.recognize(),
))
.context("name <multi-line-value>")
.parse_next(i)?;
Expand All @@ -41,16 +46,16 @@ pub(crate) fn any_header_field_multi_line<'a, E: ParseError<&'a [u8]> + ContextE
pub(crate) fn header_field<'a, T, E: ParseError<&'a [u8]>>(
i: &'a [u8],
name: &'static [u8],
parse_value: impl Fn(&'a [u8]) -> IResult<&'a [u8], T, E>,
parse_value: impl FnMut(&'a [u8]) -> IResult<&'a [u8], T, E>,
) -> IResult<&'a [u8], T, E> {
terminated(preceded(terminated(name, SPACE), parse_value), NL)(i)
}

pub(crate) fn any_header_field<'a, T, E: ParseError<&'a [u8]>>(
i: &'a [u8],
parse_value: impl Fn(&'a [u8]) -> IResult<&'a [u8], T, E>,
parse_value: impl FnMut(&'a [u8]) -> IResult<&'a [u8], T, E>,
) -> IResult<&'a [u8], (&'a [u8], T), E> {
terminated((terminated(is_not(SPACE_OR_NL), SPACE), parse_value), NL)(i)
terminated((terminated(take_till1(SPACE_OR_NL), SPACE), parse_value), NL)(i)
}

fn is_hex_digit_lc(b: u8) -> bool {
Expand Down
22 changes: 11 additions & 11 deletions gix-object/src/tag/decode.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use winnow::{
branch::alt,
bytes::complete::{tag, take_until, take_while, take_while1},
character::is_alphabetic,
combinator::{all_consuming, opt},
bytes::{tag, take_until0, take_while0, take_while1},
combinator::{eof, opt},
error::{ContextError, ParseError},
prelude::*,
sequence::preceded,
sequence::{preceded, terminated},
stream::AsChar,
};

use crate::{parse, parse::NL, BStr, ByteSlice, TagRef};
Expand All @@ -15,11 +15,11 @@ pub fn git_tag<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(i: &'a [u8]
.context("object <40 lowercase hex char>")
.parse_next(i)?;

let (i, kind) = (|i| parse::header_field(i, b"type", take_while1(is_alphabetic)))
let (i, kind) = (|i| parse::header_field(i, b"type", take_while1(AsChar::is_alpha)))
.context("type <object kind>")
.parse_next(i)?;
let kind =
crate::Kind::from_bytes(kind).map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))?;
let kind = crate::Kind::from_bytes(kind)
.map_err(|_| winnow::error::ErrMode::from_error_kind(i, winnow::error::ErrorKind::MapRes))?;

let (i, tag_version) = (|i| parse::header_field(i, b"tag", take_while1(|b| b != NL[0])))
.context("tag <version>")
Expand All @@ -28,7 +28,7 @@ pub fn git_tag<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(i: &'a [u8]
let (i, signature) = opt(|i| parse::header_field(i, b"tagger", parse::signature))
.context("tagger <signature>")
.parse_next(i)?;
let (i, (message, pgp_signature)) = all_consuming(message)(i)?;
let (i, (message, pgp_signature)) = terminated(message, eof)(i)?;
Ok((
i,
TagRef {
Expand Down Expand Up @@ -61,14 +61,14 @@ pub fn message<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&
}
let (i, (message, signature)) = alt((
(
take_until(PGP_SIGNATURE_BEGIN),
take_until0(PGP_SIGNATURE_BEGIN),
preceded(
NL,
(
&PGP_SIGNATURE_BEGIN[1..],
take_until(PGP_SIGNATURE_END),
take_until0(PGP_SIGNATURE_END),
PGP_SIGNATURE_END,
take_while(|_| true),
take_while0(|_| true),
)
.recognize(),
),
Expand Down
Loading

0 comments on commit 1080166

Please sign in to comment.