Skip to content

Commit

Permalink
refactor: cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Sep 2, 2024
1 parent d36d431 commit 241b549
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
37 changes: 17 additions & 20 deletions tasks/ast_tools/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,36 @@ pub struct AstCodegenResult {
pub outputs: Vec<SideEffect>,
}

pub enum SideEffect {
/// File write side-effect
Write(/* path */ PathBuf, /* output */ Vec<u8>),
}
pub struct SideEffect(/* path */ pub PathBuf, /* output */ pub Vec<u8>);

impl SideEffect {
/// Apply the side-effect
pub fn apply(self) -> std::io::Result<()> {
match self {
Self::Write(path, data) => {
let path = path.into_os_string();
let path = path.to_str().unwrap();
write_all_to(&data, path)?;
Ok(())
}
}
let Self(path, data) = self;
let path = path.into_os_string();
let path = path.to_str().unwrap();
write_all_to(&data, path)?;
Ok(())
}

#[allow(clippy::unnecessary_wraps)]
pub fn path(&self) -> Option<String> {
match self {
Self::Write(path, _) => {
let path = path.to_string_lossy();
Some(path.replace('\\', "/"))
}
}
let Self(path, _) = self;
let path = path.to_string_lossy();
Some(path.replace('\\', "/"))
}
}

impl From<(PathBuf, TokenStream)> for SideEffect {
fn from((path, stream): (PathBuf, TokenStream)) -> Self {
let content = pretty_print(&stream);
Self::Write(path, content.as_bytes().into())
Self(path, content.as_bytes().into())
}
}

impl From<GeneratorOutput> for SideEffect {
fn from(output: GeneratorOutput) -> Self {
Self::from((output.0, output.1))
}
}

Expand Down Expand Up @@ -207,7 +204,7 @@ impl AstCodegen {
.generators
.into_iter()
.map(|mut runner| runner.run(&ctx))
.map_ok(|output| SideEffect::from((output.0, output.1)))
.map_ok(SideEffect::from)
.chain(derives)
.collect::<Result<Vec<_>>>()?;

Expand Down
2 changes: 0 additions & 2 deletions tasks/ast_tools/src/derives/clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ fn impl_clone_in(
) -> TokenStream {
if has_lifetime {
quote! {
///@@line_break
impl <'old_alloc, 'new_alloc> CloneIn<'new_alloc> for #ty_ident<'old_alloc> {
type Cloned = #ty_ident<'new_alloc>;
fn clone_in(&self, #alloc_ident: &'new_alloc Allocator) -> Self::Cloned {
Expand All @@ -101,7 +100,6 @@ fn impl_clone_in(
}
} else {
quote! {
///@@line_break
impl <'alloc> CloneIn<'alloc> for #ty_ident {
type Cloned = #ty_ident;
fn clone_in(&self, #alloc_ident: &'alloc Allocator) -> Self::Cloned {
Expand Down
2 changes: 0 additions & 2 deletions tasks/ast_tools/src/derives/get_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ fn derive_enum(
});

quote! {
///@@line_break
impl #generics #trait_name for #target_type {
fn #method_name(#self_type) -> #result_type {
match self {
Expand Down Expand Up @@ -129,7 +128,6 @@ fn derive_struct(
};

quote! {
///@@line_break
impl #generics #trait_name for #target_type {
#[inline]
fn #method_name(#self_type) -> #result_type {
Expand Down
2 changes: 2 additions & 0 deletions tasks/ast_tools/src/derives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct DeriveOutput(pub Vec<(PathBuf, TokenStream)>);
macro_rules! define_derive {
($vis:vis struct $ident:ident $($lifetime:lifetime)? $($rest:tt)*) => {
$vis struct $ident $($lifetime)? $($rest)*

impl $($lifetime)? $crate::derives::DeriveTemplate for $ident $($lifetime)? {
fn template(module_paths: Vec<&str>, impls: TokenStream) -> TokenStream {
use itertools::Itertools;
Expand Down Expand Up @@ -61,6 +62,7 @@ macro_rules! define_derive {
}
}
}

impl $($lifetime)? $crate::codegen::Runner for $ident $($lifetime)? {
type Context = $crate::codegen::LateCtx;
type Output = $crate::derives::DeriveOutput;
Expand Down

0 comments on commit 241b549

Please sign in to comment.