mirror of
https://github.com/Noratrieb/cargo-minimize.git
synced 2026-01-14 16:35:01 +01:00
restructure passes
This commit is contained in:
parent
3b631198e0
commit
527e3ca657
6 changed files with 27 additions and 17 deletions
|
|
@ -5,8 +5,7 @@ use std::{path::PathBuf, str::FromStr};
|
||||||
|
|
||||||
mod build;
|
mod build;
|
||||||
mod dylib_flag;
|
mod dylib_flag;
|
||||||
mod everybody_loops;
|
mod passes;
|
||||||
mod privatize;
|
|
||||||
mod processor;
|
mod processor;
|
||||||
|
|
||||||
#[cfg(this_pulls_in_cargo_which_is_a_big_dep_i_dont_like_it)]
|
#[cfg(this_pulls_in_cargo_which_is_a_big_dep_i_dont_like_it)]
|
||||||
|
|
@ -18,7 +17,7 @@ use processor::Minimizer;
|
||||||
use tracing::Level;
|
use tracing::Level;
|
||||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
|
||||||
|
|
||||||
use crate::processor::Processor;
|
use crate::processor::Pass;
|
||||||
|
|
||||||
// Export so that the user doesn't have to add clap themselves.
|
// Export so that the user doesn't have to add clap themselves.
|
||||||
pub use clap::Parser;
|
pub use clap::Parser;
|
||||||
|
|
@ -118,8 +117,8 @@ pub fn minimize(options: Options) -> Result<()> {
|
||||||
let mut minimizer = Minimizer::new_glob_dir(options, build)?;
|
let mut minimizer = Minimizer::new_glob_dir(options, build)?;
|
||||||
|
|
||||||
minimizer.run_passes([
|
minimizer.run_passes([
|
||||||
Box::<privatize::Privatize>::default() as Box<dyn Processor>,
|
passes::Privatize::default().boxed(),
|
||||||
Box::<everybody_loops::EverybodyLoops>::default() as Box<dyn Processor>,
|
passes::EverybodyLoops::default().boxed(),
|
||||||
])?;
|
])?;
|
||||||
|
|
||||||
minimizer.delete_dead_code().context("deleting dead code")?;
|
minimizer.delete_dead_code().context("deleting dead code")?;
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use quote::ToTokens;
|
use quote::ToTokens;
|
||||||
use syn::{parse_quote, visit_mut::VisitMut};
|
use syn::{parse_quote, visit_mut::VisitMut};
|
||||||
|
|
||||||
use crate::processor::{tracking, PassController, ProcessState, Processor, SourceFile};
|
use crate::processor::{tracking, PassController, ProcessState, Pass, SourceFile};
|
||||||
|
|
||||||
struct Visitor<'a> {
|
struct Visitor<'a> {
|
||||||
current_path: Vec<String>,
|
current_path: Vec<String>,
|
||||||
|
|
@ -42,7 +42,7 @@ impl VisitMut for Visitor<'_> {
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct EverybodyLoops;
|
pub struct EverybodyLoops;
|
||||||
|
|
||||||
impl Processor for EverybodyLoops {
|
impl Pass for EverybodyLoops {
|
||||||
fn process_file(
|
fn process_file(
|
||||||
&mut self,
|
&mut self,
|
||||||
krate: &mut syn::File,
|
krate: &mut syn::File,
|
||||||
4
src/passes/mod.rs
Normal file
4
src/passes/mod.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
mod everybody_loops;
|
||||||
|
mod privatize;
|
||||||
|
|
||||||
|
pub use self::{everybody_loops::EverybodyLoops, privatize::Privatize};
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use quote::ToTokens;
|
use quote::ToTokens;
|
||||||
use syn::{parse_quote, visit_mut::VisitMut, Visibility};
|
use syn::{parse_quote, visit_mut::VisitMut, Visibility};
|
||||||
|
|
||||||
use crate::processor::{tracking, PassController, ProcessState, Processor, SourceFile};
|
use crate::processor::{tracking, PassController, ProcessState, Pass, SourceFile};
|
||||||
|
|
||||||
struct Visitor<'a> {
|
struct Visitor<'a> {
|
||||||
pub_crate: Visibility,
|
pub_crate: Visibility,
|
||||||
|
|
@ -37,7 +37,7 @@ impl VisitMut for Visitor<'_> {
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Privatize {}
|
pub struct Privatize {}
|
||||||
|
|
||||||
impl Processor for Privatize {
|
impl Pass for Privatize {
|
||||||
fn process_file(
|
fn process_file(
|
||||||
&mut self,
|
&mut self,
|
||||||
krate: &mut syn::File,
|
krate: &mut syn::File,
|
||||||
|
|
@ -10,7 +10,7 @@ use std::{collections::HashSet, ffi::OsStr, fmt::Debug};
|
||||||
|
|
||||||
pub(crate) use self::checker::PassController;
|
pub(crate) use self::checker::PassController;
|
||||||
|
|
||||||
pub(crate) trait Processor {
|
pub(crate) trait Pass {
|
||||||
fn refresh_state(&mut self) -> Result<()> {
|
fn refresh_state(&mut self) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -26,9 +26,16 @@ pub(crate) trait Processor {
|
||||||
) -> ProcessState;
|
) -> ProcessState;
|
||||||
|
|
||||||
fn name(&self) -> &'static str;
|
fn name(&self) -> &'static str;
|
||||||
|
|
||||||
|
fn boxed(self) -> Box<dyn Pass>
|
||||||
|
where
|
||||||
|
Self: Sized + 'static,
|
||||||
|
{
|
||||||
|
Box::new(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for dyn Processor {
|
impl Debug for dyn Pass {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
f.write_str(self.name())
|
f.write_str(self.name())
|
||||||
}
|
}
|
||||||
|
|
@ -88,7 +95,7 @@ impl Minimizer {
|
||||||
|
|
||||||
pub(crate) fn run_passes<'a>(
|
pub(crate) fn run_passes<'a>(
|
||||||
&self,
|
&self,
|
||||||
passes: impl IntoIterator<Item = Box<dyn Processor + 'a>>,
|
passes: impl IntoIterator<Item = Box<dyn Pass + 'a>>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let inital_build = self.build.build()?;
|
let inital_build = self.build.build()?;
|
||||||
info!("Initial build: {inital_build}");
|
info!("Initial build: {inital_build}");
|
||||||
|
|
@ -101,7 +108,7 @@ impl Minimizer {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_pass(&self, pass: &mut dyn Processor) -> Result<()> {
|
fn run_pass(&self, pass: &mut dyn Pass) -> Result<()> {
|
||||||
let mut invalidated_files = HashSet::new();
|
let mut invalidated_files = HashSet::new();
|
||||||
let mut refresh_and_try_again = false;
|
let mut refresh_and_try_again = false;
|
||||||
loop {
|
loop {
|
||||||
|
|
@ -137,7 +144,7 @@ impl Minimizer {
|
||||||
#[instrument(skip(self, pass, invalidated_files, changes), fields(pass = %pass.name()), level = "debug")]
|
#[instrument(skip(self, pass, invalidated_files, changes), fields(pass = %pass.name()), level = "debug")]
|
||||||
fn process_file<'file>(
|
fn process_file<'file>(
|
||||||
&self,
|
&self,
|
||||||
pass: &mut dyn Processor,
|
pass: &mut dyn Pass,
|
||||||
file: &'file SourceFile,
|
file: &'file SourceFile,
|
||||||
invalidated_files: &mut HashSet<&'file SourceFile>,
|
invalidated_files: &mut HashSet<&'file SourceFile>,
|
||||||
changes: &mut Changes,
|
changes: &mut Changes,
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
use crate::build::Build;
|
use crate::build::Build;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
files::Changes, tracking, Minimizer, PassController, ProcessState, Processor, SourceFile,
|
files::Changes, tracking, Minimizer, PassController, ProcessState, Pass, SourceFile,
|
||||||
};
|
};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use proc_macro2::Span;
|
use proc_macro2::Span;
|
||||||
|
|
@ -46,7 +46,7 @@ impl Minimizer {
|
||||||
self.apply_unused_imports(&suggestions_for_file)?;
|
self.apply_unused_imports(&suggestions_for_file)?;
|
||||||
|
|
||||||
self.run_passes([
|
self.run_passes([
|
||||||
Box::new(DeleteUnusedFunctions::new(self.build.clone(), diags)) as Box<dyn Processor>,
|
Box::new(DeleteUnusedFunctions::new(self.build.clone(), diags)) as Box<dyn Pass>,
|
||||||
])
|
])
|
||||||
.context("deleting unused functions")?;
|
.context("deleting unused functions")?;
|
||||||
|
|
||||||
|
|
@ -111,7 +111,7 @@ impl DeleteUnusedFunctions {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Processor for DeleteUnusedFunctions {
|
impl Pass for DeleteUnusedFunctions {
|
||||||
fn refresh_state(&mut self) -> Result<()> {
|
fn refresh_state(&mut self) -> Result<()> {
|
||||||
let (diags, _) = self.build.get_diags().context("getting diagnostics")?;
|
let (diags, _) = self.build.get_diags().context("getting diagnostics")?;
|
||||||
self.diags = diags;
|
self.diags = diags;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue