Skip to content

Commit 305a873

Browse files
committed
encode-decode roudtrip rustell ast tests
1 parent 401b52e commit 305a873

File tree

3 files changed

+70
-46
lines changed

3 files changed

+70
-46
lines changed

rust/rustell/src/encode.rs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,58 @@ use std::iter::{IntoIterator, once};
33
use std::vec::IntoIter;
44

55
pub fn expr<'a>(
6-
ast: Vec<Expr<'a>>,
7-
) -> impl Iterator<Item = &'a str> {
8-
ast.into_iter().flat_map(expr_one)
6+
ast: &'a [Expr<'a>],
7+
) -> impl Iterator<Item = &'a str> + 'a {
8+
ast.iter().flat_map(expr_one)
99
}
1010

11-
fn expr_one<'a>(ast: Expr<'a>) -> IntoIter<&'a str> {
11+
fn expr_one<'a>(ast: &'a Expr<'a>) -> IntoIter<&'a str> {
1212
match ast {
1313
Expr::Use(x) => expr_use(true, x)
14-
.chain(vec![";"].into_iter())
15-
.collect::<Vec<&'a str>>()
14+
.chain(once(";"))
15+
.collect::<Vec<_>>()
1616
.into_iter(),
17-
Expr::Other(x) => vec![x].into_iter(),
17+
Expr::Other(x) => vec![*x].into_iter(),
1818
}
1919
}
2020

2121
fn expr_use<'a>(
2222
top: bool,
23-
ast: ExprUse<'a>,
23+
ast: &'a ExprUse<'a>,
2424
) -> IntoIter<&'a str> {
2525
let x0 = if top { vec!["use"] } else { vec![] };
26+
2627
let x1 = match ast {
2728
ExprUse::Item {
2829
module,
2930
rename,
3031
nested,
31-
} => expr_use_item(module, rename, nested),
32+
} => expr_use_item(
33+
module,
34+
*rename,
35+
nested.as_deref(),
36+
),
3237
ExprUse::Many(xs) => expr_use_many(xs),
3338
ExprUse::Glob => vec!["*"].into_iter(),
3439
};
35-
x0.into_iter()
36-
.chain(x1.into_iter())
37-
.collect::<Vec<_>>()
38-
.into_iter()
40+
41+
x0.into_iter().chain(x1).collect::<Vec<_>>().into_iter()
3942
}
4043

4144
fn expr_use_item<'a>(
4245
module: &'a str,
4346
rename: Option<&'a str>,
44-
nested: Option<Box<ExprUse<'a>>>,
47+
nested: Option<&'a ExprUse<'a>>,
4548
) -> IntoIter<&'a str> {
4649
let module_iter = once(module);
4750

4851
let rename_iter = rename
49-
.map(|x| once("as").chain(once(x)))
52+
.map(|x| once(" as ").chain(once(x)))
5053
.into_iter()
5154
.flatten();
5255

5356
let nested_iter = nested
54-
.map(|x| once("::").chain(expr_use(false, *x)))
57+
.map(|x| once("::").chain(expr_use(false, x)))
5558
.into_iter()
5659
.flatten();
5760

@@ -63,10 +66,23 @@ fn expr_use_item<'a>(
6366
}
6467

6568
fn expr_use_many<'a>(
66-
xs: Vec<ExprUse<'a>>,
69+
xs: &'a [ExprUse<'a>],
6770
) -> IntoIter<&'a str> {
68-
xs.into_iter()
69-
.flat_map(|x| expr_use(false, x))
71+
let interspersed =
72+
xs.iter().enumerate().flat_map(|(i, x)| {
73+
let iter = expr_use(false, x);
74+
if i + 1 < xs.len() {
75+
iter.chain(once(","))
76+
.collect::<Vec<_>>()
77+
.into_iter()
78+
} else {
79+
iter
80+
}
81+
});
82+
83+
once("{")
84+
.chain(interspersed)
85+
.chain(once("}"))
7086
.collect::<Vec<_>>()
7187
.into_iter()
7288
}

rust/rustell/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn main() {
1010
.expect("Failed to read stdin");
1111
match decode::expr().parse(src.trim()).into_result() {
1212
Ok(ast) => {
13-
encode::expr(ast).for_each(|x| print!("{}", x))
13+
encode::expr(&ast).for_each(|x| print!("{}", x))
1414
}
1515
Err(errs) => {
1616
errs.into_iter().for_each(|e| println!("{e:?}"))

rust/rustell/tests/integration.rs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use rustell::*;
44

55
#[test]
66
fn test_parser() {
7-
let src = "use std::io::Read;";
8-
let slp = sloppy(src);
9-
let ast = vec![Expr::Use(ExprUse::Item {
7+
let lhs = "use std::io::Read;";
8+
let rhs = vec![Expr::Use(ExprUse::Item {
109
module: "std",
1110
rename: None,
1211
nested: Some(Box::new(ExprUse::Item {
@@ -19,9 +18,9 @@ fn test_parser() {
1918
})),
2019
})),
2120
})];
22-
assert_eq!(parse(src), ast);
23-
assert_eq!(parse(&slp), ast);
24-
assert_eq!(parse(&encode(ast.clone())), ast)
21+
assert_eq!(decode(lhs), rhs);
22+
assert_eq!(decode(&sloppy(lhs)), rhs);
23+
assert_eq!(decode(&encode(&rhs)), rhs)
2524
}
2625

2726
#[test]
@@ -51,8 +50,9 @@ fn test_parser_many() {
5150
},
5251
]))),
5352
})];
54-
assert_eq!(parse(lhs), rhs);
55-
assert_eq!(parse(&sloppy(lhs)), rhs)
53+
assert_eq!(decode(lhs), rhs);
54+
assert_eq!(decode(&sloppy(lhs)), rhs);
55+
assert_eq!(decode(&encode(&rhs)), rhs)
5656
}
5757

