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
use std::{convert::TryFrom, mem};
use strum::IntoEnumIterator;
use strum_macros::EnumIter;
use crate::ReadStructureError;
#[non_exhaustive]
#[derive(Debug, Copy, Clone, EnumIter, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(u8)]
pub enum SegmentType {
Template = b'T',
SampleBarcode = b'B',
MolecularBarcode = b'M',
Skip = b'S',
}
impl SegmentType {
pub fn value(&self) -> char {
let value = *self as u8;
value as char
}
}
impl TryFrom<char> for SegmentType {
type Error = ReadStructureError;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'T' => Ok(SegmentType::Template),
'B' => Ok(SegmentType::SampleBarcode),
'M' => Ok(SegmentType::MolecularBarcode),
'S' => Ok(SegmentType::Skip),
_ => Err(ReadStructureError::ReadSegmentTypeInvalid(value)),
}
}
}
impl TryFrom<u8> for SegmentType {
type Error = ReadStructureError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Self::try_from(value as char)
}
}
#[cfg(test)]
mod test {
use std::convert::TryFrom;
use crate::{segment_type::SegmentType, ReadStructureError};
use strum::IntoEnumIterator;
#[test]
fn test_segment_type_round_trip() -> Result<(), ReadStructureError> {
assert_eq!(SegmentType::iter().len(), 4);
for tpe in SegmentType::iter() {
assert_eq!(SegmentType::try_from(tpe.value())?, tpe);
}
Ok(())
}
#[test]
fn test_invalid_segment_type() {
assert!(SegmentType::try_from(b'G').is_err());
}
}