Skip to content

Commit e3acb05

Browse files
committed
chore(nix-compat): bump to nom 8.x
See https://github.com/rust-bakery/nom/blob/72dd5818b70f16a691b6f016d34774f2cfc7c0c7/CHANGELOG.md for the nom changelog. Most notably, there's now a .parse() to be added: `combinator(arg)(input)` -> `combinator(arg).parse(input)` There also doesn't need to be a tuple combinator (it's implemented on tuples directly). This also refactors the string / byte field parsing parts, to make them more concise. Change-Id: I9e8a3cedd07d6705be391898eb6a486fb8164069 Reviewed-on: https://cl.tvl.fyi/c/depot/+/13193 Tested-by: BuildkiteCI Reviewed-by: edef <[email protected]> Reviewed-by: Brian Olsen <[email protected]>
1 parent 4f17295 commit e3acb05

File tree

5 files changed

+53
-73
lines changed

5 files changed

+53
-73
lines changed

Cargo.lock

+2-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.nix

+4-23
Original file line numberDiff line numberDiff line change
@@ -7513,20 +7513,6 @@ rec {
75137513
};
75147514
resolvedDefaultFeatures = [ "default" "rev-mappings" ];
75157515
};
7516-
"minimal-lexical" = rec {
7517-
crateName = "minimal-lexical";
7518-
version = "0.2.1";
7519-
edition = "2018";
7520-
sha256 = "16ppc5g84aijpri4jzv14rvcnslvlpphbszc7zzp6vfkddf4qdb8";
7521-
libName = "minimal_lexical";
7522-
authors = [
7523-
"Alex Huszagh <[email protected]>"
7524-
];
7525-
features = {
7526-
"default" = [ "std" ];
7527-
};
7528-
resolvedDefaultFeatures = [ "std" ];
7529-
};
75307516
"miniz_oxide" = rec {
75317517
crateName = "miniz_oxide";
75327518
version = "0.8.2";
@@ -8528,9 +8514,9 @@ rec {
85288514
};
85298515
"nom" = rec {
85308516
crateName = "nom";
8531-
version = "7.1.3";
8532-
edition = "2018";
8533-
sha256 = "0jha9901wxam390jcf5pfa0qqfrgh8li787jx2ip0yk5b8y9hwyj";
8517+
version = "8.0.0";
8518+
edition = "2021";
8519+
sha256 = "01cl5xng9d0gxf26h39m0l8lprgpa00fcc75ps1yzgbib1vn35yz";
85348520
authors = [
85358521
85368522
];
@@ -8540,15 +8526,10 @@ rec {
85408526
packageId = "memchr";
85418527
usesDefaultFeatures = false;
85428528
}
8543-
{
8544-
name = "minimal-lexical";
8545-
packageId = "minimal-lexical";
8546-
usesDefaultFeatures = false;
8547-
}
85488529
];
85498530
features = {
85508531
"default" = [ "std" ];
8551-
"std" = [ "alloc" "memchr/std" "minimal-lexical/std" ];
8532+
"std" = [ "alloc" "memchr/std" ];
85528533
};
85538534
resolvedDefaultFeatures = [ "alloc" "default" "std" ];
85548535
};

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ md-5 = "0.10.6"
8585
mimalloc = "0.1.43"
8686
nix = "0.27.1"
8787
nohash-hasher = "0.2.0"
88-
nom = "7.1.3"
88+
nom = "8.0"
8989
num-traits = "0.2.19"
9090
object_store = "0.10.2"
9191
opentelemetry = "0.28.0"

nix-compat/src/aterm/parser.rs

+14-25
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
//! [ATerm]: http://program-transformation.org/Tools/ATermFormat.html
55
use bstr::BString;
66
use nom::branch::alt;
7-
use nom::bytes::complete::{escaped_transform, is_not, tag};
7+
use nom::bytes::complete::{escaped_transform, is_not};
88
use nom::character::complete::char as nomchar;
9-
use nom::combinator::{map, value};
9+
use nom::combinator::{map_res, opt, value};
1010
use nom::multi::separated_list0;
1111
use nom::sequence::delimited;
12-
use nom::IResult;
12+
use nom::{IResult, Parser};
1313

