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
use std::{error, fmt, str::FromStr};
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub enum Tag {
Id,
Barcode,
SequencingCenter,
Description,
ProducedAt,
FlowOrder,
KeySequence,
Library,
Program,
PredictedMedianInsertSize,
Platform,
PlatformModel,
PlatformUnit,
Sample,
Other(String),
}
impl AsRef<str> for Tag {
fn as_ref(&self) -> &str {
match self {
Self::Id => "ID",
Self::Barcode => "BC",
Self::SequencingCenter => "CN",
Self::Description => "DS",
Self::ProducedAt => "DT",
Self::FlowOrder => "FO",
Self::KeySequence => "KS",
Self::Library => "LB",
Self::Program => "PG",
Self::PredictedMedianInsertSize => "PI",
Self::Platform => "PL",
Self::PlatformModel => "PM",
Self::PlatformUnit => "PU",
Self::Sample => "SM",
Self::Other(s) => s,
}
}
}
impl fmt::Display for Tag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_ref())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
Empty,
Invalid,
}
impl error::Error for ParseError {}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => f.write_str("empty input"),
Self::Invalid => f.write_str("invalid input"),
}
}
}
impl FromStr for Tag {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"" => Err(ParseError::Empty),
"ID" => Ok(Self::Id),
"BC" => Ok(Self::Barcode),
"CN" => Ok(Self::SequencingCenter),
"DS" => Ok(Self::Description),
"DT" => Ok(Self::ProducedAt),
"FO" => Ok(Self::FlowOrder),
"KS" => Ok(Self::KeySequence),
"LB" => Ok(Self::Library),
"PG" => Ok(Self::Program),
"PI" => Ok(Self::PredictedMedianInsertSize),
"PL" => Ok(Self::Platform),
"PM" => Ok(Self::PlatformModel),
"PU" => Ok(Self::PlatformUnit),
"SM" => Ok(Self::Sample),
_ => {
if s.len() == 2 {
Ok(Self::Other(s.into()))
} else {
Err(ParseError::Invalid)
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fmt() {
assert_eq!(Tag::Id.to_string(), "ID");
assert_eq!(Tag::Barcode.to_string(), "BC");
assert_eq!(Tag::SequencingCenter.to_string(), "CN");
assert_eq!(Tag::Description.to_string(), "DS");
assert_eq!(Tag::ProducedAt.to_string(), "DT");
assert_eq!(Tag::FlowOrder.to_string(), "FO");
assert_eq!(Tag::KeySequence.to_string(), "KS");
assert_eq!(Tag::Library.to_string(), "LB");
assert_eq!(Tag::Program.to_string(), "PG");
assert_eq!(Tag::PredictedMedianInsertSize.to_string(), "PI");
assert_eq!(Tag::Platform.to_string(), "PL");
assert_eq!(Tag::PlatformModel.to_string(), "PM");
assert_eq!(Tag::PlatformUnit.to_string(), "PU");
assert_eq!(Tag::Sample.to_string(), "SM");
assert_eq!(Tag::Other(String::from("ND")).to_string(), "ND");
}
#[test]
fn test_from_str() {
assert_eq!("ID".parse(), Ok(Tag::Id));
assert_eq!("BC".parse(), Ok(Tag::Barcode));
assert_eq!("CN".parse(), Ok(Tag::SequencingCenter));
assert_eq!("DS".parse(), Ok(Tag::Description));
assert_eq!("DT".parse(), Ok(Tag::ProducedAt));
assert_eq!("FO".parse(), Ok(Tag::FlowOrder));
assert_eq!("KS".parse(), Ok(Tag::KeySequence));
assert_eq!("LB".parse(), Ok(Tag::Library));
assert_eq!("PG".parse(), Ok(Tag::Program));
assert_eq!("PI".parse(), Ok(Tag::PredictedMedianInsertSize));
assert_eq!("PL".parse(), Ok(Tag::Platform));
assert_eq!("PM".parse(), Ok(Tag::PlatformModel));
assert_eq!("PU".parse(), Ok(Tag::PlatformUnit));
assert_eq!("SM".parse(), Ok(Tag::Sample));
assert_eq!("ND".parse(), Ok(Tag::Other(String::from("ND"))));
assert_eq!("".parse::<Tag>(), Err(ParseError::Empty));
assert_eq!("NDL".parse::<Tag>(), Err(ParseError::Invalid));
}
}