From 07dcc0cae3ef45bc60b09d39e9af7020c32907e5 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Fri, 25 Oct 2024 21:16:34 +0000 Subject: [PATCH] refactor(ast_tools): move `file_path` method to `Runner` (#6902) Centralize this common method, to move responsibility from individual derives/generators. --- tasks/ast_tools/src/codegen.rs | 46 +++++++++++++++------------ tasks/ast_tools/src/derives/mod.rs | 28 ++++++++-------- tasks/ast_tools/src/generators/mod.rs | 31 +++++++----------- tasks/ast_tools/src/passes/mod.rs | 4 +++ 4 files changed, 55 insertions(+), 54 deletions(-) diff --git a/tasks/ast_tools/src/codegen.rs b/tasks/ast_tools/src/codegen.rs index 0da21c394..43b7e5e17 100644 --- a/tasks/ast_tools/src/codegen.rs +++ b/tasks/ast_tools/src/codegen.rs @@ -7,7 +7,7 @@ use crate::{ derives::Derive, generators::Generator, log, logln, - output::RawOutput, + output::{Output, RawOutput}, passes::Pass, rust_ast::{self, AstRef}, schema::{lower_ast_types, Schema, TypeDef}, @@ -18,8 +18,8 @@ use crate::{ pub struct AstCodegen { files: Vec, passes: Vec>>, - generators: Vec>>, - derives: Vec, Context = LateCtx>>>, + generators: Vec>>, + derives: Vec, Context = LateCtx>>>, } pub struct AstCodegenResult { @@ -31,6 +31,7 @@ pub trait Runner { type Context; type Output; fn name(&self) -> &'static str; + fn file_path(&self) -> &'static str; fn run(&mut self, ctx: &Self::Context) -> Result; } @@ -123,7 +124,7 @@ impl AstCodegen { #[must_use] pub fn generate(mut self, generator: G) -> Self where - G: Generator + Runner + 'static, + G: Generator + Runner + 'static, { self.generators.push(Box::new(generator)); self @@ -132,7 +133,7 @@ impl AstCodegen { #[must_use] pub fn derive(mut self, derive: D) -> Self where - D: Derive + Runner, Context = LateCtx> + 'static, + D: Derive + Runner, Context = LateCtx> + 'static, { self.derives.push(Box::new(derive)); self @@ -167,12 +168,17 @@ impl AstCodegen { let name = runner.name(); log!("Derive {name}... "); let result = runner.run(&ctx); - if result.is_ok() { - logln!("Done!"); - } else { - logln!("Fail!"); + match result { + Ok(outputs) => { + logln!("Done!"); + let generator_path = runner.file_path(); + Ok(outputs.into_iter().map(|output| output.output(generator_path))) + } + Err(err) => { + logln!("FAILED"); + Err(err) + } } - result }) .flatten_ok(); @@ -180,12 +186,17 @@ impl AstCodegen { let name = runner.name(); log!("Generate {name}... "); let result = runner.run(&ctx); - if result.is_ok() { - logln!("Done!"); - } else { - logln!("Fail!"); + match result { + Ok(output) => { + logln!("Done!"); + let generator_path = runner.file_path(); + Ok(output.output(generator_path)) + } + Err(err) => { + logln!("FAILED"); + Err(err) + } } - result }); let outputs = derives.chain(outputs).collect::>>()?; @@ -193,8 +204,3 @@ impl AstCodegen { Ok(AstCodegenResult { outputs, schema: ctx.schema }) } } - -/// Implemented by `define_derive!` and `define_generator!` macros -pub trait CodegenBase { - fn file_path() -> &'static str; -} diff --git a/tasks/ast_tools/src/derives/mod.rs b/tasks/ast_tools/src/derives/mod.rs index 733594cf9..c9921c229 100644 --- a/tasks/ast_tools/src/derives/mod.rs +++ b/tasks/ast_tools/src/derives/mod.rs @@ -4,8 +4,8 @@ use proc_macro2::TokenStream; use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ - codegen::{CodegenBase, LateCtx}, - output::{output_path, Output, RawOutput}, + codegen::LateCtx, + output::{output_path, Output}, schema::TypeDef, Result, }; @@ -22,7 +22,7 @@ pub use content_hash::DeriveContentHash; pub use estree::DeriveESTree; pub use get_span::{DeriveGetSpan, DeriveGetSpanMut}; -pub trait Derive: CodegenBase { +pub trait Derive { // Methods defined by implementer fn trait_name() -> &'static str; @@ -67,7 +67,7 @@ pub trait Derive: CodegenBase { } } - fn output(&mut self, ctx: &LateCtx) -> Result> { + fn output(&mut self, ctx: &LateCtx) -> Result> { let trait_name = Self::trait_name(); let filename = format!("derive_{}.rs", Self::snake_name()); let output = ctx @@ -112,7 +112,7 @@ pub trait Derive: CodegenBase { ), }; - acc.push(output.output(Self::file_path())); + acc.push(output); acc }); Ok(output) @@ -123,26 +123,24 @@ macro_rules! define_derive { ($ident:ident $($lifetime:lifetime)?) => { const _: () = { use $crate::{ - codegen::{CodegenBase, LateCtx, Runner}, - output::RawOutput, + codegen::{LateCtx, Runner}, + output::Output, Result, }; - impl $($lifetime)? CodegenBase for $ident $($lifetime)? { - fn file_path() -> &'static str { - file!() - } - } - impl $($lifetime)? Runner for $ident $($lifetime)? { type Context = LateCtx; - type Output = Vec; + type Output = Vec; fn name(&self) -> &'static str { stringify!($ident) } - fn run(&mut self, ctx: &LateCtx) -> Result> { + fn file_path(&self) -> &'static str { + file!() + } + + fn run(&mut self, ctx: &LateCtx) -> Result> { self.output(ctx) } } diff --git a/tasks/ast_tools/src/generators/mod.rs b/tasks/ast_tools/src/generators/mod.rs index 6ecc593c0..5ea92d377 100644 --- a/tasks/ast_tools/src/generators/mod.rs +++ b/tasks/ast_tools/src/generators/mod.rs @@ -1,8 +1,4 @@ -use crate::{ - codegen::{CodegenBase, LateCtx}, - output::{Output, RawOutput}, - Result, -}; +use crate::{codegen::LateCtx, output::Output, Result}; mod assert_layouts; mod ast_builder; @@ -16,16 +12,15 @@ pub use ast_kind::AstKindGenerator; pub use typescript::TypescriptGenerator; pub use visit::{VisitGenerator, VisitMutGenerator}; -pub trait Generator: CodegenBase { +pub trait Generator { // Methods defined by implementer fn generate(&mut self, ctx: &LateCtx) -> Output; // Standard methods - fn output(&mut self, ctx: &LateCtx) -> Result { - let output = self.generate(ctx); - Ok(output.output(Self::file_path())) + fn output(&mut self, ctx: &LateCtx) -> Result { + Ok(self.generate(ctx)) } } @@ -33,26 +28,24 @@ macro_rules! define_generator { ($ident:ident $($lifetime:lifetime)?) => { const _: () = { use $crate::{ - codegen::{CodegenBase, LateCtx, Runner}, - output::RawOutput, + codegen::{LateCtx, Runner}, + output::Output, Result, }; - impl $($lifetime)? CodegenBase for $ident $($lifetime)? { - fn file_path() -> &'static str { - file!() - } - } - impl $($lifetime)? Runner for $ident $($lifetime)? { type Context = LateCtx; - type Output = RawOutput; + type Output = Output; fn name(&self) -> &'static str { stringify!($ident) } - fn run(&mut self, ctx: &LateCtx) -> Result { + fn file_path(&self) -> &'static str { + file!() + } + + fn run(&mut self, ctx: &LateCtx) -> Result { self.output(ctx) } } diff --git a/tasks/ast_tools/src/passes/mod.rs b/tasks/ast_tools/src/passes/mod.rs index 957e41fff..5909e989d 100644 --- a/tasks/ast_tools/src/passes/mod.rs +++ b/tasks/ast_tools/src/passes/mod.rs @@ -58,6 +58,10 @@ macro_rules! define_pass { stringify!($ident) } + fn file_path(&self) -> &'static str { + file!() + } + fn run(&mut self, ctx: &Self::Context) -> Result<()> { self.call(ctx).map(|_| ()) }