Skip to content

Commit

Permalink
Allow subfield wildcard in path expressions (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwagner84 authored Dec 11, 2023
1 parent 5e6cfc9 commit 2f9272d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
21 changes: 18 additions & 3 deletions crates/pica-path/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ use winnow::error::ParserError;
use winnow::prelude::*;
use winnow::stream::{AsChar, Stream, StreamIsPartial};

const SUBFIELD_CODES: &str =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

#[derive(Debug, Error)]
#[error("invalid path expression, got `{0}`")]
pub struct ParsePathError(pub String);
Expand Down Expand Up @@ -181,8 +184,12 @@ fn parse_subfield_code_list(i: &mut &[u8]) -> PResult<Vec<char>> {

#[inline]
fn parse_subfield_codes(i: &mut &[u8]) -> PResult<Vec<char>> {
alt((parse_subfield_code_list, parse_subfield_code_single))
.parse_next(i)
alt((
parse_subfield_code_list,
parse_subfield_code_single,
'*'.value(SUBFIELD_CODES.chars().collect()),
))
.parse_next(i)
}

fn parse_path_simple(i: &mut &[u8]) -> PResult<Path> {
Expand Down Expand Up @@ -362,7 +369,7 @@ mod tests {

#[test]
fn parse_subfield_codes() {
use super::parse_subfield_codes;
use super::{parse_subfield_codes, SUBFIELD_CODES};

macro_rules! parse_success {
($input:expr, $expected:expr) => {
Expand All @@ -376,6 +383,10 @@ mod tests {
parse_success!(b"[a-cx]", vec!['a', 'b', 'c', 'x']);
parse_success!(b"[a-c]", vec!['a', 'b', 'c']);
parse_success!(b"a", vec!['a']);
parse_success!(
b"*",
SUBFIELD_CODES.chars().collect::<Vec<_>>()
);
}

#[test]
Expand All @@ -391,7 +402,9 @@ mod tests {
parse_success!(b"021A/*.[a-c]");
parse_success!(b"021A.[a-c]");
parse_success!(b"021A/*.a");
parse_success!(b"..../*.*");
parse_success!(b"021A.a");
parse_success!(b"021A.*");
}

#[test]
Expand All @@ -406,10 +419,12 @@ mod tests {
parse_success!(b"021A/*{ [a-cx], y }");
parse_success!(b"021A/*{ ([a-cx], y) }");
parse_success!(b"021A/*{ ([a-cx], y) | y? }");
parse_success!(b"021A/*{ * | y? }");
parse_success!(b"021A{ [a-cx] }");
parse_success!(b"021A/*{[a-c]}");
parse_success!(b"021A{[a-c]}");
parse_success!(b"021A/*{a}");
parse_success!(b"021A{a}");
parse_success!(b"021A{*}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bin.name = "pica"
args = "select '012A.*'"
status = "success"
stdin = "012A \u001fa123\u001fa456\u001e\n"
stdout = "123\n456\n"
stderr = ""

0 comments on commit 2f9272d

Please sign in to comment.