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
use std::fmt;
use indexmap::IndexMap;
use super::{builder, tag, Fields, Inner, Map, TryFromFieldsError};
type StandardTag = tag::Identity;
type Tag = tag::Tag<StandardTag>;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Other;
impl Inner for Other {
type Id = String;
type StandardTag = StandardTag;
type Builder = builder::Identity;
}
impl Map<Other> {
pub fn new<I>(id: I) -> Self
where
I: Into<String>,
{
Self {
id: id.into(),
inner: Other,
other_fields: IndexMap::new(),
}
}
}
impl fmt::Display for Map<Other> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
super::fmt_display_prefix(f, self.id())?;
super::fmt_display_other_fields(f, self.other_fields())?;
super::fmt_display_suffix(f)?;
Ok(())
}
}
impl TryFrom<Fields> for Map<Other> {
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;
for (key, value) in fields {
match Tag::from(key) {
Tag::Standard(StandardTag::Id) => super::parse_id(&value, &mut id)?,
Tag::Other(t) => super::insert_other_field(&mut other_fields, t, value)?,
}
}
let id = id.ok_or(TryFromFieldsError::MissingField("ID"))?;
Ok(Self {
id,
inner: Other,
other_fields,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fmt() {
let map = Map::<Other>::new("noodles");
let expected = r#"<ID=noodles>"#;
assert_eq!(map.to_string(), expected);
}
#[test]
fn test_try_from_fields_for_map_other() -> Result<(), TryFromFieldsError> {
let actual = Map::<Other>::try_from(vec![(String::from("ID"), String::from("noodles"))])?;
let expected = Map::<Other>::new("noodles");
assert_eq!(actual, expected);
Ok(())
}
#[test]
fn test_try_from_fields_for_info_with_missing_fields() {
assert_eq!(
Map::<Other>::try_from(Vec::new()),
Err(TryFromFieldsError::MissingField("ID"))
);
}
}