5858
#[test]
@@ -67,8 +67,9 @@ fn test_parser_glob() {
6767
nested: Some(Box::new(ExprUse::Glob)),
6868
})),
6969
})];
70-
assert_eq!(parse(lhs), rhs);
71-
assert_eq!(parse(&sloppy(lhs)), rhs)
70+
assert_eq!(decode(lhs), rhs);
71+
assert_eq!(decode(&sloppy(lhs)), rhs);
72+
assert_eq!(decode(&encode(&rhs)), rhs)
7273
}
7374

7475
#[test]
@@ -87,8 +88,9 @@ fn test_parser_rename() {
8788
})),
8889
})),
8990
})];
90-
assert_eq!(parse(lhs), rhs);
91-
assert_eq!(parse(&sloppy(lhs)), rhs)
91+
assert_eq!(decode(lhs), rhs);
92+
assert_eq!(decode(&sloppy(lhs)), rhs);
93+
assert_eq!(decode(&encode(&rhs)), rhs)
9294
}
9395

9496
#[test]
@@ -114,8 +116,9 @@ fn test_parser_complex() {
114116
},
115117
]))),
116118
})];
117-
assert_eq!(parse(lhs), rhs);
118-
assert_eq!(parse(&sloppy(lhs)), rhs)
119+
assert_eq!(decode(lhs), rhs);
120+
assert_eq!(decode(&sloppy(lhs)), rhs);
121+
assert_eq!(decode(&encode(&rhs)), rhs)
119122
}
120123

121124
#[test]
@@ -134,8 +137,9 @@ fn test_parser_crate() {
134137
})),
135138
})),
136139
})];
137-
assert_eq!(parse(lhs), rhs);
138-
assert_eq!(parse(&sloppy(lhs)), rhs)
140+
assert_eq!(decode(lhs), rhs);
141+
assert_eq!(decode(&sloppy(lhs)), rhs);
142+
assert_eq!(decode(&encode(&rhs)), rhs)
139143
}
140144

141145
#[test]
@@ -167,8 +171,9 @@ fn test_parser_other_then_use() {
167171
})),
168172
}),
169173
];
170-
assert_eq!(parse(lhs), rhs);
171-
assert_eq!(parse(&sloppy(lhs)), rhs)
174+
assert_eq!(decode(lhs), rhs);
175+
assert_eq!(decode(&sloppy(lhs)), rhs);
176+
assert_eq!(decode(&encode(&rhs)), rhs)
172177
}
173178

174179
#[test]
@@ -200,8 +205,9 @@ fn test_parser_multiple() {
200205
}),
201206
Expr::Other("\n "),
202207
];
203-
assert_eq!(parse(lhs), rhs);
204-
assert_eq!(parse(&sloppy(lhs)), rhs)
208+
assert_eq!(decode(lhs), rhs);
209+
assert_eq!(decode(&sloppy(lhs)), rhs);
210+
assert_eq!(decode(&encode(&rhs)), rhs)
205211
}
206212

207213
#[test]
@@ -242,8 +248,9 @@ fn test_parser_multiple_with_other() {
242248
}),
243249
Expr::Other("\n "),
244250
];
245-
assert_eq!(parse(lhs), rhs);
246-
assert_eq!(parse(&sloppy(lhs)), rhs)
251+
assert_eq!(decode(lhs), rhs);
252+
assert_eq!(decode(&sloppy(lhs)), rhs);
253+
assert_eq!(decode(&encode(&rhs)), rhs)
247254
}
248255

249256
#[test]
@@ -313,18 +320,19 @@ fn test_parser_mixed_all_cases() {
313320
"#,
314321
),
315322
];
316-
assert_eq!(parse(lhs), rhs);
317-
assert_eq!(parse(&sloppy(lhs)), rhs)
323+
assert_eq!(decode(lhs), rhs);
324+
assert_eq!(decode(&sloppy(lhs)), rhs);
325+
assert_eq!(decode(&encode(&rhs)), rhs)
318326
}
319327

320328
fn sloppy(src: &str) -> String {
321329
src.replace(";", "")
322330
}
323331

324-
fn parse(src: &str) -> Vec<Expr> {
332+
fn decode(src: &str) -> Vec<Expr> {
325333
decode::expr().parse(src).into_result().unwrap()
326334
}
327335

328-
fn encode(ast: Vec<Expr>) -> String {
336+
fn encode(ast: &[Expr]) -> String {
329337
encode::expr(ast).collect()
330338
}

0 commit comments

Comments
 (0)