Skip to content

Commit 7e3415f

Browse files
committed
lite refactoring
1 parent 17c3e56 commit 7e3415f

File tree

2 files changed

+102
-100
lines changed

2 files changed

+102
-100
lines changed

rust/rustell/src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ pub enum Expr<'src> {
1313

1414
#[derive(Eq, PartialEq, Debug, Clone)]
1515
pub enum UseExpr<'src> {
16-
Path {
17-
ident: &'src str,
16+
Item {
17+
module: &'src str,
1818
rename: Option<&'src str>,
1919
nested: Option<Box<UseExpr<'src>>>,
2020
},
21-
Group(Vec<UseExpr<'src>>),
21+
Many(Vec<UseExpr<'src>>),
2222
Glob,
2323
}
2424

@@ -54,7 +54,7 @@ fn use_parser<'src>() -> impl Parser<
5454
> {
5555
recursive(|use_parser| {
5656
let ident = || text::ascii::ident().padded();
57-
let path = ident()
57+
let item = ident()
5858
.then(
5959
lexeme("as").ignore_then(ident()).or_not(),
6060
)
@@ -63,24 +63,24 @@ fn use_parser<'src>() -> impl Parser<
6363
.ignore_then(use_parser.clone())
6464
.or_not(),
6565
)
66-
.map(|((ident, rename), nested)| {
67-
UseExpr::Path {
68-
ident,
66+
.map(|((module, rename), nested)| {
67+
UseExpr::Item {
68+
module,
6969
rename,
7070
nested: nested.map(Box::new),
7171
}
7272
});
7373

74-
let group = use_parser
74+
let many = use_parser
7575
.separated_by(lexeme(','))
7676
.allow_trailing()
7777
.collect::<Vec<_>>()
7878
.delimited_by(lexeme('{'), lexeme('}'))
79-
.map(UseExpr::Group);
79+
.map(UseExpr::Many);
8080

8181
let glob = lexeme("*").map(|_| UseExpr::Glob);
8282

83-
path.or(group).or(glob)
83+
item.or(many).or(glob)
8484
})
8585
}
8686

rust/rustell/tests/integration.rs

Lines changed: 92 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use rustell::*;
44
fn test_parser() {
55
let src = "use std::io::Read;";
66
let lhs = parser().parse(src).into_result().unwrap();
7-
let rhs = vec![Expr::Use(UseExpr::Path {
8-
ident: "std",
7+
let rhs = vec![Expr::Use(UseExpr::Item {
8+
module: "std",
99
rename: None,
10-
nested: Some(Box::new(UseExpr::Path {
11-
ident: "io",
10+
nested: Some(Box::new(UseExpr::Item {
11+
module: "io",
1212
rename: None,
13-
nested: Some(Box::new(UseExpr::Path {
14-
ident: "Read",
13+
nested: Some(Box::new(UseExpr::Item {
14+
module: "Read",
1515
rename: None,
1616
nested: None,
1717
})),
@@ -21,27 +21,27 @@ fn test_parser() {
2121
}
2222

2323
#[test]
24-
fn test_parser_group() {
24+
fn test_parser_many() {
2525
let src = "use std::{io::Read, fs::File};";
2626
let lhs = parser().parse(src).into_result().unwrap();
27-
let rhs = vec![Expr::Use(UseExpr::Path {
28-
ident: "std",
27+
let rhs = vec![Expr::Use(UseExpr::Item {
28+
module: "std",
2929
rename: None,
30-
nested: Some(Box::new(UseExpr::Group(vec![
31-
UseExpr::Path {
32-
ident: "io",
30+
nested: Some(Box::new(UseExpr::Many(vec![
31+
UseExpr::Item {
32+
module: "io",
3333
rename: None,
34-
nested: Some(Box::new(UseExpr::Path {
35-
ident: "Read",
34+
nested: Some(Box::new(UseExpr::Item {
35+
module: "Read",
3636
rename: None,
3737
nested: None,
3838
})),
3939
},
40-
UseExpr::Path {
41-
ident: "fs",
40+
UseExpr::Item {
41+
module: "fs",
4242
rename: None,
43-
nested: Some(Box::new(UseExpr::Path {
44-
ident: "File",
43+
nested: Some(Box::new(UseExpr::Item {
44+
module: "File",
4545
rename: None,
4646
nested: None,
4747
})),
@@ -55,11 +55,11 @@ fn test_parser_group() {
5555
fn test_parser_glob() {
5656
let src = "use std::io::*;";
5757
let lhs = parser().parse(src).into_result().unwrap();
58-
let rhs = vec![Expr::Use(UseExpr::Path {
59-
ident: "std",
58+
let rhs = vec![Expr::Use(UseExpr::Item {
59+
module: "std",
6060
rename: None,
61-
nested: Some(Box::new(UseExpr::Path {
62-
ident: "io",
61+
nested: Some(Box::new(UseExpr::Item {
62+
module: "io",
6363
rename: None,
6464
nested: Some(Box::new(UseExpr::Glob)),
6565
})),
@@ -71,14 +71,14 @@ fn test_parser_glob() {
7171
fn test_parser_rename() {
7272
let src = "use std::io::Read as Readable;";
7373
let lhs = parser().parse(src).into_result().unwrap();
74-
let rhs = vec![Expr::Use(UseExpr::Path {
75-
ident: "std",
74+
let rhs = vec![Expr::Use(UseExpr::Item {
75+
module: "std",
7676
rename: None,
77-
nested: Some(Box::new(UseExpr::Path {
78-
ident: "io",
77+
nested: Some(Box::new(UseExpr::Item {
78+
module: "io",
7979
rename: None,
80-
nested: Some(Box::new(UseExpr::Path {
81-
ident: "Read",
80+
nested: Some(Box::new(UseExpr::Item {
81+
module: "Read",
8282
rename: Some("Readable"),
8383
nested: None,
8484
})),
@@ -91,21 +91,21 @@ fn test_parser_rename() {
9191
fn test_parser_complex() {
9292
let src = "use std::{io::Read as Readable, fs::*};";
9393
let lhs = parser().parse(src).into_result().unwrap();
94-
let rhs = vec![Expr::Use(UseExpr::Path {
95-
ident: "std",
94+
let rhs = vec![Expr::Use(UseExpr::Item {
95+
module: "std",
9696
rename: None,
97-
nested: Some(Box::new(UseExpr::Group(vec![
98-
UseExpr::Path {
99-
ident: "io",
97+
nested: Some(Box::new(UseExpr::Many(vec![
98+
UseExpr::Item {
99+
module: "io",
100100
rename: None,
101-
nested: Some(Box::new(UseExpr::Path {
102-
ident: "Read",
101+
nested: Some(Box::new(UseExpr::Item {
102+
module: "Read",
103103
rename: Some("Readable"),
104104
nested: None,
105105
})),
106106
},
107-
UseExpr::Path {
108-
ident: "fs",
107+
UseExpr::Item {
108+
module: "fs",
109109
rename: None,
110110
nested: Some(Box::new(UseExpr::Glob)),
111111
},
@@ -118,14 +118,14 @@ fn test_parser_complex() {
118118
fn test_parser_crate() {
119119
let src = "use crate::module::Type;";
120120
let lhs = parser().parse(src).into_result().unwrap();
121-
let rhs = vec![Expr::Use(UseExpr::Path {
122-
ident: "crate",
121+
let rhs = vec![Expr::Use(UseExpr::Item {
122+
module: "crate",
123123
rename: None,
124-
nested: Some(Box::new(UseExpr::Path {
125-
ident: "module",
124+
nested: Some(Box::new(UseExpr::Item {
125+
module: "module",
126126
rename: None,
127-
nested: Some(Box::new(UseExpr::Path {
128-
ident: "Type",
127+
nested: Some(Box::new(UseExpr::Item {
128+
module: "Type",
129129
rename: None,
130130
nested: None,
131131
})),
@@ -149,14 +149,14 @@ fn test_parser_other_then_use() {
149149
println!("Hello");
150150
}"#,
151151
),
152-
Expr::Use(UseExpr::Path {
153-
ident: "crate",
152+
Expr::Use(UseExpr::Item {
153+
module: "crate",
154154
rename: None,
155-
nested: Some(Box::new(UseExpr::Path {
156-
ident: "module",
155+
nested: Some(Box::new(UseExpr::Item {
156+
module: "module",
157157
rename: None,
158-
nested: Some(Box::new(UseExpr::Path {
159-
ident: "Type",
158+
nested: Some(Box::new(UseExpr::Item {
159+
module: "Type",
160160
rename: None,
161161
nested: None,
162162
})),
@@ -174,20 +174,20 @@ fn test_parser_multiple() {
174174
"#;
175175
let lhs = parser().parse(src).into_result().unwrap();
176176
let rhs = vec![
177-
Expr::Use(UseExpr::Path {
178-
ident: "std",
177+
Expr::Use(UseExpr::Item {
178+
module: "std",
179179
rename: None,
180-
nested: Some(Box::new(UseExpr::Path {
181-
ident: "io",
180+
nested: Some(Box::new(UseExpr::Item {
181+
module: "io",
182182
rename: None,
183183
nested: None,
184184
})),
185185
}),
186-
Expr::Use(UseExpr::Path {
187-
ident: "std",
186+
Expr::Use(UseExpr::Item {
187+
module: "std",
188188
rename: None,
189-
nested: Some(Box::new(UseExpr::Path {
190-
ident: "fs",
189+
nested: Some(Box::new(UseExpr::Item {
190+
module: "fs",
191191
rename: None,
192192
nested: None,
193193
})),
@@ -207,11 +207,11 @@ fn test_parser_multiple_with_other() {
207207
"#;
208208
let lhs = parser().parse(src).into_result().unwrap();
209209
let rhs = vec![
210-
Expr::Use(UseExpr::Path {
211-
ident: "std",
210+
Expr::Use(UseExpr::Item {
211+
module: "std",
212212
rename: None,
213-
nested: Some(Box::new(UseExpr::Path {
214-
ident: "io",
213+
nested: Some(Box::new(UseExpr::Item {
214+
module: "io",
215215
rename: None,
216216
nested: None,
217217
})),
@@ -221,11 +221,11 @@ fn test_parser_multiple_with_other() {
221221
println!("Hello");
222222
}"#,
223223
),
224-
Expr::Use(UseExpr::Path {
225-
ident: "std",
224+
Expr::Use(UseExpr::Item {
225+
module: "std",
226226
rename: None,
227-
nested: Some(Box::new(UseExpr::Path {
228-
ident: "fs",
227+
nested: Some(Box::new(UseExpr::Item {
228+
module: "fs",
229229
rename: None,
230230
nested: None,
231231
})),
@@ -249,41 +249,43 @@ fn test_parser_mixed_all_cases() {
249249
"#;
250250
let lhs = parser().parse(src).into_result().unwrap();
251251
let rhs = vec![
252-
Expr::Use(UseExpr::Path {
253-
ident: "std",
252+
Expr::Use(UseExpr::Item {
253+
module: "std",
254254
rename: None,
255-
nested: Some(Box::new(UseExpr::Group(vec![
256-
UseExpr::Path {
257-
ident: "io",
255+
nested: Some(Box::new(UseExpr::Many(vec![
256+
UseExpr::Item {
257+
module: "io",
258258
rename: None,
259-
nested: Some(Box::new(UseExpr::Group(vec![
260-
UseExpr::Path {
261-
ident: "self",
262-
rename: None,
263-
nested: None,
264-
},
265-
UseExpr::Path {
266-
ident: "Read",
267-
rename: Some("R"),
268-
nested: None,
269-
},
270-
]))),
259+
nested: Some(Box::new(UseExpr::Many(
260+
vec![
261+
UseExpr::Item {
262+
module: "self",
263+
rename: None,
264+
nested: None,
265+
},
266+
UseExpr::Item {
267+
module: "Read",
268+
rename: Some("R"),
269+
nested: None,
270+
},
271+
],
272+
))),
271273
},
272-
UseExpr::Path {
273-
ident: "fs",
274+
UseExpr::Item {
275+
module: "fs",
274276
rename: None,
275277
nested: Some(Box::new(UseExpr::Glob)),
276278
},
277279
]))),
278280
}),
279-
Expr::Use(UseExpr::Path {
280-
ident: "crate",
281+
Expr::Use(UseExpr::Item {
282+
module: "crate",
281283
rename: None,
282-
nested: Some(Box::new(UseExpr::Path {
283-
ident: "module",
284+
nested: Some(Box::new(UseExpr::Item {
285+
module: "module",
284286
rename: None,
285-
nested: Some(Box::new(UseExpr::Path {
286-
ident: "Type",
287+
nested: Some(Box::new(UseExpr::Item {
288+
module: "Type",
287289
rename: Some("T"),
288290
nested: None,
289291
})),

0 commit comments

Comments
 (0)