Skip to content

Commit

Permalink
fix: handling of empty allow-lists (#903)
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Wagner <[email protected]>
  • Loading branch information
nwagner84 authored Feb 11, 2025
1 parent a1d82aa commit 559f09b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
32 changes: 18 additions & 14 deletions crates/pica-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,42 @@ pub(crate) enum FilterSetError {
}

pub(crate) struct FilterSet {
allow: HashSet<BString>,
deny: HashSet<BString>,
allow: Option<HashSet<BString>>,
deny: Option<HashSet<BString>>,
}

impl FilterSet {
pub(crate) fn new<P: AsRef<Path>>(
allow: Vec<P>,
deny: Vec<P>,
) -> Result<Self, FilterSetError> {
Ok(Self {
allow: read_filter_set(allow)?,
deny: read_filter_set(deny)?,
})
let allow = read_filter_set(allow)?;
let deny = read_filter_set(deny)?;

Ok(Self { allow, deny })
}

#[inline]
pub(crate) fn check(&self, ppn: &BStr) -> bool {
if self.allow.is_empty() && self.deny.is_empty() {
return true;
if let Some(ref deny) = self.deny {
if deny.contains(ppn) {
return false;
}
}

if !self.allow.is_empty() && !self.allow.contains(ppn) {
false
} else {
!self.deny.contains(ppn)
if let Some(ref allow) = self.allow {
if !allow.contains(ppn) || allow.is_empty() {
return false;
}
}

true
}
}

fn read_filter_set<P: AsRef<Path>>(
paths: Vec<P>,
) -> Result<HashSet<BString>, FilterSetError> {
) -> Result<Option<HashSet<BString>>, FilterSetError> {
let mut set = HashSet::new();

for path in paths.iter() {
Expand All @@ -73,7 +77,7 @@ fn read_filter_set<P: AsRef<Path>>(
);
}

Ok(set)
Ok(if !paths.is_empty() { Some(set) } else { None })
}

fn read_df<P: AsRef<Path>>(
Expand Down
38 changes: 38 additions & 0 deletions crates/pica-cli/tests/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,25 @@ fn filter_allow() -> TestResult {
.stderr(predicates::str::is_empty());
temp_dir.close().unwrap();

// empty allow list
let mut cmd = Command::cargo_bin("pica")?;
let temp_dir = TempDir::new().unwrap();
let allow = temp_dir.child("allow.csv");
allow.write_str("idn\n").unwrap();

let assert = cmd
.args(["filter", "-s", "003@?"])
.args(["-A", allow.to_str().unwrap()])
.arg(data_dir().join("DUMP.dat.gz"))
.assert();

assert
.success()
.code(0)
.stdout(predicates::str::is_empty())
.stderr(predicates::str::is_empty());
temp_dir.close().unwrap();

Ok(())
}

Expand Down Expand Up @@ -430,6 +449,25 @@ fn filter_deny() -> TestResult {
.stderr(predicates::str::is_empty());
temp_dir.close().unwrap();

// empty deny list
let mut cmd = Command::cargo_bin("pica")?;
let temp_dir = TempDir::new().unwrap();
let deny = temp_dir.child("deny.csv");
deny.write_str("idn\n").unwrap();

let assert = cmd
.args(["filter", "-s", "003@?"])
.args(["-D", deny.to_str().unwrap()])
.arg(data_dir().join("ada.dat"))
.assert();

assert
.success()
.code(0)
.stdout(predicates::path::eq_file(data_dir().join("ada.dat")))
.stderr(predicates::str::is_empty());
temp_dir.close().unwrap();

Ok(())
}

Expand Down

0 comments on commit 559f09b

Please sign in to comment.