Skip to content

Commit

Permalink
Merge pull request #27 from Rust-Wellcome/better-parsing-of-header
Browse files Browse the repository at this point in the history
Better parsing of header



This ensures that the text from nested headers is not split on commas that are enclosed in quotes.

In particular, it ensures that this line

##INFO=<ID=DB,Number=0,Type=Flag,Description="dbSNP membership, build 129">

from the example in section 1.1 of the specs can be parsed.

Note that section 1.2 lists the comma as one of the special characters which should always be 'represented with the
capitalized percent encoding' when they are not used for their specific meaning. However, I assume this refers only to cases where they are not enclosed within quotes.
  • Loading branch information
MrCurtis committed Sep 5, 2023
2 parents da15735 + 4143189 commit d8b9cdd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
2 changes: 2 additions & 0 deletions vcf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
regex = "1.9.3"
lazy_static = "1.4.0"
40 changes: 40 additions & 0 deletions vcf/src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,44 @@ mod tests {
],
);
}

#[test]
fn can_parse_when_quoted_text_contains_comma_in_last_key_value_pair() {
let input = "##FORMAT=<abc=123,xyz=3125,sfh=\"1,574\">";
let header = Header::parse(input);

assert_eq!(
header,
Ok(
Header {
key: "FORMAT",
value: HeaderValue::Nested(HashMap::from([
("abc", "123"),
("xyz", "3125"),
("sfh", "1,574"),
])),
}
)
);
}

#[test]
fn can_parse_when_quoted_text_contains_comma_in_first_key_value_pair() {
let input = "##FORMAT=<abc=\"1,233\",xyz=3125,sfh=157>";
let header = Header::parse(input);

assert_eq!(
header,
Ok(
Header {
key: "FORMAT",
value: HeaderValue::Nested(HashMap::from([
("abc", "1,233"),
("xyz", "3125"),
("sfh", "157"),
])),
}
)
);
}
}
19 changes: 18 additions & 1 deletion vcf/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
use std::collections::HashMap;

use regex::Regex;
use lazy_static::lazy_static;

use crate::{Header, HeaderValue};

lazy_static! {
// Repeatedly match either non-comma/non-quote characters or blocks of text enclosed in
// quotes, until we can't, in which case we're either at a non-quote-enclosed comma or the
// end of the string.
static ref HEADER_VALUE_REGEX: Regex = Regex::new(r#"(?:[^,"]+|(?:"[^"]*"))+"#).unwrap();
}

impl<'src> Header<'src> {
pub fn parse(input: &'src str) -> Result<Self, ParseError> {
let line = input.trim();
Expand All @@ -18,8 +28,15 @@ impl<'src> HeaderValue<'src> {
match input.strip_prefix('<').and_then(|input| input.strip_suffix('>')) {
None => Ok(Self::Flat(input)),
Some(pairs) => {
pairs.split(',')
HEADER_VALUE_REGEX.captures_iter(pairs)
.map(|c| c.get(0).unwrap().as_str())
.map(|pair| pair.split_once('=').ok_or(ParseError))
.map(
|r| match r {
Ok((k, v)) => Ok((k, v.trim_matches('\"'))),
x => x,
}
)
.collect::<Result<HashMap<_, _>, _>>()
.map(HeaderValue::Nested)
}
Expand Down

0 comments on commit d8b9cdd

Please sign in to comment.