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 { None } else { Some(Self(n)) }
34 }
35
36 pub const fn get(&self) -> u8 {
46 self.0
47 }
48}
49
50#[derive(Clone, Debug, Eq, PartialEq)]
52pub enum TryFromIntError {
53 Missing,
55}
56
57impl error::Error for TryFromIntError {}
58
59impl fmt::Display for TryFromIntError {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61 match self {
62 Self::Missing => write!(f, "missing value: {MISSING}"),
63 }
64 }
65}
66
67impl TryFrom<u8> for MappingQuality {
68 type Error = TryFromIntError;
69
70 fn try_from(n: u8) -> Result<Self, Self::Error> {
71 Self::new(n).ok_or(TryFromIntError::Missing)
72 }
73}
74
75impl From<MappingQuality> for u8 {
76 fn from(mapping_quality: MappingQuality) -> Self {
77 mapping_quality.0
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn test_try_from_u8_for_mapping_quality() {
87 assert_eq!(MappingQuality::try_from(0), Ok(MappingQuality(0)));
88 assert_eq!(MappingQuality::try_from(8), Ok(MappingQuality(8)));
89 assert_eq!(MappingQuality::try_from(13), Ok(MappingQuality(13)));
90 assert_eq!(MappingQuality::try_from(144), Ok(MappingQuality(144)));
91 assert_eq!(MappingQuality::try_from(255), Err(TryFromIntError::Missing));
92 }
93
94 #[test]
95 fn test_from_mapping_quality_for_u8() {
96 assert_eq!(u8::from(MappingQuality(0)), 0);
97 assert_eq!(u8::from(MappingQuality(8)), 8);
98 assert_eq!(u8::from(MappingQuality(13)), 13);
99 assert_eq!(u8::from(MappingQuality(144)), 144);
100 }
101}