noodles_sam/alignment/record/data/field/value/
array.rs

1//! Alignment record data field array value.
2
3mod subtype;
4mod values;
5
6use std::{fmt, io};
7
8pub use self::{subtype::Subtype, values::Values};
9
10/// An alignment record data field array value.
11pub enum Array<'a> {
12    /// An 8-bit integer array (`B:c`).
13    Int8(Box<dyn Values<'a, i8> + 'a>),
14    /// An 8-bit unsigned integer array (`B:C`).
15    UInt8(Box<dyn Values<'a, u8> + 'a>),
16    /// A 16-bit integer array (`B:s`).
17    Int16(Box<dyn Values<'a, i16> + 'a>),
18    /// A 16-bit unsigned integer array (`B:S`).
19    UInt16(Box<dyn Values<'a, u16> + 'a>),
20    /// A 32-bit integer array (`B:i`).
21    Int32(Box<dyn Values<'a, i32> + 'a>),
22    /// A 32-bit unsigned integer array (`B:I`).
23    UInt32(Box<dyn Values<'a, u32> + 'a>),
24    /// A single-precision floating-point array (`B:f`).
25    Float(Box<dyn Values<'a, f32> + 'a>),
26}
27
28impl Array<'_> {
29    /// Returns the array value type.
30    pub fn subtype(&self) -> Subtype {
31        match self {
32            Array::Int8(_) => Subtype::Int8,
33            Array::UInt8(_) => Subtype::UInt8,
34            Array::Int16(_) => Subtype::Int16,
35            Array::UInt16(_) => Subtype::UInt16,
36            Array::Int32(_) => Subtype::Int32,
37            Array::UInt32(_) => Subtype::UInt32,
38            Array::Float(_) => Subtype::Float,
39        }
40    }
41}
42
43impl fmt::Debug for Array<'_> {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            Self::Int8(values) => f.debug_list().entries(values.iter()).finish(),
47            Self::UInt8(values) => f.debug_list().entries(values.iter()).finish(),
48            Self::Int16(values) => f.debug_list().entries(values.iter()).finish(),
49            Self::UInt16(values) => f.debug_list().entries(values.iter()).finish(),
50            Self::Int32(values) => f.debug_list().entries(values.iter()).finish(),
51            Self::UInt32(values) => f.debug_list().entries(values.iter()).finish(),
52            Self::Float(values) => f.debug_list().entries(values.iter()).finish(),
53        }
54    }
55}
56
57impl<'a> TryFrom<Array<'a>> for crate::alignment::record_buf::data::field::value::Array {
58    type Error = io::Error;
59
60    fn try_from(array: Array<'a>) -> Result<Self, Self::Error> {
61        match array {
62            Array::Int8(values) => values.iter().collect::<Result<_, _>>().map(Self::Int8),
63            Array::UInt8(values) => values.iter().collect::<Result<_, _>>().map(Self::UInt8),
64            Array::Int16(values) => values.iter().collect::<Result<_, _>>().map(Self::Int16),
65            Array::UInt16(values) => values.iter().collect::<Result<_, _>>().map(Self::UInt16),
66            Array::Int32(values) => values.iter().collect::<Result<_, _>>().map(Self::Int32),
67            Array::UInt32(values) => values.iter().collect::<Result<_, _>>().map(Self::UInt32),
68            Array::Float(values) => values.iter().collect::<Result<_, _>>().map(Self::Float),
69        }
70    }
71}