Skip to content

Commit

Permalink
fix: process of records without a ppn (#908)
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Wagner <[email protected]>
  • Loading branch information
nwagner84 authored Feb 13, 2025
1 parent 7465d04 commit 003a405
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/pica-cli/src/commands/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub(crate) enum Strategy {
#[inline]
fn record_key(record: &ByteRecord, strategy: &Strategy) -> String {
match strategy {
Strategy::Idn => record.ppn().to_string(),
Strategy::Idn => record.ppn().expect("ppn").to_string(),
Strategy::Hash => {
record.sha256().iter().fold(String::new(), |mut out, b| {
let _ = write!(out, "{b:02x}");
Expand Down
6 changes: 4 additions & 2 deletions crates/pica-cli/src/commands/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ impl Filter {
Ok(ref mut record) => {
progress.update(false);

if !filter_set.check(record.ppn()) {
continue;
if let Some(ppn) = record.ppn() {
if !filter_set.check(ppn) {
continue;
}
}

let mut is_match =
Expand Down
6 changes: 4 additions & 2 deletions crates/pica-cli/src/commands/frequency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,10 @@ impl Frequency {
Ok(ref record) => {
progress.update(false);

if !filter_set.check(record.ppn()) {
continue;
if let Some(ppn) = record.ppn() {
if !filter_set.check(ppn) {
continue;
}
}

if let Some(ref matcher) = matcher {
Expand Down
5 changes: 4 additions & 1 deletion crates/pica-cli/src/commands/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ impl Hash {
);

writer.write_record(&[
record.ppn().to_string(),
record
.ppn()
.unwrap_or_default()
.to_string(),
hash,
])?;
}
Expand Down
6 changes: 4 additions & 2 deletions crates/pica-cli/src/commands/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,10 @@ impl Select {
Ok(ref record) => {
progress.update(false);

if !filter_set.check(record.ppn()) {
continue;
if let Some(ppn) = record.ppn() {
if !filter_set.check(ppn) {
continue;
}
}

if let Some(ref matcher) = matcher {
Expand Down
16 changes: 16 additions & 0 deletions crates/pica-cli/tests/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,19 @@ fn filter_tee() -> TestResult {
temp_dir.close().unwrap();
Ok(())
}

/// https://github.com/deutsche-nationalbibliothek/pica-rs/issues/907
#[test]
fn filter_no_ppn() -> TestResult {
let mut cmd = Command::cargo_bin("pica")?;

let data = "036E/00 \x1faSpringer-Lehrbuch\x1e036E/01 \x1faSpringer-Link\x1fpBücher\x1e\n";
let assert =
cmd.args(["filter", "....?"]).write_stdin(data).assert();
assert
.success()
.code(0)
.stdout(predicates::ord::eq(data))
.stderr(predicates::str::is_empty());
Ok(())
}
8 changes: 4 additions & 4 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,15 @@ pub trait PathExt {
/// use pica_record::prelude::*;
///
/// let record = ByteRecord::from_bytes(b"003@ \x1f0118540238\x1e\n")?;
/// assert_eq!(record.ppn(), "118540238");
/// assert_eq!(record.ppn().unwrap(), "118540238");
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
fn ppn(&self) -> &Self::Value {
fn ppn(&self) -> Option<&Self::Value> {
static PATH: LazyLock<Path> =
LazyLock::new(|| Path::new("[email protected]").unwrap());

self.first(&PATH, &Default::default()).unwrap()
self.first(&PATH, &Default::default())
}
}
impl PathExt for RecordRef<'_> {
Expand Down Expand Up @@ -492,7 +492,7 @@ mod tests {
fn test_path_ppn() -> TestResult {
let data = ada_lovelace();
let record = ByteRecord::from_bytes(&data)?;
assert_eq!(record.ppn(), "119232022");
assert_eq!(record.ppn().unwrap(), "119232022");
Ok(())
}
}

0 comments on commit 003a405

Please sign in to comment.