use std::{error, fmt};
const MISSING: u8 = 255;
#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
pub struct MappingQuality(u8);
impl MappingQuality {
pub const MIN: Self = Self(0);
pub const MAX: Self = Self(254);
pub const fn new(n: u8) -> Option<Self> {
if n == MISSING { None } else { Some(Self(n)) }
}
pub const fn get(&self) -> u8 {
self.0
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TryFromIntError {
Missing,
}
impl error::Error for TryFromIntError {}
impl fmt::Display for TryFromIntError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Missing => write!(f, "missing value: {MISSING}"),
}
}
}
impl TryFrom<u8> for MappingQuality {
type Error = TryFromIntError;
fn try_from(n: u8) -> Result<Self, Self::Error> {
Self::new(n).ok_or(TryFromIntError::Missing)
}
}
impl From<MappingQuality> for u8 {
fn from(mapping_quality: MappingQuality) -> Self {
mapping_quality.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_try_from_u8_for_mapping_quality() {
assert_eq!(MappingQuality::try_from(0), Ok(MappingQuality(0)));
assert_eq!(MappingQuality::try_from(8), Ok(MappingQuality(8)));
assert_eq!(MappingQuality::try_from(13), Ok(MappingQuality(13)));
assert_eq!(MappingQuality::try_from(144), Ok(MappingQuality(144)));
assert_eq!(MappingQuality::try_from(255), Err(TryFromIntError::Missing));
}
#[test]
fn test_from_mapping_quality_for_u8() {
assert_eq!(u8::from(MappingQuality(0)), 0);
assert_eq!(u8::from(MappingQuality(8)), 8);
assert_eq!(u8::from(MappingQuality(13)), 13);
assert_eq!(u8::from(MappingQuality(144)), 144);
}
}