Skip to content

Commit ec25a4d

Browse files
David Tolnayfacebook-github-bot
authored andcommitted
Update hgproto from nom v7 to nom v8
Differential Revision: D75796556 fbshipit-source-id: 65c4b6be9b07e78985a41dcd6ca2f4cb256261d0
1 parent 4f328b4 commit ec25a4d

3 files changed

Lines changed: 56 additions & 47 deletions

File tree

eden/mononoke/hgproto/BUCK

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ rust_library(
2222
"fbsource//third-party/rust:futures",
2323
"fbsource//third-party/rust:hex",
2424
"fbsource//third-party/rust:itertools",
25-
"fbsource//third-party/rust:nom-old",
25+
"fbsource//third-party/rust:nom",
2626
"fbsource//third-party/rust:pin-project",
2727
"fbsource//third-party/rust:thiserror",
2828
"fbsource//third-party/rust:tokio",

eden/mononoke/hgproto/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ itertools = "0.14.0"
1717
mercurial_bundles = { version = "0.1.0", path = "../mercurial/bundles" }
1818
mercurial_types = { version = "0.1.0", path = "../mercurial/types" }
1919
mononoke_types = { version = "0.1.0", path = "../mononoke_types" }
20-
nom = "7.1"
20+
nom = "8"
2121
pin-project = "0.4.30"
2222
qps = { version = "0.1.0", path = "../server/qps" }
2323
slog = { package = "tracing_slog_compat", version = "0.1.0", git = "https://github.com/facebookexperimental/rust-shed.git", branch = "main" }

eden/mononoke/hgproto/src/sshproto/request.rs

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ use hex::FromHex;
2121
use mercurial_types::HgChangesetId;
2222
use mercurial_types::HgManifestId;
2323
use mononoke_types::path::MPath;
24+
use nom::AsChar as _;
2425
use nom::Err;
2526
use nom::IResult;
27+
use nom::Input as _;
2628
use nom::Needed;
2729
use nom::Parser;
2830
use nom::branch::alt;
2931
use nom::bytes::streaming::tag;
3032
use nom::bytes::streaming::take;
3133
use nom::bytes::streaming::take_while;
3234
use nom::bytes::streaming::take_while1;
33-
use nom::character::is_alphanumeric;
34-
use nom::character::is_digit;
3535
use nom::character::streaming::digit1;
3636
use nom::combinator::complete;
3737
use nom::combinator::map;
@@ -44,7 +44,6 @@ use nom::multi::many0;
4444
use nom::multi::separated_list0;
4545
use nom::sequence::separated_pair;
4646
use nom::sequence::terminated;
47-
use nom::sequence::tuple;
4847

4948
use crate::GetbundleArgs;
5049
use crate::GettreepackArgs;
@@ -80,7 +79,6 @@ impl ParseError<&[u8]> for Error {
8079
fn take_until1(substr: &str) -> impl Fn(&[u8]) -> IResult<&[u8], &[u8], Error> {
8180
move |input: &[u8]| {
8281
use nom::FindSubstring as _;
83-
use nom::InputTake as _;
8482

8583
match input.find_substring(substr) {
8684
None => Err(nom::Err::Incomplete(Needed::new(1 + substr.len()))),
@@ -95,22 +93,22 @@ fn take_until1(substr: &str) -> impl Fn(&[u8]) -> IResult<&[u8], &[u8], Error> {
9593

9694
fn take_until_and_consume1<'a>(
9795
substr: &str,
98-
) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], &'a [u8], Error> {
96+
) -> impl Parser<&'a [u8], Output = &'a [u8], Error = Error> {
9997
terminated(take_until1(substr), tag(substr))
10098
}
10199

102-
fn separated_list_complete<'a, F, O>(
100+
fn separated_list_complete<'a, F>(
103101
sep: &str,
104102
element: F,
105-
) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], Vec<O>, Error>
103+
) -> impl Parser<&'a [u8], Output = Vec<F::Output>, Error = F::Error>
106104
where
107-
F: Parser<&'a [u8], O, Error>,
105+
F: Parser<&'a [u8]>,
108106
{
109107
separated_list0(complete(tag(sep)), complete(element))
110108
}
111109

