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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
mod ty;
pub use self::ty::Type;
use std::fmt;
use indexmap::IndexMap;
use super::{builder, tag, Described, Fields, Indexed, Inner, Map, TryFromFieldsError, Typed};
use crate::header::{info::Key, FileFormat, Number};
type StandardTag = tag::TypedDescribedIndexed;
type Tag = tag::Tag<StandardTag>;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Info {
number: Number,
ty: Type,
description: String,
idx: Option<usize>,
}
impl Inner for Info {
type StandardTag = StandardTag;
type Builder = builder::TypedDescribedIndexed<Self>;
}
impl Typed for Info {
type Type = Type;
fn number(&self) -> Number {
self.number
}
fn number_mut(&mut self) -> &mut Number {
&mut self.number
}
fn ty(&self) -> Self::Type {
self.ty
}
fn type_mut(&mut self) -> &mut Self::Type {
&mut self.ty
}
}
impl Described for Info {
fn description(&self) -> &str {
&self.description
}
fn description_mut(&mut self) -> &mut String {
&mut self.description
}
}
impl Indexed for Info {
fn idx(&self) -> Option<usize> {
self.idx
}
fn idx_mut(&mut self) -> &mut Option<usize> {
&mut self.idx
}
}
impl Map<Info> {
pub fn new<D>(number: Number, ty: Type, description: D) -> Self
where
D: Into<String>,
{
Self {
inner: Info {
number,
ty,
description: description.into(),
idx: None,
},
other_fields: IndexMap::new(),
}
}
}
impl fmt::Display for Map<Info> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
super::fmt_display_type_fields(f, self.number(), self.ty())?;
super::fmt_display_description_field(f, self.description())?;
super::fmt_display_other_fields(f, self.other_fields())?;
if let Some(idx) = self.idx() {
super::fmt_display_idx_field(f, idx)?;
}
Ok(())
}
}
impl From<&Key> for Map<Info> {
fn from(key: &Key) -> Self {
use crate::header::info::key;
let number = key::number(key).unwrap_or(Number::Count(1));
let ty = key::ty(key).unwrap_or(Type::String);
let description = key::description(key).map(|s| s.into()).unwrap_or_default();
Self {
inner: Info {
number,
ty,
description,
idx: None,
},
other_fields: IndexMap::new(),
}
}
}
impl TryFrom<Fields> for Map<Info> {
type Error = TryFromFieldsError;
fn try_from(fields: Fields) -> Result<Self, Self::Error> {
Self::try_from((FileFormat::default(), fields))
}
}
impl TryFrom<(FileFormat, Fields)> for Map<Info> {
type Error = TryFromFieldsError;
fn try_from((_, fields): (FileFormat, Fields)) -> Result<Self, Self::Error> {
let mut other_fields = super::init_other_fields();
let mut number = None;
let mut ty = None;
let mut description = None;
let mut idx = None;
for (key, value) in fields {
match Tag::from(key) {
Tag::Standard(StandardTag::Id) => return Err(TryFromFieldsError::DuplicateTag),
Tag::Standard(StandardTag::Number) => super::parse_number(&value, &mut number)?,
Tag::Standard(StandardTag::Type) => super::parse_type(&value, &mut ty)?,
Tag::Standard(StandardTag::Description) => {
super::parse_description(value, &mut description)?
}
Tag::Standard(StandardTag::Idx) => super::parse_idx(&value, &mut idx)?,
Tag::Other(t) => super::insert_other_field(&mut other_fields, t, value)?,
}
}
let number = number.ok_or(TryFromFieldsError::MissingField("Number"))?;
let ty = ty.ok_or(TryFromFieldsError::MissingField("Type"))?;
let description = description.ok_or(TryFromFieldsError::MissingField("Description"))?;
Ok(Self {
inner: Info {
number,
ty,
description,
idx,
},
other_fields,
})
}
}
impl builder::Inner<Info> for builder::TypedDescribedIndexed<Info> {
fn build(self) -> Result<Info, builder::BuildError> {
let number = self
.number
.ok_or(builder::BuildError::MissingField("Number"))?;
let ty = self.ty.ok_or(builder::BuildError::MissingField("Type"))?;
let description = self
.description
.ok_or(builder::BuildError::MissingField("Description"))?;
Ok(Info {
number,
ty,
description,
idx: self.idx,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::header::info::key;
#[test]
fn test_fmt() {
let map = Map::<Info>::from(&key::SAMPLES_WITH_DATA_COUNT);
let expected = r#",Number=1,Type=Integer,Description="Number of samples with data""#;
assert_eq!(map.to_string(), expected);
}
#[test]
fn test_try_from_fields_for_map_info() -> Result<(), TryFromFieldsError> {
let actual = Map::<Info>::try_from(vec![
(String::from("Number"), String::from("1")),
(String::from("Type"), String::from("Integer")),
(
String::from("Description"),
String::from("Number of samples with data"),
),
])?;
let expected = Map::<Info>::from(&key::SAMPLES_WITH_DATA_COUNT);
assert_eq!(actual, expected);
Ok(())
}
#[test]
fn test_try_from_fields_for_map_info_with_missing_fields() {
assert_eq!(
Map::<Info>::try_from(vec![
(String::from("Type"), String::from("Integer")),
(
String::from("Description"),
String::from("Number of samples with data")
),
]),
Err(TryFromFieldsError::MissingField("Number"))
);
assert_eq!(
Map::<Info>::try_from(vec![
(String::from("Number"), String::from("1")),
(
String::from("Description"),
String::from("Number of samples with data")
),
]),
Err(TryFromFieldsError::MissingField("Type"))
);
assert_eq!(
Map::<Info>::try_from(vec![
(String::from("Number"), String::from("1")),
(String::from("Type"), String::from("Integer")),
]),
Err(TryFromFieldsError::MissingField("Description"))
);
}
}