Add from_iso & to_iso

This commit is contained in:
Guilhem 2022-06-13 18:47:27 +02:00
parent 2ae63e33b5
commit 7c3ffb7ecf

View File

@ -38,4 +38,57 @@ impl Teeth {
}
}
fn check_teeth_number(number: u8, permanent: bool) {
if permanent && number > 8 {
panic!("Permanent teeth {} should be in range [1; 8]", number)
} else if !permanent && number > 5 {
panic!("Primary teeth {} should be in range [1; 5]", number)
}
}
pub fn from_iso(value: &str) -> Self {
if value.len() != 2 {
panic!("{} is not a valid ISO dental notation", value);
}
let mut char_iter = value.chars();
let (quadrant, permanent) = match char_iter.next().unwrap() {
'1' => (QuadrantKind::TopLeft, true),
'2' => (QuadrantKind::TopRight, true),
'3' => (QuadrantKind::BottomRight, true),
'4' => (QuadrantKind::BottomLeft, true),
'5' => (QuadrantKind::TopLeft, false),
'6' => (QuadrantKind::TopRight, false),
'7' => (QuadrantKind::BottomRight, false),
'8' => (QuadrantKind::BottomLeft, false),
x => panic!("Quadrant value {} not included in range [1; 8]", x),
};
let teeth_string = char_iter.next().unwrap();
let teeth_number_option = teeth_string.to_digit(10);
if teeth_number_option.is_none() {
panic!("{} is not a number", teeth_string)
}
let number = teeth_number_option.unwrap() as u8;
Teeth::check_teeth_number(number, permanent);
Teeth {
quadrant,
permanent,
number,
}
}
pub fn to_iso(&self) -> String {
let quadrant_number: u8 = match (&self.quadrant, self.permanent) {
(QuadrantKind::TopLeft, true) => 1,
(QuadrantKind::TopRight, true) => 2,
(QuadrantKind::BottomLeft, true) => 4,
(QuadrantKind::BottomRight, true) => 3,
(QuadrantKind::TopLeft, false) => 5,
(QuadrantKind::TopRight, false) => 6,
(QuadrantKind::BottomLeft, false) => 8,
(QuadrantKind::BottomRight, false) => 7,
};
quadrant_number.to_string() + &self.number.to_string()
}
}