112110
fn integer(input: &[u8]) -> IResult<&[u8], usize, Error> {
113-
map_res(map_res(digit1, str::from_utf8), usize::from_str)(input)
111+
map_res(map_res(digit1, str::from_utf8), usize::from_str).parse(input)
114112
}
115113

116114
/// Return an identifier of the form [a-zA-Z_][a-zA-Z0-9_]*. Returns Incomplete
@@ -144,30 +142,32 @@ fn ident_complete(input: &[u8]) -> IResult<&[u8], &[u8], Error> {
144142
// Assumption: input is complete
145143
// We can't use 'integer' defined above as it reads until a non digit character
146144
fn boolean(input: &[u8]) -> IResult<&[u8], bool, Error> {
147-
map_res(alt((complete(take_while1(is_digit)), rest)), |s| {
145+
map_res(alt((complete(take_while1(u8::is_dec_digit)), rest)), |s| {
148146
let s = str::from_utf8(s)?;
149147
anyhow::Ok(u32::from_str(s)? != 0)
150-
})(input)
148+
})
149+
.parse(input)
151150
}
152151

153152
fn batch_param_comma_separated(input: &[u8]) -> IResult<&[u8], Bytes, Error> {
154153
map_res(terminated(take_while(notcomma), take(1usize)), |k| {
155154
batch::unescape(k).map(Bytes::from)
156-
})(input)
155+
})
156+
.parse(input)
157157
}
158158

159159
// List of comma-separated values, each of which is encoded using batch param encoding.
160160
fn gettreepack_directories(input: &[u8]) -> IResult<&[u8], Vec<Bytes>, Error> {
161-
many0(complete(batch_param_comma_separated))(input)
161+
many0(complete(batch_param_comma_separated)).parse(input)
162162
}
163163

164164
// A "*" parameter is a meta-parameter - its argument is a count of
165165
// a number of other parameters. (We accept nested/recursive star parameters,
166166
// but I don't know if that ever happens in practice.)
167167
fn param_star(input: &[u8]) -> IResult<&[u8], HashMap<&[u8], &[u8]>, Error> {
168-
let (input, _) = tag("* ")(input)?;
168+
let (input, _) = tag("* ").parse(input)?;
169169
let (input, count) = integer(input)?;
170-
let (input, _) = tag("\n")(input)?;
170+
let (input, _) = tag("\n").parse(input)?;
171171
params_ref(input, count)
172172
}
173173

@@ -177,10 +177,10 @@ fn param_star(input: &[u8]) -> IResult<&[u8], HashMap<&[u8], &[u8]>, Error> {
177177
// <bytelen bytes>
178178
fn param_kv(input: &[u8]) -> IResult<&[u8], HashMap<&[u8], &[u8]>, Error> {
179179
let (input, key) = ident(input)?;
180-
let (input, _) = tag(" ")(input)?;
180+
let (input, _) = tag(" ").parse(input)?;
181181
let (input, len) = integer(input)?;
182-
let (input, _) = tag("\n")(input)?;
183-
let (input, val) = take(len)(input)?;
182+
let (input, _) = tag("\n").parse(input)?;
183+
let (input, val) = take(len).parse(input)?;
184184
Ok((input, iter::once((key, val)).collect()))
185185
}
186186

@@ -193,7 +193,7 @@ fn params_ref(mut input: &[u8], count: usize) -> IResult<&[u8], HashMap<&[u8], &
193193
let mut ret = HashMap::with_capacity(count);
194194

195195
for _ in 0..count {
196-
let (rest, val) = alt((param_star, param_kv))(input)?;
196+
let (rest, val) = alt((param_star, param_kv)).parse(input)?;
197197
ret.extend(val);
198198
input = rest;
199199
}
@@ -231,10 +231,11 @@ fn notcomma(b: u8) -> bool {
231231
// (which is actually from the "batch" command "cmds" parameter), or at a ',', as they're
232232
// comma-delimited.
233233
fn batch_param_escaped(input: &[u8]) -> IResult<&[u8], (Vec<u8>, Vec<u8>), Error> {
234-
tuple((
234+
(
235235
map_res(take_until_and_consume1("="), batch::unescape),
236236
map_res(alt((complete(take_while(notcomma)), rest)), batch::unescape),
237-
))(input)
237+
)
238+
.parse(input)
238239
}
239240

240241
// Extract parameters from batch - same signature as params
@@ -244,56 +245,60 @@ fn batch_params(input: &[u8], _count: usize) -> IResult<&[u8], HashMap<Vec<u8>,
244245
map(
245246
separated_list_complete(",", batch_param_escaped),
246247
HashMap::from_iter,
247-
)(input)
248+
)
249+
.parse(input)
248250
}
249251

