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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! Variant record samples array value.

mod values;

use std::{borrow::Cow, fmt, io};

pub use self::values::Values;

/// A variant record samples array value.
pub enum Array<'a> {
    /// A 32-bit integer array.
    Integer(Box<dyn Values<'a, i32> + 'a>),
    /// A single-precision floating-point array..
    Float(Box<dyn Values<'a, f32> + 'a>),
    /// A character array.
    Character(Box<dyn Values<'a, char> + 'a>),
    /// A string array.
    String(Box<dyn Values<'a, Cow<'a, str>> + 'a>),
}

impl<'a> fmt::Debug for Array<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Integer(values) => f.debug_list().entries(values.iter()).finish(),
            Self::Float(values) => f.debug_list().entries(values.iter()).finish(),
            Self::Character(values) => f.debug_list().entries(values.iter()).finish(),
            Self::String(values) => f.debug_list().entries(values.iter()).finish(),
        }
    }
}

impl<'a> TryFrom<Array<'a>> for crate::variant::record_buf::samples::sample::value::Array {
    type Error = io::Error;

    fn try_from(array: Array<'a>) -> Result<Self, Self::Error> {
        match array {
            Array::Integer(values) => values.iter().collect::<io::Result<_>>().map(Self::Integer),
            Array::Float(values) => values.iter().collect::<io::Result<_>>().map(Self::Float),
            Array::Character(values) => values
                .iter()
                .collect::<io::Result<_>>()
                .map(Self::Character),
            Array::String(values) => values
                .iter()
                .map(|result| result.map(|value| value.map(String::from)))
                .collect::<io::Result<_>>()
                .map(Self::String),
        }
    }
}