Skip to content

Commit

Permalink
[Misc]: changing all walkdirs to use the same cmp operator (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshfried-aws authored Feb 2, 2024
1 parent 4d8d420 commit eb43629
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
12 changes: 11 additions & 1 deletion guard/src/commands/files.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::cmp::Ordering;
use std::cmp::{self, Ordering};
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::PathBuf;
Expand Down Expand Up @@ -103,3 +103,13 @@ pub(crate) fn regular_ordering(
) -> Ordering {
Ordering::Equal
}

pub(crate) fn walk_dir(
base: PathBuf,
cmp: fn(&walkdir::DirEntry, &walkdir::DirEntry) -> cmp::Ordering,
) -> std::iter::Flatten<walkdir::IntoIter> {
walkdir::WalkDir::new(base)
.sort_by(cmp)
.into_iter()
.flatten()
}
12 changes: 4 additions & 8 deletions guard/src/commands/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use enumflags2::BitFlags;
use serde::Deserialize;

use crate::command::Command;
use crate::commands::files::{alpabetical, iterate_over, last_modified};
use crate::commands::files::{alpabetical, iterate_over, last_modified, walk_dir};
use crate::commands::tracker::StatusContext;
use crate::commands::validate::structured::StructuredEvaluator;
use crate::commands::validate::summary_table::SummaryType;
Expand Down Expand Up @@ -307,7 +307,7 @@ or rules files.
for file_or_dir in list_of_file_or_dir {
validate_path(file_or_dir)?;
let base = PathBuf::from_str(file_or_dir)?;
for file in walkdir::WalkDir::new(base).into_iter().flatten() {
for file in walk_dir(base, cmp) {
if file.path().is_file() {
let name = file
.file_name()
Expand Down Expand Up @@ -349,7 +349,7 @@ or rules files.
validate_path(file_or_dir)?;
let base = PathBuf::from_str(file_or_dir)?;

for file in walkdir::WalkDir::new(base).into_iter().flatten() {
for file in walk_dir(base, cmp) {
if file.path().is_file() {
let name = file
.file_name()
Expand Down Expand Up @@ -396,11 +396,7 @@ or rules files.
if base.is_file() {
rules.push(base.clone())
} else {
for entry in walkdir::WalkDir::new(base)
.sort_by(cmp)
.into_iter()
.flatten()
{
for entry in walk_dir(base, cmp) {
if entry.path().is_file()
&& entry
.path()
Expand Down

0 comments on commit eb43629

Please sign in to comment.