Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 37 additions & 26 deletions src/cleanup_modules/device_cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,36 +148,47 @@ struct DeviceDumper {}
#[async_trait]
impl Dumper for DeviceDumper {
async fn dump(&self, state: &State) -> Result<(), ModuleError> {
let inf_regex = Regex::new(r"^oem[0-9]+\.inf$").unwrap();
let devices: Vec<Device> = enumerate_devices()
.into_module_report(DEVICE_MODULE_NAME)?
.into_iter()
.filter(|d| inf_regex.is_match(d.inf_name().unwrap_or("")))
.filter(is_of_interest)
.collect();

let file_path =
get_path_to_dump(state, "devices.json").into_module_report(DEVICE_MODULE_NAME)?;
let dump_file = create_dump_file(&file_path).into_module_report(DEVICE_MODULE_NAME)?;
let file_name = file_path.as_path().to_str().unwrap();

if devices.is_empty() {
println!("No devices to dump");
return Ok(());
}
dump_filtered(state, "devices.json", is_of_interest).await
}

serde_json::to_writer_pretty(dump_file, &devices)
.into_report()
.attach_printable_lazy(|| format!("failed to dump devices into '{}'", file_name))
.into_module_report(DEVICE_MODULE_NAME)?;
async fn dumpall(&self, state: &State) -> Result<(), ModuleError> {
dump_filtered(state, "devices-all.json", |_| true).await
}
}

match devices.len() {
1 => println!("Dumped 1 device to {}", file_name),
n => println!("Dumped {} devices to {}", n, file_name),
}
async fn dump_filtered<F: Fn(&Device) -> bool>(
state: &State,
output_file: &str,
filter_fn: F,
) -> Result<(), ModuleError> {
let inf_regex = Regex::new(r"^oem[0-9]+\.inf$").unwrap();
let devices: Vec<Device> = enumerate_devices()
.into_module_report(DEVICE_MODULE_NAME)?
.into_iter()
.filter(|d| inf_regex.is_match(d.inf_name().unwrap_or("")))
.filter(filter_fn)
.collect();

let file_path = get_path_to_dump(state, output_file).into_module_report(DEVICE_MODULE_NAME)?;
let dump_file = create_dump_file(&file_path).into_module_report(DEVICE_MODULE_NAME)?;
let file_name = file_path.as_path().to_str().unwrap();

if devices.is_empty() {
println!("No devices to dump");
return Ok(());
}

Ok(())
serde_json::to_writer_pretty(dump_file, &devices)
.into_report()
.attach_printable_lazy(|| format!("failed to dump devices into '{}'", file_name))
.into_module_report(DEVICE_MODULE_NAME)?;

match devices.len() {
1 => println!("Dumped 1 device to {}", file_name),
n => println!("Dumped {} devices to {}", n, file_name),
}

Ok(())
}

#[derive(Deserialize, Debug)]
Expand Down
59 changes: 35 additions & 24 deletions src/cleanup_modules/driver_cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,34 +124,45 @@ struct DriverDumper {}
#[async_trait]
impl Dumper for DriverDumper {
async fn dump(&self, state: &State) -> Result<(), ModuleError> {
let drivers: Vec<Driver> = enumerate_drivers()
.into_module_report(DRIVER_MODULE_NAME)?
.into_iter()
.filter(is_of_interest)
.collect();

let file_path =
get_path_to_dump(state, "drivers.json").into_module_report(DRIVER_MODULE_NAME)?;
let dump_file = create_dump_file(&file_path).into_module_report(DRIVER_MODULE_NAME)?;
let file_name = file_path.as_path().to_str().unwrap();

if drivers.is_empty() {
println!("No drivers to dump");
return Ok(());
}
dump_filtered(state, "drivers.json", is_of_interest).await
}

serde_json::to_writer_pretty(dump_file, &drivers)
.into_report()
.attach_printable_lazy(|| format!("failed to dump drivers into '{}'", file_name))
.into_module_report(DRIVER_MODULE_NAME)?;
async fn dumpall(&self, state: &State) -> Result<(), ModuleError> {
dump_filtered(state, "drivers-all.json", |_| true).await
}
}

match drivers.len() {
1 => println!("Dumped 1 driver into '{}'", file_name),
n => println!("Dumped {} drivers into '{}'", n, file_name),
}
async fn dump_filtered<F: Fn(&Driver) -> bool>(
state: &State,
output_file: &str,
filter_fn: F,
) -> Result<(), ModuleError> {
let drivers: Vec<Driver> = enumerate_drivers()
.into_module_report(DRIVER_MODULE_NAME)?
.into_iter()
.filter(filter_fn)
.collect();

let file_path = get_path_to_dump(state, output_file).into_module_report(DRIVER_MODULE_NAME)?;
let dump_file = create_dump_file(&file_path).into_module_report(DRIVER_MODULE_NAME)?;
let file_name = file_path.as_path().to_str().unwrap();

if drivers.is_empty() {
println!("No drivers to dump");
return Ok(());
}

Ok(())
serde_json::to_writer_pretty(dump_file, &drivers)
.into_report()
.attach_printable_lazy(|| format!("failed to dump drivers into '{}'", file_name))
.into_module_report(DRIVER_MODULE_NAME)?;

match drivers.len() {
1 => println!("Dumped 1 driver into '{}'", file_name),
n => println!("Dumped {} drivers into '{}'", n, file_name),
}

Ok(())
}

#[derive(Deserialize, Debug)]
Expand Down
59 changes: 34 additions & 25 deletions src/cleanup_modules/driver_package_cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,36 +150,45 @@ struct DriverPackageDumper {}
#[async_trait]
impl Dumper for DriverPackageDumper {
async fn dump(&self, state: &State) -> Result<(), ModuleError> {
let driver_packages: Vec<DriverPackage> = enumerate_driver_packages()
.into_module_report(MODULE_NAME)?
.into_iter()
.filter(is_of_interest)
.collect();

let file_path =
get_path_to_dump(state, "driver-packages.json").into_module_report(MODULE_NAME)?;
let dump_file = create_dump_file(&file_path).into_module_report(MODULE_NAME)?;
let file_name = file_path.as_path().to_str().unwrap();
dump_filtered(state, "driver-packages.json", is_of_interest).await
}

if driver_packages.is_empty() {
println!("No driver packages to dump");
return Ok(());
}
async fn dumpall(&self, state: &State) -> Result<(), ModuleError> {
dump_filtered(state, "driver-packages-all.json", |_| true).await
}
}

serde_json::to_writer_pretty(dump_file, &driver_packages)
.into_report()
.attach_printable_lazy(|| {
format!("failed to dump driver packages into '{}'", file_name)
})
.into_module_report(MODULE_NAME)?;
async fn dump_filtered<F: Fn(&DriverPackage) -> bool>(
state: &State,
output_file: &str,
filter_fn: F,
) -> Result<(), ModuleError> {
let driver_packages: Vec<DriverPackage> = enumerate_driver_packages()
.into_module_report(MODULE_NAME)?
.into_iter()
.filter(filter_fn)
.collect();

let file_path = get_path_to_dump(state, output_file).into_module_report(MODULE_NAME)?;
let dump_file = create_dump_file(&file_path).into_module_report(MODULE_NAME)?;
let file_name = file_path.as_path().to_str().unwrap();

if driver_packages.is_empty() {
println!("No driver packages to dump");
return Ok(());
}

match driver_packages.len() {
1 => println!("Dumped 1 driver package into '{}'", file_name),
n => println!("Dumped {} driver packages into '{}'", n, file_name),
}
serde_json::to_writer_pretty(dump_file, &driver_packages)
.into_report()
.attach_printable_lazy(|| format!("failed to dump driver packages into '{}'", file_name))
.into_module_report(MODULE_NAME)?;

Ok(())
match driver_packages.len() {
1 => println!("Dumped 1 driver package into '{}'", file_name),
n => println!("Dumped {} driver packages into '{}'", n, file_name),
}

Ok(())
}

#[derive(Deserialize, Debug)]
Expand Down
1 change: 1 addition & 0 deletions src/cleanup_modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ pub struct ModuleRunInfo {
#[async_trait]
pub trait Dumper {
async fn dump(&self, state: &State) -> Result<(), ModuleError>;
async fn dumpall(&self, state: &State) -> Result<(), ModuleError>;
}

fn get_path_to_dump(state: &State, filename: &str) -> Result<PathBuf, std::io::Error> {
Expand Down
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,13 @@ pub async fn dump(config: Config) {
None => continue,
};

let result = dumper.dump(&state).await;
if let Err(err) = result {
eprintln!("{:?}", err);
eprintln!()
}
let results = [dumper.dump(&state).await, dumper.dumpall(&state).await];
results.iter().for_each(|result| {
if let Err(err) = result {
eprintln!("{:?}", err);
eprintln!()
}
});
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/services/regex_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ pub fn cached_match(input: Option<&str>, regex_pattern: Option<&str>) -> bool {
Some(regex) => regex,
None => {
let regex = build_regex(regex_pattern);
let Entry::Vacant(vacant) = cache.entry(regex_pattern.to_string()) else { unreachable!() };
let Entry::Vacant(vacant) = cache.entry(regex_pattern.to_string()) else {
unreachable!()
};
vacant.insert(regex)
}
}
Expand Down
Loading