Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handling of empty allow-lists #903

Merged
merged 1 commit into from
Feb 11, 2025
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
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