mirror of
https://github.com/Noratrieb/cargo-minimize.git
synced 2026-01-15 17:05:00 +01:00
restructure passes
This commit is contained in:
parent
3b631198e0
commit
527e3ca657
6 changed files with 27 additions and 17 deletions
60
src/passes/everybody_loops.rs
Normal file
60
src/passes/everybody_loops.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
use quote::ToTokens;
|
||||
use syn::{parse_quote, visit_mut::VisitMut};
|
||||
|
||||
use crate::processor::{tracking, PassController, ProcessState, Pass, SourceFile};
|
||||
|
||||
struct Visitor<'a> {
|
||||
current_path: Vec<String>,
|
||||
checker: &'a mut PassController,
|
||||
|
||||
loop_expr: syn::Block,
|
||||
process_state: ProcessState,
|
||||
}
|
||||
|
||||
impl<'a> Visitor<'a> {
|
||||
fn new(checker: &'a mut PassController) -> Self {
|
||||
Self {
|
||||
current_path: Vec::new(),
|
||||
checker,
|
||||
process_state: ProcessState::NoChange,
|
||||
loop_expr: parse_quote! { { loop {} } },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl VisitMut for Visitor<'_> {
|
||||
fn visit_block_mut(&mut self, block: &mut syn::Block) {
|
||||
match block.stmts.as_slice() {
|
||||
[syn::Stmt::Expr(syn::Expr::Loop(syn::ExprLoop {
|
||||
body: loop_body, ..
|
||||
}))] if loop_body.stmts.is_empty() => {}
|
||||
_ if self.checker.can_process(&self.current_path) => {
|
||||
*block = self.loop_expr.clone();
|
||||
self.process_state = ProcessState::Changed;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
tracking!();
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct EverybodyLoops;
|
||||
|
||||
impl Pass for EverybodyLoops {
|
||||
fn process_file(
|
||||
&mut self,
|
||||
krate: &mut syn::File,
|
||||
_: &SourceFile,
|
||||
checker: &mut PassController,
|
||||
) -> ProcessState {
|
||||
let mut visitor = Visitor::new(checker);
|
||||
visitor.visit_file_mut(krate);
|
||||
visitor.process_state
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"everybody-loops"
|
||||
}
|
||||
}
|
||||
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};
|
||||
55
src/passes/privatize.rs
Normal file
55
src/passes/privatize.rs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
use quote::ToTokens;
|
||||
use syn::{parse_quote, visit_mut::VisitMut, Visibility};
|
||||
|
||||
use crate::processor::{tracking, PassController, ProcessState, Pass, SourceFile};
|
||||
|
||||
struct Visitor<'a> {
|
||||
pub_crate: Visibility,
|
||||
process_state: ProcessState,
|
||||
current_path: Vec<String>,
|
||||
checker: &'a mut PassController,
|
||||
}
|
||||
|
||||
impl<'a> Visitor<'a> {
|
||||
fn new(checker: &'a mut PassController) -> Self {
|
||||
Self {
|
||||
process_state: ProcessState::NoChange,
|
||||
pub_crate: parse_quote! { pub(crate) },
|
||||
current_path: Vec::new(),
|
||||
checker,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl VisitMut for Visitor<'_> {
|
||||
fn visit_visibility_mut(&mut self, vis: &mut Visibility) {
|
||||
if let Visibility::Public(_) = vis {
|
||||
if self.checker.can_process(&self.current_path) {
|
||||
self.process_state = ProcessState::Changed;
|
||||
*vis = self.pub_crate.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tracking!();
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Privatize {}
|
||||
|
||||
impl Pass for Privatize {
|
||||
fn process_file(
|
||||
&mut self,
|
||||
krate: &mut syn::File,
|
||||
_: &SourceFile,
|
||||
checker: &mut PassController,
|
||||
) -> ProcessState {
|
||||
let mut visitor = Visitor::new(checker);
|
||||
visitor.visit_file_mut(krate);
|
||||
visitor.process_state
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"privatize"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue