noodles_vcf/variant/record/info/field/value/
array.rs

1//! Variant record info field array value.
2
3mod values;
4
5use std::{borrow::Cow, fmt, io};
6
7pub use self::values::Values;
8
9/// A variant record info field 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::info::field::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::<Result<_, _>>().map(Self::Integer),
38            Array::Float(values) => values.iter().collect::<Result<_, _>>().map(Self::Float),
39            Array::Character(values) => {
40                values.iter().collect::<Result<_, _>>().map(Self::Character)
41            }
42            Array::String(values) => values
43                .iter()
44                .map(|result| result.map(|value| value.map(String::from)))
45                .collect::<Result<_, _>>()
46                .map(Self::String),
47        }
48    }
49}