Skip to content

Commit

Permalink
sam/lazy/record/position: Implement crate::alignment::record::Positio…
Browse files Browse the repository at this point in the history
…n for Position
  • Loading branch information
zaeleus committed Dec 14, 2023
1 parent 565fd62 commit f251100
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions noodles-sam/src/lazy/record/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::io;

use noodles_core as core;

use crate::alignment::record::Position as _;

/// Raw SAM record position.
#[derive(Debug, Eq, PartialEq)]
pub struct Position<'a>(&'a [u8]);
Expand All @@ -12,6 +14,13 @@ impl<'a> Position<'a> {
}
}

impl<'a> crate::alignment::record::Position for Position<'a> {
fn try_to_usize(&self) -> io::Result<usize> {
lexical_core::parse(self.as_ref())
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
}
}

impl<'a> AsRef<[u8]> for Position<'a> {
fn as_ref(&self) -> &[u8] {
self.0
Expand All @@ -22,11 +31,8 @@ impl<'a> TryFrom<Position<'a>> for core::Position {
type Error = io::Error;

fn try_from(raw_position: Position<'a>) -> Result<Self, Self::Error> {
lexical_core::parse(raw_position.as_ref())
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
.and_then(|n: usize| {
core::Position::try_from(n)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
})
raw_position.try_to_usize().and_then(|n: usize| {
core::Position::try_from(n).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
})
}
}

0 comments on commit f251100

Please sign in to comment.