noodles_sam/alignment/record/
mapping_quality.rs1use std::{error, fmt};
4
5const MISSING: u8 = 255;
7
8#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
14pub struct MappingQuality(u8);
15
16impl MappingQuality {
17 pub const MIN: Self = Self(0);
19
20 pub const MAX: Self = Self(254);
22
23 pub const fn new(n: u8) -> Option<Self> {
33 if n == MISSING {
34 None
35 } else {
36 Some(Self(n))
37 }
38 }
39
40 pub const fn get(&self) -> u8 {
50 self.0
51 }
52}
53
54#[derive(Clone, Debug, Eq, PartialEq)]
56pub enum TryFromIntError {
57 Missing,
59}
60
61impl error::Error for TryFromIntError {}
62
63impl fmt::Display for TryFromIntError {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 match self {
66 Self::Missing => write!(f, "missing value: {MISSING}"),
67 }
68 }
69}
70
71impl TryFrom<u8> for MappingQuality {
72 type Error = TryFromIntError;
73
74 fn try_from(n: u8) -> Result<Self, Self::Error> {
75 Self::new(n).ok_or(TryFromIntError::Missing)
76 }
77}
78
79impl From<MappingQuality> for u8 {
80 fn from(mapping_quality: MappingQuality) -> Self {
81 mapping_quality.0
82 }
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88
89 #[test]
90 fn test_try_from_u8_for_mapping_quality() {
91 assert_eq!(MappingQuality::try_from(0), Ok(MappingQuality(0)));
92 assert_eq!(MappingQuality::try_from(8), Ok(MappingQuality(8)));
93 assert_eq!(MappingQuality::try_from(13), Ok(MappingQuality(13)));
94 assert_eq!(MappingQuality::try_from(144), Ok(MappingQuality(144)));
95 assert_eq!(MappingQuality::try_from(255), Err(TryFromIntError::Missing));
96 }
97
98 #[test]
99 fn test_from_mapping_quality_for_u8() {
100 assert_eq!(u8::from(MappingQuality(0)), 0);
101 assert_eq!(u8::from(MappingQuality(8)), 8);
102 assert_eq!(u8::from(MappingQuality(13)), 13);
103 assert_eq!(u8::from(MappingQuality(144)), 144);
104 }
105}