Skip to content

Commit

Permalink
sam/header/record/value/map/read_group/platform: Disallow inputs with…
Browse files Browse the repository at this point in the history
… mixed-case

This is in response to the change in samtools/hts-specs#684.
  • Loading branch information
zaeleus committed Jan 11, 2023
1 parent d45d9b9 commit ecc0037
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
3 changes: 3 additions & 0 deletions noodles-sam/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
* sam/header/record: Add record IDs to `ParseError::InvalidReferenceSequence`
and `ParseError::InvalidReadGroup`.

* sam/header/record/value/map/read_group/platform: Disallow inputs with
mixed-case.

* sam/header/record/value/map/reference_sequence: `ReferenceSequence::new`
requires a nonzero length.

Expand Down
27 changes: 23 additions & 4 deletions noodles-sam/src/header/record/value/map/read_group/platform.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! SAM header read group platform.
use std::{error, fmt, str::FromStr};
use std::{borrow::Cow, error, fmt, str::FromStr};

/// A SAM header read group platform (`PL`).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -77,8 +77,27 @@ impl FromStr for Platform {
type Err = ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_uppercase().as_str() {
"" => Err(ParseError::Empty),
if s.is_empty() {
return Err(ParseError::Empty);
}

let (is_lowercase, is_uppercase) = s
.chars()
.filter(char::is_ascii_alphabetic)
.map(|c| c.is_ascii_uppercase())
.fold((true, true), |(l, u), is_ascii_uppercase| {
(l & !is_ascii_uppercase, u & is_ascii_uppercase)
});

let t = if is_uppercase {
Cow::from(s)
} else if is_lowercase {
Cow::from(s.to_uppercase())
} else {
return Err(ParseError::Invalid);
};

match t.as_ref() {
"CAPILLARY" => Ok(Self::Capillary),
"DNBSEQ" => Ok(Self::DnbSeq),
"ELEMENT" => Ok(Self::Element),
Expand Down Expand Up @@ -128,10 +147,10 @@ mod tests {
assert_eq!("PACBIO".parse(), Ok(Platform::PacBio));
assert_eq!("ULTIMA".parse(), Ok(Platform::Ultima));

assert_eq!("Illumina".parse(), Ok(Platform::Illumina));
assert_eq!("illumina".parse(), Ok(Platform::Illumina));

assert_eq!("".parse::<Platform>(), Err(ParseError::Empty));
assert_eq!("Illumina".parse::<Platform>(), Err(ParseError::Invalid));
assert_eq!("NOODLES".parse::<Platform>(), Err(ParseError::Invalid));
}
}

0 comments on commit ecc0037

Please sign in to comment.