Skip to content

Commit

Permalink
gff/record: Parse phase
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Nov 20, 2024
1 parent 59d83ce commit 8dca4da
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
2 changes: 2 additions & 0 deletions noodles-gff/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

This splits the line into its key-optional value components.

* gff/record: Parse phase (`Record::phase`).

### Removed

* gff: Remove `lazy` module.
Expand Down
38 changes: 35 additions & 3 deletions noodles-gff/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use noodles_core::Position;

pub use self::attributes::Attributes;
use self::fields::Fields;
use crate::record_buf::Strand;
use crate::record_buf::{Phase, Strand};

/// An immutable, lazily-evalulated GFF record.
#[derive(Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -57,8 +57,8 @@ impl<'l> Record<'l> {
}

/// Returns the phase.
pub fn phase(&self) -> &str {
self.0.phase()
pub fn phase(&self) -> Option<io::Result<Phase>> {
parse_phase(self.0.phase())
}

/// Returns the attributes.
Expand Down Expand Up @@ -87,3 +87,35 @@ fn parse_strand(s: &str) -> io::Result<Strand> {
s.parse()
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
}

fn parse_phase(s: &str) -> Option<io::Result<Phase>> {
const MISSING: &str = ".";

match s {
MISSING => None,
_ => Some(
s.parse()
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)),
),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_phase() -> io::Result<()> {
assert!(parse_phase(".").is_none());
assert_eq!(parse_phase("0").transpose()?, Some(Phase::Zero));
assert_eq!(parse_phase("1").transpose()?, Some(Phase::One));
assert_eq!(parse_phase("2").transpose()?, Some(Phase::Two));

assert!(matches!(
parse_phase(""),
Some(Err(e)) if e.kind() == io::ErrorKind::InvalidData
));

Ok(())
}
}
7 changes: 1 addition & 6 deletions noodles-gff/src/record_buf/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ impl<'l> TryFrom<Record<'l>> for RecordBuf {

builder = builder.set_strand(record.strand()?);

if record.phase() != MISSING_FIELD {
let phase = record
.phase()
.parse()
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;

if let Some(phase) = record.phase().transpose()? {
builder = builder.set_phase(phase);
}

Expand Down

0 comments on commit 8dca4da

Please sign in to comment.