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
mod builder;
use std::fmt;
use indexmap::IndexMap;
use super::{tag, Described, Fields, Inner, Map, TryFromFieldsError};
type StandardTag = tag::Described;
type Tag = tag::Tag<StandardTag>;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AlternativeAllele {
description: String,
}
impl Inner for AlternativeAllele {
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> {
pub fn new<D>(description: D) -> Self
where
D: Into<String>,
{
Self {
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_description_field(f, self.description())?;
super::fmt_display_other_fields(f, self.other_fields())?;
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();
let mut description = None;
for (key, value) in fields {
match Tag::from(key) {
Tag::Standard(StandardTag::Id) => return Err(TryFromFieldsError::DuplicateTag),
Tag::Standard(StandardTag::Description) => {
super::parse_description(value, &mut description)?
}
Tag::Other(t) => super::insert_other_field(&mut other_fields, t, value)?,
}
}
let description = description.ok_or(TryFromFieldsError::MissingField("Description"))?;
Ok(Self {
inner: AlternativeAllele { description },
other_fields,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fmt() {
let map = Map::<AlternativeAllele>::new("Deletion");
let expected = r#",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("Description"),
String::from("Deletion"),
)])?;
let expected = Map::<AlternativeAllele>::new("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("Other"),
String::from("noodles")
),]),
Err(TryFromFieldsError::MissingField("Description")),
);
}
}