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
use std::{error, fmt, str::FromStr};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Kind {
Match,
Insertion,
Deletion,
Skip,
SoftClip,
HardClip,
Pad,
SeqMatch,
SeqMismatch,
}
impl fmt::Display for Kind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", char::from(*self))
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParseError(String);
impl error::Error for ParseError {}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl FromStr for Kind {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"M" => Ok(Self::Match),
"I" => Ok(Self::Insertion),
"D" => Ok(Self::Deletion),
"N" => Ok(Self::Skip),
"S" => Ok(Self::SoftClip),
"H" => Ok(Self::HardClip),
"P" => Ok(Self::Pad),
"=" => Ok(Self::SeqMatch),
"X" => Ok(Self::SeqMismatch),
_ => Err(ParseError(s.into())),
}
}
}
impl From<Kind> for char {
fn from(kind: Kind) -> Self {
match kind {
Kind::Match => 'M',
Kind::Insertion => 'I',
Kind::Deletion => 'D',
Kind::Skip => 'N',
Kind::SoftClip => 'S',
Kind::HardClip => 'H',
Kind::Pad => 'P',
Kind::SeqMatch => '=',
Kind::SeqMismatch => 'X',
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fmt() {
assert_eq!(Kind::Match.to_string(), "M");
assert_eq!(Kind::Insertion.to_string(), "I");
assert_eq!(Kind::Deletion.to_string(), "D");
assert_eq!(Kind::Skip.to_string(), "N");
assert_eq!(Kind::SoftClip.to_string(), "S");
assert_eq!(Kind::HardClip.to_string(), "H");
assert_eq!(Kind::Pad.to_string(), "P");
assert_eq!(Kind::SeqMatch.to_string(), "=");
assert_eq!(Kind::SeqMismatch.to_string(), "X");
}
#[test]
fn test_from_str() {
assert_eq!("M".parse(), Ok(Kind::Match));
assert_eq!("I".parse(), Ok(Kind::Insertion));
assert_eq!("D".parse(), Ok(Kind::Deletion));
assert_eq!("N".parse(), Ok(Kind::Skip));
assert_eq!("S".parse(), Ok(Kind::SoftClip));
assert_eq!("H".parse(), Ok(Kind::HardClip));
assert_eq!("P".parse(), Ok(Kind::Pad));
assert_eq!("=".parse(), Ok(Kind::SeqMatch));
assert_eq!("X".parse(), Ok(Kind::SeqMismatch));
assert_eq!("".parse::<Kind>(), Err(ParseError(String::from(""))));
assert_eq!("O".parse::<Kind>(), Err(ParseError(String::from("O"))));
}
#[test]
fn test_from_kind_for_char() {
assert_eq!(char::from(Kind::Match), 'M');
assert_eq!(char::from(Kind::Insertion), 'I');
assert_eq!(char::from(Kind::Deletion), 'D');
assert_eq!(char::from(Kind::Skip), 'N');
assert_eq!(char::from(Kind::SoftClip), 'S');
assert_eq!(char::from(Kind::HardClip), 'H');
assert_eq!(char::from(Kind::Pad), 'P');
assert_eq!(char::from(Kind::SeqMatch), '=');
assert_eq!(char::from(Kind::SeqMismatch), 'X');
}
}