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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
mod builder;

use std::fmt;

use indexmap::IndexMap;

use super::{tag, Described, Fields, Inner, Map, TryFromFieldsError};
use crate::record::alternate_bases::allele::Symbol;

type StandardTag = tag::Described;
type Tag = tag::Tag<StandardTag>;

/// An inner VCF header alternative allele map value.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AlternativeAllele {
    description: String,
}

impl Inner for AlternativeAllele {
    type Id = Symbol;
    type StandardTag = StandardTag;
    type Builder = builder::Builder;
}

impl Described for AlternativeAllele {
    fn description(&self) -> &str {
        &self.description
    }

    fn description_mut(&mut self) -> &mut String {
        &mut self.description
    }
}

impl Map<AlternativeAllele> {
    /// Creates a VCF header alternative allele map value.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_vcf::{
    ///     header::record::value::{map::AlternativeAllele, Map},
    ///     record::alternate_bases::allele::{
    ///         symbol::{structural_variant::Type, StructuralVariant},
    ///         Symbol,
    ///     },
    /// };
    ///
    /// let map = Map::<AlternativeAllele>::new(
    ///     Symbol::StructuralVariant(StructuralVariant::from(Type::Deletion)),
    ///     "Deletion",
    /// );
    /// ```
    pub fn new<D>(id: Symbol, description: D) -> Self
    where
        D: Into<String>,
    {
        Self {
            id,
            inner: AlternativeAllele {
                description: description.into(),
            },
            other_fields: IndexMap::new(),
        }
    }
}

impl fmt::Display for Map<AlternativeAllele> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        super::fmt_display_prefix(f, self.id())?;
        super::fmt_display_description_field(f, self.description())?;
        super::fmt_display_other_fields(f, self.other_fields())?;
        super::fmt_display_suffix(f)?;
        Ok(())
    }
}

impl TryFrom<Fields> for Map<AlternativeAllele> {
    type Error = TryFromFieldsError;

    fn try_from(fields: Fields) -> Result<Self, Self::Error> {
        let mut other_fields = super::init_other_fields(fields.len());

        let mut id = None;
        let mut description = None;

        for (key, value) in fields {
            match Tag::from(key) {
                Tag::Standard(StandardTag::Id) => super::parse_id(&value, &mut id)?,
                Tag::Standard(StandardTag::Description) => {
                    super::parse_description(value, &mut description)?
                }
                Tag::Other(t) => super::insert_other_field(&mut other_fields, t, value)?,
            }
        }

        let id = id.ok_or(TryFromFieldsError::MissingField("ID"))?;
        let description = description.ok_or(TryFromFieldsError::MissingField("Description"))?;

        Ok(Self {
            id,
            inner: AlternativeAllele { description },
            other_fields,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn del() -> Symbol {
        use crate::record::alternate_bases::allele::symbol::{
            structural_variant::Type, StructuralVariant,
        };

        Symbol::StructuralVariant(StructuralVariant::from(Type::Deletion))
    }

    #[test]
    fn test_fmt() {
        let map = Map::<AlternativeAllele>::new(del(), "Deletion");
        let expected = r#"<ID=DEL,Description="Deletion">"#;
        assert_eq!(map.to_string(), expected);
    }

    #[test]
    fn test_try_from_fields_for_map_alternative_allele() -> Result<(), TryFromFieldsError> {
        let actual = Map::<AlternativeAllele>::try_from(vec![
            (String::from("ID"), String::from("DEL")),
            (String::from("Description"), String::from("Deletion")),
        ])?;

        let expected = Map::<AlternativeAllele>::new(del(), "Deletion");

        assert_eq!(actual, expected);

        Ok(())
    }

    #[test]
    fn test_try_from_fields_for_map_alternative_allele_with_missing_fields() {
        assert_eq!(
            Map::<AlternativeAllele>::try_from(vec![(
                String::from("Description"),
                String::from("Deletion")
            ),]),
            Err(TryFromFieldsError::MissingField("ID")),
        );

        assert_eq!(
            Map::<AlternativeAllele>::try_from(vec![(String::from("ID"), String::from("DEL")),]),
            Err(TryFromFieldsError::MissingField("Description")),
        );
    }
}