Skip to content

Commit 2fb4a54

Browse files
committed
Simplify LineRef parser
1 parent f0cbf81 commit 2fb4a54

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

gix-ref/src/store/file/log/line.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ impl<'a> From<LineRef<'a>> for Line {
7575
pub mod decode {
7676
use gix_object::bstr::{BStr, ByteSlice};
7777
use winnow::{
78+
combinator::alt,
79+
combinator::eof,
80+
combinator::fail,
7881
combinator::opt,
82+
combinator::preceded,
7983
combinator::terminated,
8084
error::{AddContext, ParserError},
8185
prelude::*,
@@ -134,28 +138,22 @@ pub mod decode {
134138
}
135139

136140
fn one<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(bytes: &'a [u8]) -> IResult<&[u8], LineRef<'a>, E> {
137-
let (i, (old, new, signature, message_sep, message)) = (
138-
terminated(hex_hash, b" ").context("<old-hexsha>"),
139-
terminated(hex_hash, b" ").context("<new-hexsha>"),
140-
gix_actor::signature::decode.context("<name> <<email>> <timestamp>"),
141-
opt(b'\t'),
142-
message.context("<optional message>"),
141+
let (i, ((old, new, signature), message)) = (
142+
(
143+
terminated(hex_hash, b" ").context("<old-hexsha>"),
144+
terminated(hex_hash, b" ").context("<new-hexsha>"),
145+
gix_actor::signature::decode.context("<name> <<email>> <timestamp>"),
146+
)
147+
.context("<old-hexsha> <new-hexsha> <name> <<email>> <timestamp> <tz>\\t<message>"),
148+
alt((
149+
preceded(b'\t', message.context("<optional message>")),
150+
b'\n'.value(Default::default()),
151+
eof.value(Default::default()),
152+
fail.context("log message must be separated from signature with whitespace"),
153+
)),
143154
)
144-
.context("<old-hexsha> <new-hexsha> <name> <<email>> <timestamp> <tz>\\t<message>")
145155
.parse_next(bytes)?;
146156

147-
if message_sep.is_none() {
148-
if let Some(first) = message.first() {
149-
if !first.is_ascii_whitespace() {
150-
return Err(
151-
winnow::error::ErrMode::from_error_kind(i, winnow::error::ErrorKind::Verify).map(|err: E| {
152-
err.add_context(i, "log message must be separated from signature with whitespace")
153-
}),
154-
);
155-
}
156-
}
157-
}
158-
159157
Ok((
160158
i,
161159
LineRef {
@@ -204,9 +202,11 @@ pub mod decode {
204202
let err = one::<VerboseError<&[u8]>>(line.as_bytes())
205203
.map_err(to_bstr_err)
206204
.expect_err("this should fail");
207-
assert!(err
208-
.to_string()
209-
.contains("log message must be separated from signature with whitespace"));
205+
assert!(
206+
err.to_string()
207+
.contains("log message must be separated from signature with whitespace"),
208+
"expected\n `log message must be separated from signature with whitespace`\nin\n```\n{err}\n```"
209+
);
210210
}
211211
}
212212

0 commit comments

Comments
 (0)