Skip to content

Commit

Permalink
bam/lazy/record: Change lazy::Record::flags to be infallible
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Aug 31, 2023
1 parent 20d498e commit f591311
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
6 changes: 6 additions & 0 deletions noodles-bam/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Changed

* bam/lazy/record: Change `lazy::Record::flags` to be infallible.

## 0.44.0 - 2023-08-31

### Added
Expand Down
15 changes: 7 additions & 8 deletions noodles-bam/src/lazy/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,14 @@ impl Record {
/// ```
/// use noodles_bam as bam;
/// use noodles_sam::record::Flags;
///
/// let record = bam::lazy::Record::default();
/// assert_eq!(record.flags()?, Flags::UNMAPPED);
/// # Ok::<_, std::io::Error>(())
/// assert_eq!(record.flags(), Flags::UNMAPPED);
/// ```
pub fn flags(&self) -> io::Result<sam::record::Flags> {
use crate::record::codec::decoder::get_flags;
let mut src = &self.buf[bounds::FLAGS_RANGE];
get_flags(&mut src).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
pub fn flags(&self) -> sam::record::Flags {
let src = &self.buf[bounds::FLAGS_RANGE];
// SAFETY: `src` is 2 bytes.
let n = u16::from_le_bytes(src.try_into().unwrap());
sam::record::Flags::from(n)
}

/// Returns the mate reference sequence ID.
Expand Down Expand Up @@ -324,7 +323,7 @@ impl TryFrom<Record> for sam::alignment::Record {
builder = builder.set_read_name(read_name.try_into()?);
}

builder = builder.set_flags(lazy_record.flags()?);
builder = builder.set_flags(lazy_record.flags());

if let Some(reference_sequence_id) = lazy_record.reference_sequence_id()? {
builder = builder.set_reference_sequence_id(reference_sequence_id);
Expand Down
10 changes: 6 additions & 4 deletions noodles-bam/src/record/codec/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ pub(crate) mod sequence;
mod template_length;

pub(crate) use self::{
cigar::get_cigar, data::get_data, flags::get_flags, position::get_position,
quality_scores::get_quality_scores, read_name::get_read_name,
reference_sequence_id::get_reference_sequence_id, sequence::get_sequence,
cigar::get_cigar, data::get_data, position::get_position, quality_scores::get_quality_scores,
read_name::get_read_name, reference_sequence_id::get_reference_sequence_id,
sequence::get_sequence,
};

use std::{error, fmt, mem};

use bytes::Buf;
use noodles_sam::{self as sam, alignment::Record};

use self::{mapping_quality::get_mapping_quality, template_length::get_template_length};
use self::{
flags::get_flags, mapping_quality::get_mapping_quality, template_length::get_template_length,
};

/// An error when a raw BAM record fails to parse.
#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion noodles-bam/src/record/codec/decoder/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl fmt::Display for DecodeError {
}
}

pub(crate) fn get_flags<B>(src: &mut B) -> Result<Flags, DecodeError>
pub(super) fn get_flags<B>(src: &mut B) -> Result<Flags, DecodeError>
where
B: Buf,
{
Expand Down

0 comments on commit f591311

Please sign in to comment.