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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! VCF record genotype sample.

pub mod value;

use std::{hash::Hash, io};

pub use self::value::Value;
use super::Keys;
use crate::Header;

/// A VCF record genotype sample.
#[derive(Debug, PartialEq)]
pub struct Sample<'g> {
    keys: &'g Keys,
    values: &'g [Option<Value>],
}

impl<'g> Sample<'g> {
    /// Creates a new genotype sample.
    pub fn new(keys: &'g Keys, values: &'g [Option<Value>]) -> Self {
        Self { keys, values }
    }

    /// Returns the keys.
    pub fn keys(&self) -> &'g Keys {
        self.keys
    }

    /// Returns the values.
    pub fn values(&self) -> &'g [Option<Value>] {
        self.values
    }

    /// Returns a reference to the value with the given key.
    pub fn get<K>(&self, key: &K) -> Option<Option<&'g Value>>
    where
        K: Hash + indexmap::Equivalent<String> + ?Sized,
    {
        self.keys
            .as_ref()
            .get_index_of(key)
            .and_then(|i| self.values.get(i).map(|value| value.as_ref()))
    }
}

impl<'g> crate::variant::record::samples::Sample for Sample<'g> {
    fn get<'a, 'h: 'a>(
        &'a self,
        header: &'h Header,
        key: &str,
    ) -> Option<io::Result<Option<crate::variant::record::samples::series::Value<'a>>>> {
        self.keys
            .as_ref()
            .get_index_of(key)
            .and_then(|i| self.get_index(header, i))
    }

    fn get_index<'a, 'h: 'a>(
        &'a self,
        _: &'h Header,
        i: usize,
    ) -> Option<io::Result<Option<crate::variant::record::samples::series::Value<'a>>>> {
        self.values
            .get(i)
            .map(|value| Ok(value.as_ref().map(|v| v.into())))
    }

    fn iter<'a, 'h: 'a>(
        &'a self,
        _: &'h Header,
    ) -> Box<
        dyn Iterator<
                Item = io::Result<(
                    &str,
                    Option<crate::variant::record::samples::series::Value<'a>>,
                )>,
            > + 'a,
    > {
        Box::new(
            self.keys
                .as_ref()
                .iter()
                .zip(self.values)
                .map(|(key, value)| Ok((key.as_ref(), value.as_ref().map(|v| v.into())))),
        )
    }
}