Skip to content

Commit 136029e

Browse files
committed
refactor(ast_tools): give GeneratorOutput and SideEffect named fields (#6685)
Pure refactor. Named fields are clearer.
1 parent 53049fe commit 136029e

File tree

6 files changed

+33
-22
lines changed

6 files changed

+33
-22
lines changed

tasks/ast_tools/src/codegen.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,37 @@ pub struct AstCodegenResult {
2929
pub outputs: Vec<SideEffect>,
3030
}
3131

32-
pub struct SideEffect(/* path */ pub PathBuf, /* output */ pub Vec<u8>);
32+
pub struct SideEffect {
33+
pub path: PathBuf,
34+
pub content: Vec<u8>,
35+
}
3336

3437
impl SideEffect {
3538
/// Apply the side-effect
3639
pub fn apply(self) -> std::io::Result<()> {
37-
let Self(path, data) = self;
40+
let Self { path, content } = self;
3841
let path = path.into_os_string();
3942
let path = path.to_str().unwrap();
40-
write_all_to(&data, path)?;
43+
write_all_to(&content, path)?;
4144
Ok(())
4245
}
4346

4447
pub fn path(&self) -> String {
45-
let Self(path, _) = self;
46-
let path = path.to_string_lossy();
48+
let path = self.path.to_string_lossy();
4749
path.replace('\\', "/")
4850
}
4951
}
5052

5153
impl From<(PathBuf, TokenStream)> for SideEffect {
5254
fn from((path, stream): (PathBuf, TokenStream)) -> Self {
5355
let content = pretty_print(&stream);
54-
Self(path, content.as_bytes().into())
56+
Self { path, content: content.into() }
5557
}
5658
}
5759

5860
impl From<GeneratorOutput> for SideEffect {
5961
fn from(output: GeneratorOutput) -> Self {
60-
Self::from((output.0, output.1))
62+
Self::from((output.path, output.tokens))
6163
}
6264
}
6365

tasks/ast_tools/src/generators/assert_layouts.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ impl Generator for AssertLayouts {
2828

2929
let header = generated_header!();
3030

31-
GeneratorOutput(
32-
output(crate::AST_CRATE, "assert_layouts.rs"),
33-
quote! {
31+
GeneratorOutput {
32+
path: output(crate::AST_CRATE, "assert_layouts.rs"),
33+
tokens: quote! {
3434
#header
3535

3636
use std::mem::{align_of, offset_of, size_of};
@@ -55,7 +55,7 @@ impl Generator for AssertLayouts {
5555
#[cfg(not(any(target_pointer_width = "64", target_pointer_width = "32")))]
5656
const _: () = panic!("Platforms with pointer width other than 64 or 32 bit are not supported");
5757
},
58-
)
58+
}
5959
}
6060
}
6161

tasks/ast_tools/src/generators/ast_builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ impl Generator for AstBuilderGenerator {
3434

3535
let header = generated_header!();
3636

37-
GeneratorOutput(
38-
output(crate::AST_CRATE, "ast_builder.rs"),
39-
quote! {
37+
GeneratorOutput {
38+
path: output(crate::AST_CRATE, "ast_builder.rs"),
39+
tokens: quote! {
4040
#header
4141

4242
#![allow(
@@ -64,7 +64,7 @@ impl Generator for AstBuilderGenerator {
6464
#(#fns)*
6565
}
6666
},
67-
)
67+
}
6868
}
6969
}
7070

tasks/ast_tools/src/generators/ast_kind.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ impl Generator for AstKindGenerator {
175175

176176
let header = generated_header!();
177177

178-
GeneratorOutput(
179-
output(crate::AST_CRATE, "ast_kind.rs"),
180-
quote! {
178+
GeneratorOutput {
179+
path: output(crate::AST_CRATE, "ast_kind.rs"),
180+
tokens: quote! {
181181
#header
182182

183183
use oxc_span::{GetSpan, Span};
@@ -214,6 +214,6 @@ impl Generator for AstKindGenerator {
214214
#(#as_ast_kind_impls)*
215215
}
216216
},
217-
)
217+
}
218218
}
219219
}

tasks/ast_tools/src/generators/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ pub trait Generator {
2727
}
2828

2929
#[derive(Debug, Clone)]
30-
pub struct GeneratorOutput(/* output path */ pub PathBuf, pub TokenStream);
30+
pub struct GeneratorOutput {
31+
pub path: PathBuf,
32+
pub tokens: TokenStream,
33+
}
3134

3235
macro_rules! define_generator {
3336
($vis:vis struct $ident:ident $($lifetime:lifetime)? $($rest:tt)*) => {

tasks/ast_tools/src/generators/visit.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@ define_generator! {
2828

2929
impl Generator for VisitGenerator {
3030
fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
31-
GeneratorOutput(output(crate::AST_CRATE, "visit.rs"), generate_visit::<false>(ctx))
31+
GeneratorOutput {
32+
path: output(crate::AST_CRATE, "visit.rs"),
33+
tokens: generate_visit::<false>(ctx),
34+
}
3235
}
3336
}
3437

3538
impl Generator for VisitMutGenerator {
3639
fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
37-
GeneratorOutput(output(crate::AST_CRATE, "visit_mut.rs"), generate_visit::<true>(ctx))
40+
GeneratorOutput {
41+
path: output(crate::AST_CRATE, "visit_mut.rs"),
42+
tokens: generate_visit::<true>(ctx),
43+
}
3844
}
3945
}
4046

0 commit comments

Comments
 (0)