250252
// A nodehash is simply 40 hex digits.
251253
fn nodehash(input: &[u8]) -> IResult<&[u8], HgChangesetId, Error> {
252254
map_res(
253255
map_res(take(40usize), str::from_utf8),
254256
HgChangesetId::from_str,
255-
)(input)
257+
)
258+
.parse(input)
256259
}
257260

258261
// A manifestid is simply 40 hex digits.
259262
fn manifestid(input: &[u8]) -> IResult<&[u8], HgManifestId, Error> {
260263
map_res(
261264
map_res(take(40usize), str::from_utf8),
262265
HgManifestId::from_str,
263-
)(input)
266+
)
267+
.parse(input)
264268
}
265269

266270
// A pair of nodehashes, separated by '-'
267271
fn pair(input: &[u8]) -> IResult<&[u8], (HgChangesetId, HgChangesetId), Error> {
268-
separated_pair(nodehash, tag("-"), nodehash)(input)
272+
separated_pair(nodehash, tag("-"), nodehash).parse(input)
269273
}
270274

271275
// A space-separated list of pairs.
272276
fn pairlist(input: &[u8]) -> IResult<&[u8], Vec<(HgChangesetId, HgChangesetId)>, Error> {
273-
separated_list_complete(" ", pair)(input)
277+
separated_list_complete(" ", pair).parse(input)
274278
}
275279

276280
// A space-separated list of changeset IDs
277281
fn hashlist(input: &[u8]) -> IResult<&[u8], Vec<HgChangesetId>, Error> {
278-
separated_list_complete(" ", nodehash)(input)
282+
separated_list_complete(" ", nodehash).parse(input)
279283
}
280284

281285
// A changeset is simply 40 hex digits.
282286
fn hg_changeset_id(input: &[u8]) -> IResult<&[u8], HgChangesetId, Error> {
283287
map_res(
284288
map_res(take(40usize), str::from_utf8),
285289
HgChangesetId::from_str,
286-
)(input)
290+
)
291+
.parse(input)
287292
}
288293

289294
// A space-separated list of hg changesets
290295
fn hg_changeset_list(input: &[u8]) -> IResult<&[u8], Vec<HgChangesetId>, Error> {
291-
separated_list_complete(" ", hg_changeset_id)(input)
296+
separated_list_complete(" ", hg_changeset_id).parse(input)
292297
}
293298

294299
// A space-separated list of manifest IDs
295300
fn manifestlist(input: &[u8]) -> IResult<&[u8], Vec<HgManifestId>, Error> {
296-
separated_list_complete(" ", manifestid)(input)
301+
separated_list_complete(" ", manifestid).parse(input)
297302
}
298303

299304
// A space-separated list of strings
@@ -302,12 +307,13 @@ fn stringlist(input: &[u8]) -> IResult<&[u8], Vec<String>, Error> {
302307
complete(tag(" ")),
303308
map_res(
304309
map_res(
305-
alt((complete(take_while(is_alphanumeric)), rest)),
310+
alt((complete(take_while(u8::is_alphanum)), rest)),
306311
str::from_utf8,
307312
),
308313
FromStr::from_str,
309314
),
310-
)(input)
315+
)
316+
.parse(input)
311317
}
312318

313319
fn hex_stringlist(input: &[u8]) -> IResult<&[u8], Vec<String>, Error> {
@@ -319,7 +325,8 @@ fn hex_stringlist(input: &[u8]) -> IResult<&[u8], Vec<String>, Error> {
319325
.and_then(|v| String::from_utf8(v).map_err(anyhow::Error::from))
320326
})
321327
.collect::<Result<Vec<String>>>()
322-
})(input)
328+
})
329+
.parse(input)
323330
}
324331

