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
mod builder;
pub mod name;
mod tag;
pub use self::name::Name;
use std::fmt;
use indexmap::IndexMap;
use super::{Fields, Indexed, Inner, Map, TryFromFieldsError};
type StandardTag = tag::Standard;
type Tag = super::tag::Tag<StandardTag>;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Contig {
length: Option<usize>,
idx: Option<usize>,
}
impl Inner for Contig {
type Id = Name;
type StandardTag = StandardTag;
type Builder = builder::Builder;
}
impl Indexed for Contig {
fn idx(&self) -> Option<usize> {
self.idx
}
fn idx_mut(&mut self) -> &mut Option<usize> {
&mut self.idx
}
}
impl Map<Contig> {
pub fn new(id: Name) -> Self {
Self {
id,
inner: Contig {
length: None,
idx: None,
},
other_fields: IndexMap::new(),
}
}
pub fn length(&self) -> Option<usize> {
self.inner.length
}
pub fn length_mut(&mut self) -> &mut Option<usize> {
&mut self.inner.length
}
}
impl fmt::Display for Map<Contig> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
super::fmt_display_prefix(f, self.id())?;
if let Some(length) = self.length() {
write!(f, ",length={}", length)?;
}
super::fmt_display_other_fields(f, self.other_fields())?;
if let Some(idx) = self.idx() {
super::fmt_display_idx_field(f, idx)?;
}
super::fmt_display_suffix(f)?;
Ok(())
}
}
impl TryFrom<Fields> for Map<Contig> {
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 length = None;
let mut idx = None;
for (key, value) in fields {
match Tag::from(key) {
Tag::Standard(StandardTag::Id) => super::parse_id(&value, &mut id)?,
Tag::Standard(StandardTag::Length) => parse_length(&value, &mut length)?,
Tag::Standard(StandardTag::Idx) => super::parse_idx(&value, &mut idx)?,
Tag::Other(t) => super::insert_other_field(&mut other_fields, t, value)?,
}
}
let id = id.ok_or(TryFromFieldsError::MissingField("ID"))?;
Ok(Self {
id,
inner: Contig { length, idx },
other_fields,
})
}
}
fn parse_length(s: &str, value: &mut Option<usize>) -> Result<(), TryFromFieldsError> {
let n = s
.parse()
.map_err(|_| TryFromFieldsError::InvalidValue("length"))?;
if value.replace(n).is_none() {
Ok(())
} else {
Err(TryFromFieldsError::DuplicateTag)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fmt() -> Result<(), TryFromFieldsError> {
let map = Map::<Contig>::try_from(vec![
(String::from("ID"), String::from("sq0")),
(String::from("length"), String::from("8")),
(
String::from("md5"),
String::from("d7eba311421bbc9d3ada44709dd61534"),
),
])?;
let expected = r#"<ID=sq0,length=8,md5="d7eba311421bbc9d3ada44709dd61534">"#;
assert_eq!(map.to_string(), expected);
Ok(())
}
#[test]
fn test_try_from_fields_for_map_contig() -> Result<(), Box<dyn std::error::Error>> {
let actual = Map::<Contig>::try_from(vec![(String::from("ID"), String::from("sq0"))])?;
let expected = Map::<Contig>::new("sq0".parse()?);
assert_eq!(actual, expected);
Ok(())
}
#[test]
fn test_try_from_fields_for_map_contig_with_missing_fields() {
assert_eq!(
Map::<Contig>::try_from(Vec::new()),
Err(TryFromFieldsError::MissingField("ID")),
);
}
#[test]
fn test_parse_length() -> Result<(), TryFromFieldsError> {
let mut length = None;
parse_length("8", &mut length)?;
assert_eq!(length, Some(8));
assert_eq!(
parse_length("eight", &mut None),
Err(TryFromFieldsError::InvalidValue("length"))
);
Ok(())
}
}