1414
/// Parse a bstr and undo any escaping (which is why this needs to allocate).
1515
// FUTUREWORK: have a version for fields that are known to not need escaping
@@ -32,48 +32,37 @@ fn parse_escaped_bytes(i: &[u8]) -> IResult<&[u8], BString> {
3232
/// Parse a field in double quotes, undo any escaping, and return the unquoted
3333
/// and decoded `Vec<u8>`.
3434
pub(crate) fn parse_bytes_field(i: &[u8]) -> IResult<&[u8], BString> {
35-
// inside double quotes…
3635
delimited(
3736
nomchar('\"'),
38-
// There is
39-
alt((
40-
// …either is a bstr after unescaping
41-
parse_escaped_bytes,
42-
// …or an empty string.
43-
map(tag(b""), |_| BString::default()),
44-
)),
37+
opt(parse_escaped_bytes).map(|opt_bstr| opt_bstr.unwrap_or_default()),
4538
nomchar('\"'),
46-
)(i)
39+
)
40+
.parse(i)
4741
}
4842

4943
/// Parse a field in double quotes, undo any escaping, and return the unquoted
5044
/// and decoded [String], if it's valid UTF-8.
5145
/// Or fail parsing if the bytes are no valid UTF-8.
5246
pub(crate) fn parse_string_field(i: &[u8]) -> IResult<&[u8], String> {
53-
// inside double quotes…
5447
delimited(
5548
nomchar('\"'),
56-
// There is
57-
alt((
58-
// either is a String after unescaping
59-
nom::combinator::map_opt(parse_escaped_bytes, |escaped_bytes| {
60-
String::from_utf8(escaped_bytes.into()).ok()
61-
}),
62-
// or an empty string.
63-
map(tag(b""), |_| "".to_string()),
64-
)),
49+
map_res(
50+
opt(parse_escaped_bytes).map(|opt_bstr| opt_bstr.unwrap_or_default()),
51+
|bstr| String::from_utf8(bstr.to_vec()),
52+
),
6553
nomchar('\"'),
66-
)(i)
54+
)
55+
.parse(i)
6756
}
6857

6958
/// Parse a list of string fields (enclosed in brackets)
7059
pub(crate) fn parse_string_list(i: &[u8]) -> IResult<&[u8], Vec<String>> {
71-
// inside brackets
7260
delimited(
7361
nomchar('['),
7462
separated_list0(nomchar(','), parse_string_field),
7563
nomchar(']'),
76-
)(i)
64+
)
65+
.parse(i)
7766
}
7867

7968
#[cfg(test)]

nix-compat/src/derivation/parser.rs

+32-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use nom::bytes::complete::tag;
77
use nom::character::complete::char as nomchar;
88
use nom::combinator::{all_consuming, map_res};
99
use nom::multi::{separated_list0, separated_list1};
10-
use nom::sequence::{delimited, preceded, separated_pair, terminated, tuple};
10+
use nom::sequence::{delimited, preceded, separated_pair, terminated};
11+
use nom::Parser;
1112
use std::collections::{btree_map, BTreeMap, BTreeSet};
1213
use thiserror;
1314

@@ -27,7 +28,7 @@ pub enum Error<I> {
2728
}
2829