325332
/// A comma-separated list of arbitrary values. The input is assumed to be
@@ -347,14 +354,14 @@ fn notsemi(b: u8) -> bool {
347354
// A command in a batch. Commands are represented as "command parameters". The parameters
348355
// end either at the end of the buffer or at ';'.
349356
fn cmd(input: &[u8]) -> IResult<&[u8], (Vec<u8>, Vec<u8>), Error> {
350-
let (input, cmd) = take_until_and_consume1(" ")(input)?;
351-
let (input, args) = alt((complete(take_while(notsemi)), rest))(input)?;
357+
let (input, cmd) = take_until_and_consume1(" ").parse(input)?;
358+
let (input, args) = alt((complete(take_while(notsemi)), rest)).parse(input)?;
352359
Ok((input, (cmd.to_vec(), args.to_vec())))
353360
}
354361

355362
// A list of batched commands - the list is delimited by ';'.
356363
fn cmdlist(input: &[u8]) -> IResult<&[u8], Vec<(Vec<u8>, Vec<u8>)>, Error> {
357-
separated_list0(complete(tag(";")), cmd)(input)
364+
separated_list0(complete(tag(";")), cmd).parse(input)
358365
}
359366

360367
/// Given a hash of parameters, look up a parameter by name, and if it exists,
@@ -411,11 +418,11 @@ fn parseval_option<'a, F, T>(
411418
mut parser: F,
412419
) -> Result<Option<T>>
413420
where
414-
F: FnMut(&'a [u8]) -> IResult<&'a [u8], T, Error>,
421+
F: Parser<&'a [u8], Output = T, Error = Error>,
415422
{
416423
match params.get(key.as_bytes()) {
417424
None => Ok(None),
418-
Some(v) => match parser(v.as_ref()) {
425+
Some(v) => match parser.parse(v.as_ref()) {
419426
Ok((unparsed, v)) => match unparsed {
420427
[] => Ok(Some(v)),
421428
[..] => bail!(
@@ -444,8 +451,8 @@ where
444451
C: AsRef<[u8]>,
445452
{
446453
move |input| {
447-
let (input, _) = tag(cmd.as_ref())(input)?;
448-
let (input, _) = tag("\n")(input)?;
454+
let (input, _) = tag(cmd.as_ref()).parse(input)?;
455+
let (input, _) = tag("\n").parse(input)?;
449456
let (input, v) = parse_params(input, nargs)?;
450457

451458
match func(v) {
@@ -539,12 +546,13 @@ fn parse_batchrequest(input: &[u8]) -> IResult<&[u8], Vec<SingleRequest>, Error>
539546

540547
let (rest, batch) = command_star!("batch", Batch, params, {
541548
cmds => cmdlist,
542-
})(input)?;
549+
})
550+
.parse(input)?;
543551

544552
let mut parsed_cmds = Vec::with_capacity(batch.cmds.len());
545553
for cmd in batch.cmds {
546554
let full_cmd = Bytes::from([cmd.0, cmd.1].join(&b'\n'));
547-
let ([], cmd) = complete(parse_cmd)(&full_cmd)? else {
555+
let ([], cmd) = complete(parse_cmd).parse(&full_cmd)? else {
548556
return Err(Err::Error(Error::Nom(ErrorKind::Eof)));
549557
};
550558
parsed_cmds.push(cmd);
@@ -556,7 +564,8 @@ pub fn parse_request(buf: &mut BytesMut) -> Result<Option<Request>> {
556564
let res = alt((
557565
map(parse_batchrequest, Request::Batch),
558566
map(parse_singlerequest, Request::Single),
559-
))(buf);
567+
))
568+
.parse(buf);
560569

561570
match res {
562571
Ok((rest, val)) => {
@@ -648,7 +657,7 @@ fn parse_with_params(
648657
basemfnodes: parseval(&kv, "basemfnodes", manifestlist)?.into_iter().collect(),
649658
directories: parseval(&kv, "directories", gettreepack_directories)?,
650659
depth: parseval_option(&kv, "depth", map_res(
651-
map_res(alt((complete(take_while1(is_digit)), rest)), str::from_utf8),
660+
map_res(alt((complete(take_while1(u8::is_dec_digit)), rest)), str::from_utf8),
652661
usize::from_str
653662
))?,
654663
}))
@@ -663,7 +672,7 @@ fn parse_with_params(
663672
command!("getcommitdata", GetCommitData, parse_params, {
664673
nodes => hg_changeset_list,
665674
}),
666-
))(input)
675+
)).parse(input)
667676
}
668677

669678
/// Test individual combinators

0 commit comments

Comments
 (0)