refactor(ast_tools): move file_path method to Runner (#6902)

Centralize this common method, to move responsibility from individual derives/generators.
This commit is contained in:
overlookmotel 2024-10-25 21:16:34 +00:00
parent 5f44d84744
commit 07dcc0cae3
4 changed files with 55 additions and 54 deletions

View file

@ -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<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 {
@ -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>;
}
@ -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
@ -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
@ -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::<Result<Vec<_>>>()?;
@ -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;
}

View file

@ -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<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
@ -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<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)
}
}

View file

@ -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<RawOutput> {
let output = self.generate(ctx);
Ok(output.output(Self::file_path()))
fn output(&mut self, ctx: &LateCtx) -> Result<Output> {
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<RawOutput> {
fn file_path(&self) -> &'static str {
file!()
}
fn run(&mut self, ctx: &LateCtx) -> Result<Output> {
self.output(ctx)
}
}

View file

@ -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(|_| ())
}