From 7c3ffb7ecfd41393b45fc51a317d61225eb88933 Mon Sep 17 00:00:00 2001 From: Guilhem Date: Mon, 13 Jun 2022 18:47:27 +0200 Subject: [PATCH] :sparkles: Add from_iso & to_iso --- src/lib.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 1fde4cf..f7ee5e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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() + } }