Skip to content

Commit

Permalink
refactor(ast_tools): move file_path method to Runner (#6902)
Browse files Browse the repository at this point in the history
Centralize this common method, to move responsibility from individual derives/generators.
  • Loading branch information
overlookmotel committed Oct 25, 2024
1 parent 5f44d84 commit 07dcc0c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 54 deletions.
46 changes: 26 additions & 20 deletions tasks/ast_tools/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -18,8 +18,8 @@ use crate::{
pub struct AstCodegen {
files: Vec<PathBuf>,
passes: Vec<Box<dyn Runner<Output = (), Context = EarlyCtx>>>,
generators: Vec<Box<dyn Runner<Output = RawOutput, Context = LateCtx>>>,
derives: Vec<Box<dyn Runner<Output = Vec<RawOutput>, Context = LateCtx>>>,
generators: Vec<Box<dyn Runner<Output = Output, Context = LateCtx>>>,
derives: Vec<Box<dyn Runner<Output = Vec<Output>, Context = LateCtx>>>,
}

pub struct AstCodegenResult {
Expand All @@ -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<Self::Output>;
}

Expand Down Expand Up @@ -123,7 +124,7 @@ impl AstCodegen {
#[must_use]
pub fn generate<G>(mut self, generator: G) -> Self
where
G: Generator + Runner<Output = RawOutput, Context = LateCtx> + 'static,
G: Generator + Runner<Output = Output, Context = LateCtx> + 'static,
{
self.generators.push(Box::new(generator));
self
Expand All @@ -132,7 +133,7 @@ impl AstCodegen {
#[must_use]
pub fn derive<D>(mut self, derive: D) -> Self
where
D: Derive + Runner<Output = Vec<RawOutput>, Context = LateCtx> + 'static,
D: Derive + Runner<Output = Vec<Output>, Context = LateCtx> + 'static,
{
self.derives.push(Box::new(derive));
self
Expand Down Expand Up @@ -167,34 +168,39 @@ 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();

let outputs = self.generators.into_iter().map(|mut runner| {
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::<Result<Vec<_>>>()?;

Ok(AstCodegenResult { outputs, schema: ctx.schema })
}
}

/// Implemented by `define_derive!` and `define_generator!` macros
pub trait CodegenBase {
fn file_path() -> &'static str;
}
28 changes: 13 additions & 15 deletions tasks/ast_tools/src/derives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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;
Expand Down Expand Up @@ -67,7 +67,7 @@ pub trait Derive: CodegenBase {
}
}

fn output(&mut self, ctx: &LateCtx) -> Result<Vec<RawOutput>> {
fn output(&mut self, ctx: &LateCtx) -> Result<Vec<Output>> {
let trait_name = Self::trait_name();
let filename = format!("derive_{}.rs", Self::snake_name());
let output = ctx
Expand Down Expand Up @@ -112,7 +112,7 @@ pub trait Derive: CodegenBase {
),
};

acc.push(output.output(Self::file_path()));
acc.push(output);
acc
});
Ok(output)
Expand All @@ -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<RawOutput>;
type Output = Vec<Output>;

fn name(&self) -> &'static str {
stringify!($ident)
}

fn run(&mut self, ctx: &LateCtx) -> Result<Vec<RawOutput>> {
fn file_path(&self) -> &'static str {
file!()
}

fn run(&mut self, ctx: &LateCtx) -> Result<Vec<Output>> {
self.output(ctx)
}
}
Expand Down
31 changes: 12 additions & 19 deletions tasks/ast_tools/src/generators/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,43 +12,40 @@ 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<RawOutput> {
let output = self.generate(ctx);
Ok(output.output(Self::file_path()))
fn output(&mut self, ctx: &LateCtx) -> Result<Output> {
Ok(self.generate(ctx))
}
}

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<RawOutput> {
fn file_path(&self) -> &'static str {
file!()
}

fn run(&mut self, ctx: &LateCtx) -> Result<Output> {
self.output(ctx)
}
}
Expand Down
4 changes: 4 additions & 0 deletions tasks/ast_tools/src/passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|_| ())
}
Expand Down

0 comments on commit 07dcc0c

Please sign in to comment.