Skip to content

Commit

Permalink
sam/alignment/record_buf/data/field/value: Add conversion to crate::a…
Browse files Browse the repository at this point in the history
…lignment::record::data::field::Value
  • Loading branch information
zaeleus committed Jan 19, 2024
1 parent 8bb067d commit 5388ad9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 87 deletions.
91 changes: 4 additions & 87 deletions noodles-sam/src/alignment/record_buf/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ pub mod field;

use std::{io, mem};

use bstr::ByteSlice;

use self::field::Value;
use crate::alignment::record::data::field::Tag;

Expand Down Expand Up @@ -254,8 +252,7 @@ impl crate::alignment::record::field::Data for &Data {
&self,
tag: &Tag,
) -> Option<io::Result<crate::alignment::record::data::field::Value<'_>>> {
let value = Data::get(self, tag)?;
Some(Ok(value_buf_to_value(value)))
Data::get(self, tag).map(|value| Ok(value.into()))
}

fn iter(
Expand All @@ -264,7 +261,7 @@ impl crate::alignment::record::field::Data for &Data {
dyn Iterator<Item = io::Result<(Tag, crate::alignment::record::data::field::Value<'_>)>>
+ '_,
> {
Box::new(Data::iter(self).map(|(tag, value)| Ok((tag, value_buf_to_value(value)))))
Box::new(Data::iter(self).map(|(tag, value)| Ok((tag, value.into()))))
}
}

Expand All @@ -277,8 +274,7 @@ impl crate::alignment::record::field::Data for Data {
&self,
tag: &Tag,
) -> Option<io::Result<crate::alignment::record::data::field::Value<'_>>> {
let value = self.get(tag)?;
Some(Ok(value_buf_to_value(value)))
self.get(tag).map(|value| Ok(value.into()))
}

fn iter(
Expand All @@ -287,86 +283,7 @@ impl crate::alignment::record::field::Data for Data {
dyn Iterator<Item = io::Result<(Tag, crate::alignment::record::data::field::Value<'_>)>>
+ '_,
> {
Box::new(
self.iter()
.map(|(tag, value)| Ok((tag, value_buf_to_value(value)))),
)
}
}

fn value_buf_to_value(value: &Value) -> crate::alignment::record::data::field::Value<'_> {
use self::field::value::Array;

match value {
Value::Character(c) => crate::alignment::record::data::field::Value::Character(*c),
Value::Int8(n) => crate::alignment::record::data::field::Value::Int8(*n),
Value::UInt8(n) => crate::alignment::record::data::field::Value::UInt8(*n),
Value::Int16(n) => crate::alignment::record::data::field::Value::Int16(*n),
Value::UInt16(n) => crate::alignment::record::data::field::Value::UInt16(*n),
Value::Int32(n) => crate::alignment::record::data::field::Value::Int32(*n),
Value::UInt32(n) => crate::alignment::record::data::field::Value::UInt32(*n),
Value::Float(n) => crate::alignment::record::data::field::Value::Float(*n),
Value::String(s) => crate::alignment::record::data::field::Value::String(s.as_bstr()),
Value::Hex(s) => crate::alignment::record::data::field::Value::Hex(s.as_bstr()),
Value::Array(Array::Int8(values)) => crate::alignment::record::data::field::Value::Array(
crate::alignment::record::data::field::value::Array::Int8(Box::new(Values::new(
values.as_ref(),
))),
),
Value::Array(Array::UInt8(values)) => crate::alignment::record::data::field::Value::Array(
crate::alignment::record::data::field::value::Array::UInt8(Box::new(Values::new(
values.as_ref(),
))),
),
Value::Array(Array::Int16(values)) => crate::alignment::record::data::field::Value::Array(
crate::alignment::record::data::field::value::Array::Int16(Box::new(Values::new(
values.as_ref(),
))),
),
Value::Array(Array::UInt16(values)) => crate::alignment::record::data::field::Value::Array(
crate::alignment::record::data::field::value::Array::UInt16(Box::new(Values::new(
values.as_ref(),
))),
),
Value::Array(Array::Int32(values)) => crate::alignment::record::data::field::Value::Array(
crate::alignment::record::data::field::value::Array::Int32(Box::new(Values::new(
values.as_ref(),
))),
),
Value::Array(Array::UInt32(values)) => crate::alignment::record::data::field::Value::Array(
crate::alignment::record::data::field::value::Array::UInt32(Box::new(Values::new(
values.as_ref(),
))),
),
Value::Array(Array::Float(values)) => crate::alignment::record::data::field::Value::Array(
crate::alignment::record::data::field::value::Array::Float(Box::new(Values::new(
values.as_ref(),
))),
),
}
}

struct Values<'a, N>(&'a [N]);

impl<'a, N> Values<'a, N>
where
N: Copy,
{
fn new(values: &'a [N]) -> Self {
Self(values)
}
}

impl<'a, N> crate::alignment::record::data::field::value::array::Values<'a, N> for Values<'a, N>
where
N: Copy,
{
fn len(&self) -> usize {
self.0.len()
}

fn iter(&self) -> Box<dyn Iterator<Item = io::Result<N>> + '_> {
Box::new(self.0.iter().copied().map(Ok))
Box::new(self.iter().map(|(tag, value)| Ok((tag, value.into()))))
}
}

Expand Down
18 changes: 18 additions & 0 deletions noodles-sam/src/alignment/record_buf/data/field/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,24 @@ impl TryFrom<i64> for Value {
}
}

impl<'a> From<&'a Value> for crate::alignment::record::data::field::Value<'a> {
fn from(value_buf: &'a Value) -> Self {
match value_buf {
Value::Character(c) => Self::Character(*c),
Value::Int8(n) => Self::Int8(*n),
Value::UInt8(n) => Self::UInt8(*n),
Value::Int16(n) => Self::Int16(*n),
Value::UInt16(n) => Self::UInt16(*n),
Value::Int32(n) => Self::Int32(*n),
Value::UInt32(n) => Self::UInt32(*n),
Value::Float(n) => Self::Float(*n),
Value::String(s) => Self::String(s.as_ref()),
Value::Hex(s) => Self::Hex(s.as_ref()),
Value::Array(array) => Self::Array(array.into()),
}
}
}

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

0 comments on commit 5388ad9

Please sign in to comment.