Skip to content

Commit

Permalink
Upgrade to Winnow 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 17, 2023
1 parent 12f03db commit 3f8c91f
Show file tree
Hide file tree
Showing 31 changed files with 212 additions and 194 deletions.
23 changes: 7 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cargo-smart-release/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cargo-smart-release/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ toml_edit = "0.19.1"
semver = "1.0.4"
crates-index = { version = "2.1.0", default-features = false, features = ["git-performance", "git-https"] }
cargo_toml = "0.15.1"
winnow = "0.5.1"
winnow = "0.5.12"
git-conventional = "0.12.0"
time = "0.3.23"
pulldown-cmark = "0.9.0"
Expand Down
2 changes: 1 addition & 1 deletion gix-actor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ gix-date = { version = "^0.7.1", path = "../gix-date" }
thiserror = "1.0.38"
btoi = "0.4.2"
bstr = { version = "1.3.0", default-features = false, features = ["std", "unicode"]}
winnow = { version = "0.4", features = ["simd"] }
winnow = { version = "0.5.12", features = ["simd"] }
itoa = "1.0.1"
serde = { version = "1.0.114", optional = true, default-features = false, features = ["derive"]}

Expand Down
5 changes: 3 additions & 2 deletions gix-actor/src/identity.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use bstr::ByteSlice;
use winnow::prelude::*;

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::error::ErrMode<E>>
pub fn from_bytes<E>(mut data: &'a [u8]) -> Result<Self, winnow::error::ErrMode<E>>
where
E: winnow::error::ParserError<&'a [u8]> + winnow::error::AddContext<&'a [u8]>,
{
decode::identity(data).map(|(_, t)| t)
decode::identity.parse_next(&mut data)
}

/// Create an owned instance from this shared one.
Expand Down
38 changes: 22 additions & 16 deletions gix-actor/src/signature/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub(crate) mod function {

/// Parse a signature from the bytes input `i` using `nom`.
pub fn decode<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(
i: &'a [u8],
) -> IResult<&'a [u8], SignatureRef<'a>, E> {
i: &mut &'a [u8],
) -> PResult<SignatureRef<'a>, E> {
separated_pair(
identity,
b" ",
Expand Down Expand Up @@ -60,8 +60,8 @@ pub(crate) mod function {

/// Parse an identity from the bytes input `i` (like `name <email>`) using `nom`.
pub fn identity<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(
i: &'a [u8],
) -> IResult<&'a [u8], IdentityRef<'a>, E> {
i: &mut &'a [u8],
) -> PResult<IdentityRef<'a>, E> {
(
terminated(take_until0(&b" <"[..]), take(2usize)).context("<name>"),
terminated(take_until0(&b">"[..]), take(1usize)).context("<email>"),
Expand All @@ -82,12 +82,14 @@ mod tests {
use bstr::ByteSlice;
use gix_date::{time::Sign, OffsetInSeconds, SecondsSinceUnixEpoch};
use gix_testtools::to_bstr_err;
use winnow::IResult;
use winnow::prelude::*;

use crate::{signature, SignatureRef, Time};

fn decode(i: &[u8]) -> IResult<&[u8], SignatureRef<'_>, winnow::error::VerboseError<&[u8]>> {
signature::decode(i)
fn decode<'i>(
i: &mut &'i [u8],
) -> PResult<SignatureRef<'i>, winnow::error::VerboseError<&'i [u8], &'static str>> {
signature::decode.parse_next(i)
}

fn signature(
Expand All @@ -107,7 +109,8 @@ mod tests {
#[test]
fn tz_minus() {
assert_eq!(
decode(b"Sebastian Thiel <[email protected]> 1528473343 -0230")
decode
.parse_peek(b"Sebastian Thiel <[email protected]> 1528473343 -0230")
.expect("parse to work")
.1,
signature("Sebastian Thiel", "[email protected]", 1528473343, Sign::Minus, -9000)
Expand All @@ -117,7 +120,8 @@ mod tests {
#[test]
fn tz_plus() {
assert_eq!(
decode(b"Sebastian Thiel <[email protected]> 1528473343 +0230")
decode
.parse_peek(b"Sebastian Thiel <[email protected]> 1528473343 +0230")
.expect("parse to work")
.1,
signature("Sebastian Thiel", "[email protected]", 1528473343, Sign::Plus, 9000)
Expand All @@ -127,7 +131,8 @@ mod tests {
#[test]
fn negative_offset_0000() {
assert_eq!(
decode(b"Sebastian Thiel <[email protected]> 1528473343 -0000")
decode
.parse_peek(b"Sebastian Thiel <[email protected]> 1528473343 -0000")
.expect("parse to work")
.1,
signature("Sebastian Thiel", "[email protected]", 1528473343, Sign::Minus, 0)
Expand All @@ -137,7 +142,8 @@ mod tests {
#[test]
fn negative_offset_double_dash() {
assert_eq!(
decode(b"name <[email protected]> 1288373970 --700")
decode
.parse_peek(b"name <[email protected]> 1288373970 --700")
.expect("parse to work")
.1,
signature("name", "[email protected]", 1288373970, Sign::Minus, -252000)
Expand All @@ -147,30 +153,30 @@ mod tests {
#[test]
fn empty_name_and_email() {
assert_eq!(
decode(b" <> 12345 -1215").expect("parse to work").1,
decode.parse_peek(b" <> 12345 -1215").expect("parse to work").1,
signature("", "", 12345, Sign::Minus, -44100)
);
}

#[test]
fn invalid_signature() {
assert_eq!(
decode(b"hello < 12345 -1215")
decode.parse_peek(b"hello < 12345 -1215")
.map_err(to_bstr_err)
.expect_err("parse fails as > is missing")
.to_string(),
"Parse error:\nSlice at: 12345 -1215\nin section '<email>', at: 12345 -1215\nin section '<name> <<email>>', at: hello < 12345 -1215\nin section '<name> <<email>> <timestamp> <+|-><HHMM>', at: hello < 12345 -1215\n"
"Parse error:\nslice at: 12345 -1215\nin section '<email>', at: 12345 -1215\nin section '<name> <<email>>', at: 12345 -1215\nin section '<name> <<email>> <timestamp> <+|-><HHMM>', at: 12345 -1215\n"
);
}

#[test]
fn invalid_time() {
assert_eq!(
decode(b"hello <> abc -1215")
decode.parse_peek(b"hello <> abc -1215")
.map_err(to_bstr_err)
.expect_err("parse fails as > is missing")
.to_string(),
"Parse error:\nVerify at: abc -1215\nin section '<timestamp>', at: abc -1215\nin section '<name> <<email>> <timestamp> <+|-><HHMM>', at: hello <> abc -1215\n"
"Parse error:\npredicate verification at: abc -1215\nin section '<timestamp>', at: abc -1215\nin section '<name> <<email>> <timestamp> <+|-><HHMM>', at: abc -1215\n"
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions gix-actor/src/signature/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
mod _ref {
use bstr::ByteSlice;
use winnow::prelude::*;

use crate::{signature::decode, IdentityRef, Signature, SignatureRef};

impl<'a> SignatureRef<'a> {
/// Deserialize a signature from the given `data`.
pub fn from_bytes<E>(data: &'a [u8]) -> Result<SignatureRef<'a>, winnow::error::ErrMode<E>>
pub fn from_bytes<E>(mut data: &'a [u8]) -> Result<SignatureRef<'a>, winnow::error::ErrMode<E>>
where
E: winnow::error::ParserError<&'a [u8]> + winnow::error::AddContext<&'a [u8]>,
{
decode(data).map(|(_, t)| t)
decode.parse_next(&mut data)
}

/// Create an owned instance from this shared one.
Expand Down
2 changes: 1 addition & 1 deletion gix-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ gix-ref = { version = "^0.34.0", path = "../gix-ref" }
gix-glob = { version = "^0.10.2", path = "../gix-glob" }

log = "0.4.17"
winnow = { version = "0.5", features = ["simd"] }
winnow = { version = "0.5.12", features = ["simd"] }
memchr = "2"
thiserror = "1.0.26"
unicode-bom = "2.0.2"
Expand Down
2 changes: 1 addition & 1 deletion gix-object/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ btoi = "0.4.2"
itoa = "1.0.1"
thiserror = "1.0.34"
bstr = { version = "1.3.0", default-features = false, features = ["std", "unicode"] }
winnow = { version = "0.4", features = ["simd"] }
winnow = { version = "0.5.12", features = ["simd"] }
smallvec = { version = "1.4.0", features = ["write"] }
serde = { version = "1.0.114", optional = true, default-features = false, features = ["derive"]}

Expand Down
19 changes: 9 additions & 10 deletions gix-object/src/commit/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use winnow::{

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

pub fn message<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a BStr, E> {
pub fn message<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(i: &mut &'a [u8]) -> PResult<&'a BStr, E> {
if i.is_empty() {
// newline + [message]
return Err(
Expand All @@ -27,22 +27,21 @@ pub fn message<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(i: &'a [u8])
.parse_next(i)
}

pub fn commit<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], CommitRef<'a>, E> {
pub fn commit<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(i: &mut &'a [u8]) -> PResult<CommitRef<'a>, E> {
(
(|i| parse::header_field(i, b"tree", parse::hex_hash)).context("tree <40 lowercase hex char>"),
repeat(0.., |i| parse::header_field(i, b"parent", parse::hex_hash))
(|i: &mut _| parse::header_field(i, b"tree", parse::hex_hash)).context("tree <40 lowercase hex char>"),
repeat(0.., |i: &mut _| parse::header_field(i, b"parent", parse::hex_hash))
.map(|p: Vec<_>| p)
.context("zero or more 'parent <40 lowercase hex char>'"),
(|i| parse::header_field(i, b"author", parse::signature)).context("author <signature>"),
(|i| parse::header_field(i, b"committer", parse::signature)).context("committer <signature>"),
opt(|i| parse::header_field(i, b"encoding", take_till1(NL))).context("encoding <encoding>"),
(|i: &mut _| parse::header_field(i, b"author", parse::signature)).context("author <signature>"),
(|i: &mut _| parse::header_field(i, b"committer", parse::signature)).context("committer <signature>"),
opt(|i: &mut _| parse::header_field(i, b"encoding", take_till1(NL))).context("encoding <encoding>"),
repeat(
0..,
alt((
parse::any_header_field_multi_line.map(|(k, o)| (k.as_bstr(), Cow::Owned(o))),
|i| {
parse::any_header_field(i, take_till1(NL))
.map(|(i, (k, o))| (i, (k.as_bstr(), Cow::Borrowed(o.as_bstr()))))
|i: &mut _| {
parse::any_header_field(i, take_till1(NL)).map(|(k, o)| (k.as_bstr(), Cow::Borrowed(o.as_bstr())))
},
)),
)
Expand Down
19 changes: 10 additions & 9 deletions gix-object/src/commit/message/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ pub struct TrailerRef<'a> {
pub value: &'a BStr,
}

fn parse_single_line_trailer<'a, E: ParserError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&'a BStr, &'a BStr), E> {
let (i, (token, value)) = separated_pair(take_until1(b":".as_ref()), b": ", rest).parse_next(i.trim_end())?;
fn parse_single_line_trailer<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> PResult<(&'a BStr, &'a BStr), E> {
*i = i.trim_end();
let (token, value) = separated_pair(take_until1(b":".as_ref()), b": ", rest).parse_next(i)?;

if token.trim_end().len() != token.len() || value.trim_start().len() != value.len() {
Err(winnow::error::ErrMode::from_error_kind(i, ErrorKind::Fail).cut())
} else {
Ok((i, (token.as_bstr(), value.as_bstr())))
Ok((token.as_bstr(), value.as_bstr()))
}
}

Expand All @@ -51,12 +52,12 @@ impl<'a> Iterator for Trailers<'a> {
if self.cursor.is_empty() {
return None;
}
for line in self.cursor.lines_with_terminator() {
for mut line in self.cursor.lines_with_terminator() {
self.cursor = &self.cursor[line.len()..];
if let Some(trailer) = terminated(parse_single_line_trailer::<()>, eof)
.parse_next(line)
.parse_next(&mut line)
.ok()
.map(|(_, (token, value))| TrailerRef {
.map(|(token, value)| TrailerRef {
token: token.trim().as_bstr(),
value: value.trim().as_bstr(),
})
Expand Down Expand Up @@ -121,7 +122,7 @@ mod test_parse_trailer {
use super::*;

fn parse(input: &str) -> (&BStr, &BStr) {
parse_single_line_trailer::<()>(input.as_bytes()).unwrap().1
parse_single_line_trailer::<()>.parse_peek(input.as_bytes()).unwrap().1
}

#[test]
Expand All @@ -144,8 +145,8 @@ mod test_parse_trailer {

#[test]
fn extra_whitespace_before_token_or_value_is_error() {
assert!(parse_single_line_trailer::<()>(b"foo : bar").is_err());
assert!(parse_single_line_trailer::<()>(b"foo: bar").is_err())
assert!(parse_single_line_trailer::<()>.parse_peek(b"foo : bar").is_err());
assert!(parse_single_line_trailer::<()>.parse_peek(b"foo: bar").is_err())
}

#[test]
Expand Down
Loading

0 comments on commit 3f8c91f

Please sign in to comment.