Skip to content

Commit b0eb28f

Browse files
author
Joe Grund
authored
Fixup leading zero parsing (#10)
Our parser strips leading zeros in a non-conformant way leading to suprising results. Signed-off-by: Joe Grund <jgrund@whamcloud.io>
1 parent 35cefaa commit b0eb28f

17 files changed

Lines changed: 247 additions & 34 deletions

Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
[package]
2-
name = "hostlist-parser"
2+
authors = ["EMF Team <emf@whamcloud.com>"]
33
description = "Parses hostlist expressions into a Vec of Strings"
4-
repository = "https://github.com/whamcloud/hostlist-parser"
4+
edition = "2021"
55
keywords = ["hostlist", "pdsh", "hpc", "cluster"]
66
license = "MIT"
7-
version = "0.1.5"
8-
authors = ["EMF Team <emf@whamcloud.com>"]
9-
edition = "2021"
7+
name = "hostlist-parser"
8+
repository = "https://github.com/whamcloud/hostlist-parser"
9+
version = "0.1.6"
1010

1111
[dependencies]
1212
combine = "4.6"
13-
itertools = "0.10"
13+
itertools = "0.12"
1414

1515
[dev-dependencies]
16-
insta = "1.12"
16+
insta = "1"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# hostlist-parser
22

3-
![Crates.io](https://img.shields.io/crates/v/hostlist-parser) ![docs.rs](https://docs.rs/hostlist-parser/badge.svg?version=0.1.5)
3+
![Crates.io](https://img.shields.io/crates/v/hostlist-parser) ![docs.rs](https://docs.rs/hostlist-parser/badge.svg?version=0.1.6)
44

55
Parses hostlist expressions into a deduped `Vec` of Strings
66

src/lib.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,20 @@ where
106106
optional_spaces().with(leading_zeros()),
107107
))
108108
.and_then(|((start_zeros, start), _, (end_zeros, end))| {
109-
let mut xs = vec![start, end];
109+
let mut xs = [start, end];
110110
xs.sort_unstable();
111111

112+
let same_prefix_len = start_zeros == end_zeros;
113+
112114
let (range, start_zeros, end_zeros) = if start > end {
113115
(
114-
RangeOutput::RangeReversed(end_zeros, end, start),
116+
RangeOutput::RangeReversed(end_zeros, same_prefix_len, end, start),
115117
end_zeros,
116118
start_zeros,
117119
)
118120
} else {
119121
(
120-
RangeOutput::Range(start_zeros, start, end),
122+
RangeOutput::Range(start_zeros, same_prefix_len, start, end),
121123
start_zeros,
122124
end_zeros,
123125
)
@@ -206,7 +208,7 @@ pub fn parse(input: &str) -> Result<Vec<String>, combine::stream::easy::Errors<c
206208
.easy_parse(input)
207209
.map_err(|err| err.map_position(|p| p.translate_position(input)))?;
208210

209-
let mut xs = Vec::new();
211+
let mut xs = vec![];
210212

211213
for parts in hosts {
212214
let x_prod: Vec<_> = parts
@@ -218,8 +220,6 @@ pub fn parse(input: &str) -> Result<Vec<String>, combine::stream::easy::Errors<c
218220

219221
// No ranges means no interpolation
220222
if x_prod.is_empty() {
221-
if parts.is_empty() {}
222-
223223
let mut s = String::new();
224224

225225
for p in parts.clone() {
@@ -263,6 +263,8 @@ mod tests {
263263
assert_debug_snapshot!(leading_zeros().easy_parse("01"));
264264
assert_debug_snapshot!(leading_zeros().easy_parse("00"));
265265
assert_debug_snapshot!(leading_zeros().easy_parse("0"));
266+
assert_debug_snapshot!(leading_zeros().easy_parse("042"));
267+
assert_debug_snapshot!(leading_zeros().easy_parse("042"));
266268
}
267269

268270
#[test]
@@ -478,6 +480,13 @@ mod tests {
478480
);
479481
}
480482

483+
#[test]
484+
fn test_parse_large_expression() {
485+
let xs = parse("atla-pio-03-o[048-051],atla-pio-05-o[052-055],atla-pio-07-o[056-059],atla-pio-09-o[060-063],atla-pio-11-o[064-067],atla-pio-13-o[068-071],atla-pip-03-o[072-075],atla-pip-05-o[076-079],atla-pip-07-o[080-083],atla-pip-11-o[088-091],atla-pip-13-o[092-095],atla-pip-09-o[085,087],atla-piq-03-o[096-099],atla-piq-05-o[100-103],atla-piq-07-o[104-107],atla-piq-09-o[108-111],atla-piq-11-o[112-115],atla-piq-13-o[116-119],atla-pir-03-o[120-123],atla-pir-05-o[124-127],atla-pir-07-o[128-131],atla-pir-09-o[132-135],atla-pir-11-o[136-139],atla-pir-13-o[140-143],atla-pis-03-m[000-003],atla-pis-05-o[000-003],atla-pis-07-o[004-007],atla-pis-09-o[008-011],atla-pis-11-o[012-015],atla-pis-13-o[016-019],atla-pis-15-o[020-023],atla-pit-03-m[004-007],atla-pit-05-o[024-027],atla-pit-07-o[028-031],atla-pit-09-o[032-035],atla-pit-11-o[036-039],atla-pit-15-o[044-047],atla-pit-13-o[040,042]").unwrap();
486+
487+
assert_debug_snapshot!("Large expression", xs);
488+
}
489+
481490
#[test]
482491

483492
fn test_parse_osts() {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
source: src/structures.rs
3-
expression: "RangeOutput::Disjoint(vec![1, 10]).iter().collect::<Vec<_>>()"
3+
expression: "RangeOutput::Disjoint(vec![(0, 1), (1, 10)]).iter().collect::<Vec<_>>()"
44
---
55
[
66
"1",
7-
"10",
7+
"010",
88
]
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
source: src/lib.rs
3+
expression: xs
4+
---
5+
[
6+
"atla-pio-03-o048",
7+
"atla-pio-03-o049",
8+
"atla-pio-03-o050",
9+
"atla-pio-03-o051",
10+
"atla-pio-05-o052",
11+
"atla-pio-05-o053",
12+
"atla-pio-05-o054",
13+
"atla-pio-05-o055",
14+
"atla-pio-07-o056",
15+
"atla-pio-07-o057",
16+
"atla-pio-07-o058",
17+
"atla-pio-07-o059",
18+
"atla-pio-09-o060",
19+
"atla-pio-09-o061",
20+
"atla-pio-09-o062",
21+
"atla-pio-09-o063",
22+
"atla-pio-11-o064",
23+
"atla-pio-11-o065",
24+
"atla-pio-11-o066",
25+
"atla-pio-11-o067",
26+
"atla-pio-13-o068",
27+
"atla-pio-13-o069",
28+
"atla-pio-13-o070",
29+
"atla-pio-13-o071",
30+
"atla-pip-03-o072",
31+
"atla-pip-03-o073",
32+
"atla-pip-03-o074",
33+
"atla-pip-03-o075",
34+
"atla-pip-05-o076",
35+
"atla-pip-05-o077",
36+
"atla-pip-05-o078",
37+
"atla-pip-05-o079",
38+
"atla-pip-07-o080",
39+
"atla-pip-07-o081",
40+
"atla-pip-07-o082",
41+
"atla-pip-07-o083",
42+
"atla-pip-11-o088",
43+
"atla-pip-11-o089",
44+
"atla-pip-11-o090",
45+
"atla-pip-11-o091",
46+
"atla-pip-13-o092",
47+
"atla-pip-13-o093",
48+
"atla-pip-13-o094",
49+
"atla-pip-13-o095",
50+
"atla-pip-09-o085",
51+
"atla-pip-09-o087",
52+
"atla-piq-03-o096",
53+
"atla-piq-03-o097",
54+
"atla-piq-03-o098",
55+
"atla-piq-03-o099",
56+
"atla-piq-05-o100",
57+
"atla-piq-05-o101",
58+
"atla-piq-05-o102",
59+
"atla-piq-05-o103",
60+
"atla-piq-07-o104",
61+
"atla-piq-07-o105",
62+
"atla-piq-07-o106",
63+
"atla-piq-07-o107",
64+
"atla-piq-09-o108",
65+
"atla-piq-09-o109",
66+
"atla-piq-09-o110",
67+
"atla-piq-09-o111",
68+
"atla-piq-11-o112",
69+
"atla-piq-11-o113",
70+
"atla-piq-11-o114",
71+
"atla-piq-11-o115",
72+
"atla-piq-13-o116",
73+
"atla-piq-13-o117",
74+
"atla-piq-13-o118",
75+
"atla-piq-13-o119",
76+
"atla-pir-03-o120",
77+
"atla-pir-03-o121",
78+
"atla-pir-03-o122",
79+
"atla-pir-03-o123",
80+
"atla-pir-05-o124",
81+
"atla-pir-05-o125",
82+
"atla-pir-05-o126",
83+
"atla-pir-05-o127",
84+
"atla-pir-07-o128",
85+
"atla-pir-07-o129",
86+
"atla-pir-07-o130",
87+
"atla-pir-07-o131",
88+
"atla-pir-09-o132",
89+
"atla-pir-09-o133",
90+
"atla-pir-09-o134",
91+
"atla-pir-09-o135",
92+
"atla-pir-11-o136",
93+
"atla-pir-11-o137",
94+
"atla-pir-11-o138",
95+
"atla-pir-11-o139",
96+
"atla-pir-13-o140",
97+
"atla-pir-13-o141",
98+
"atla-pir-13-o142",
99+
"atla-pir-13-o143",
100+
"atla-pis-03-m000",
101+
"atla-pis-03-m001",
102+
"atla-pis-03-m002",
103+
"atla-pis-03-m003",
104+
"atla-pis-05-o000",
105+
"atla-pis-05-o001",
106+
"atla-pis-05-o002",
107+
"atla-pis-05-o003",
108+
"atla-pis-07-o004",
109+
"atla-pis-07-o005",
110+
"atla-pis-07-o006",
111+
"atla-pis-07-o007",
112+
"atla-pis-09-o008",
113+
"atla-pis-09-o009",
114+
"atla-pis-09-o010",
115+
"atla-pis-09-o011",
116+
"atla-pis-11-o012",
117+
"atla-pis-11-o013",
118+
"atla-pis-11-o014",
119+
"atla-pis-11-o015",
120+
"atla-pis-13-o016",
121+
"atla-pis-13-o017",
122+
"atla-pis-13-o018",
123+
"atla-pis-13-o019",
124+
"atla-pis-15-o020",
125+
"atla-pis-15-o021",
126+
"atla-pis-15-o022",
127+
"atla-pis-15-o023",
128+
"atla-pit-03-m004",
129+
"atla-pit-03-m005",
130+
"atla-pit-03-m006",
131+
"atla-pit-03-m007",
132+
"atla-pit-05-o024",
133+
"atla-pit-05-o025",
134+
"atla-pit-05-o026",
135+
"atla-pit-05-o027",
136+
"atla-pit-07-o028",
137+
"atla-pit-07-o029",
138+
"atla-pit-07-o030",
139+
"atla-pit-07-o031",
140+
"atla-pit-09-o032",
141+
"atla-pit-09-o033",
142+
"atla-pit-09-o034",
143+
"atla-pit-09-o035",
144+
"atla-pit-11-o036",
145+
"atla-pit-11-o037",
146+
"atla-pit-11-o038",
147+
"atla-pit-11-o039",
148+
"atla-pit-15-o044",
149+
"atla-pit-15-o045",
150+
"atla-pit-15-o046",
151+
"atla-pit-15-o047",
152+
"atla-pit-13-o040",
153+
"atla-pit-13-o042",
154+
]

src/snapshots/hostlist_parser__tests__hostlists-3.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ Ok(
4949
),
5050
Range(
5151
0,
52+
true,
5253
11,
5354
12,
5455
),
5556
Range(
5657
0,
58+
true,
5759
2,
5860
3,
5961
),
@@ -79,6 +81,7 @@ Ok(
7981
[
8082
Range(
8183
0,
84+
true,
8285
15,
8386
17,
8487
),

src/snapshots/hostlist_parser__tests__hostlists-4.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ Ok(
4949
),
5050
Range(
5151
0,
52+
true,
5253
11,
5354
12,
5455
),
5556
Range(
5657
0,
58+
true,
5759
2,
5860
3,
5961
),
@@ -79,6 +81,7 @@ Ok(
7981
[
8082
Range(
8183
0,
84+
true,
8285
15,
8386
17,
8487
),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: src/lib.rs
3+
expression: "leading_zeros().easy_parse(\"042\")"
4+
---
5+
Ok(
6+
(
7+
(
8+
1,
9+
42,
10+
),
11+
"",
12+
),
13+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: src/lib.rs
3+
expression: "leading_zeros().easy_parse(\"042\")"
4+
---
5+
Ok(
6+
(
7+
(
8+
1,
9+
42,
10+
),
11+
"",
12+
),
13+
)

src/snapshots/hostlist_parser__tests__range-2.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Ok(
1919
),
2020
Range(
2121
0,
22+
true,
2223
3,
2324
5,
2425
),

0 commit comments

Comments
 (0)