noodles_vcf/variant/record/samples/series/value/
array.rs

1//! Variant record samples array value.
2
3mod values;
4
5use std::{borrow::Cow, fmt, io};
6
7pub use self::values::Values;
8
9/// A variant record samples array value.
10pub enum Array<'a> {
11    /// A 32-bit integer array.
12    Integer(Box<dyn Values<'a, i32> + 'a>),
13    /// A single-precision floating-point array..
14    Float(Box<dyn Values<'a, f32> + 'a>),
15    /// A character array.
16    Character(Box<dyn Values<'a, char> + 'a>),
17    /// A string array.
18    String(Box<dyn Values<'a, Cow<'a, str>> + 'a>),
19}
20
21impl fmt::Debug for Array<'_> {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            Self::Integer(values) => f.debug_list().entries(values.iter()).finish(),
25            Self::Float(values) => f.debug_list().entries(values.iter()).finish(),
26            Self::Character(values) => f.debug_list().entries(values.iter()).finish(),
27            Self::String(values) => f.debug_list().entries(values.iter()).finish(),
28        }
29    }
30}
31
32impl<'a> TryFrom<Array<'a>> for crate::variant::record_buf::samples::sample::value::Array {
33    type Error = io::Error;
34
35    fn try_from(array: Array<'a>) -> Result<Self, Self::Error> {
36        match array {
37            Array::Integer(values) => values.iter().collect::<io::Result<_>>().map(Self::Integer),
38            Array::Float(values) => values.iter().collect::<io::Result<_>>().map(Self::Float),
39            Array::Character(values) => values
40                .iter()
41                .collect::<io::Result<_>>()
42                .map(Self::Character),
43            Array::String(values) => values
44                .iter()
45                .map(|result| result.map(|value| value.map(String::from)))
46                .collect::<io::Result<_>>()
47                .map(Self::String),
48        }
49    }
50}