2930
pub(crate) fn parse(i: &[u8]) -> Result<Derivation, Error<&[u8]>> {
30-
match all_consuming(parse_derivation)(i) {
31+
match all_consuming(parse_derivation).parse(i) {
3132
Ok((rest, derivation)) => {
3233
// this shouldn't happen, as all_consuming shouldn't return.
3334
debug_assert!(rest.is_empty());
@@ -68,13 +69,14 @@ fn parse_output(i: &[u8]) -> NomResult<&[u8], (String, Output)> {
6869
nomchar('('),
6970
map_res(
7071
|i| {
71-
tuple((
72+
(
7273
terminated(aterm::parse_string_field, nomchar(',')),
7374
terminated(aterm::parse_string_field, nomchar(',')),
7475
terminated(aterm::parse_string_field, nomchar(',')),
7576
aterm::parse_bytes_field,
76-
))(i)
77-
.map_err(into_nomerror)
77+
)
78+
.parse(i)
79+
.map_err(into_nomerror)
7880
},
7981
|(output_name, output_path, algo_and_mode, encoded_digest)| {
8082
// convert these 4 fields into an [Output].
@@ -114,7 +116,8 @@ fn parse_output(i: &[u8]) -> NomResult<&[u8], (String, Output)> {
114116
},
115117
),
116118
nomchar(')'),
117-
)(i)
119+
)
120+
.parse(i)
118121
}
119122

120123
/// Parse multiple outputs in ATerm. This is a list of things acccepted by
@@ -127,7 +130,8 @@ fn parse_outputs(i: &[u8]) -> NomResult<&[u8], BTreeMap<String, Output>> {
127130
nomchar('['),
128131
separated_list1(tag(","), parse_output),
129132
nomchar(']'),
130-
)(i);
133+
)
134+
.parse(i);
131135

132136
match res {
133137
Ok((rst, outputs_lst)) => {
@@ -228,22 +232,34 @@ pub fn parse_derivation(i: &[u8]) -> NomResult<&[u8], Derivation> {
228232
nomchar('('),
229233
// tuple requires all errors to be of the same type, so we need to be a
230234
// bit verbose here wrapping generic IResult into [NomATermResult].
231-
tuple((
235+
(
232236
// parse outputs
233237
terminated(parse_outputs, nomchar(',')),
234238
// // parse input derivations
235239
terminated(parse_input_derivations, nomchar(',')),
236240
// // parse input sources
237241
terminated(parse_input_sources, nomchar(',')),
238242
// // parse system
239-
|i| terminated(aterm::parse_string_field, nomchar(','))(i).map_err(into_nomerror),
243+
|i| {
244+
terminated(aterm::parse_string_field, nomchar(','))
245+
.parse(i)
246+
.map_err(into_nomerror)
247+
},
240248
// // parse builder
241-
|i| terminated(aterm::parse_string_field, nomchar(','))(i).map_err(into_nomerror),
249+
|i| {
250+
terminated(aterm::parse_string_field, nomchar(','))
251+
.parse(i)
252+
.map_err(into_nomerror)
253+
},
242254
// // parse arguments
243-
|i| terminated(aterm::parse_string_list, nomchar(','))(i).map_err(into_nomerror),
255+
|i| {
256+
terminated(aterm::parse_string_list, nomchar(','))
257+
.parse(i)
258+
.map_err(into_nomerror)
259+
},
244260
// parse environment
245261
parse_kv(aterm::parse_bytes_field),
246-
)),
262+
),
247263
nomchar(')'),
248264
)
249265
.map(
@@ -267,7 +283,8 @@ pub fn parse_derivation(i: &[u8]) -> NomResult<&[u8], Derivation> {
267283
}
268284
},
269285
),
270-
)(i)
286+
)
287+
.parse(i)
271288
}
272289

273290
/// Parse a list of key/value pairs into a BTreeMap.
@@ -298,7 +315,7 @@ where
298315
),
299316
nomchar(')'),
300317
),
301-
)(ii).map_err(into_nomerror);
318+
).parse(ii).map_err(into_nomerror);
302319

303320
match res {
304321
Ok((rest, pairs)) => {
@@ -322,7 +339,7 @@ where
322339
}
323340
},
324341
nomchar(']'),
325-
)(i)
342+
).parse(i)
326343
}
327344

328345
#[cfg(test)]

0 commit comments

Comments
 (0)