fix: Rename teeth in tooth

This commit is contained in:
Guilhem 2022-07-20 18:04:43 +02:00
parent efc028fb0d
commit 144c56b517

View File

@ -19,34 +19,34 @@ pub enum QuadrantKind {
BottomRight,
}
pub struct Teeth {
pub struct Tooth {
quadrant: QuadrantKind,
number: u8,
permanent: bool,
}
impl Teeth {
pub fn new(number: u8, quadrant: QuadrantKind, permanent: bool) -> Result<Teeth, String> {
if let Err(err) = Teeth::check_teeth_number(number, permanent) {
impl Tooth {
pub fn new(number: u8, quadrant: QuadrantKind, permanent: bool) -> Result<Tooth, String> {
if let Err(err) = Tooth::check_tooth_number(number, permanent) {
return Err(err);
}
Ok(Teeth {
Ok(Tooth {
number,
quadrant,
permanent,
})
}
fn check_teeth_number(number: u8, permanent: bool) -> Result<(), String> {
fn check_tooth_number(number: u8, permanent: bool) -> Result<(), String> {
if permanent && (number < 1 || number > 8) {
Err(format!(
"Permanent teeth {} should be in range [1; 8]",
"Permanent tooth {} should be in range [1; 8]",
number
))
} else if !permanent && (number < 1 || number > 5) {
Err(format!(
"Primary teeth {} should be in range [1; 5]",
"Primary tooth {} should be in range [1; 5]",
number
))
} else {
@ -79,17 +79,17 @@ impl Teeth {
x => return Err(format!("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);
let tooth_string = char_iter.next().unwrap();
let tooth_number_option = tooth_string.to_digit(10);
if teeth_number_option.is_none() {
return Err(format!("{} is not a number", teeth_string));
if tooth_number_option.is_none() {
return Err(format!("{} is not a number", tooth_string));
}
let number = teeth_number_option.unwrap() as u8;
if let Err(err) = Teeth::check_teeth_number(number, permanent) {
let number = tooth_number_option.unwrap() as u8;
if let Err(err) = Tooth::check_tooth_number(number, permanent) {
return Err(err);
}
Ok(Teeth {
Ok(Tooth {
quadrant,
permanent,
number,
@ -122,29 +122,29 @@ impl Teeth {
if permanent && (uns < 1 || uns > 32) {
return Err(format!(
"UNS permanent teeth has to be in range [1; 32] (currently {})",
"UNS permanent tooth has to be in range [1; 32] (currently {})",
uns
));
} else if !permanent && (uns < 1 || uns > 20) {
return Err(format!("UNS primary teeth has to be in range [A; T]"));
return Err(format!("UNS primary tooth has to be in range [A; T]"));
}
let max = Teeth::quadrant_max(permanent);
let max = Tooth::quadrant_max(permanent);
let quadrant = match ((uns - 1) / max) + 1 {
1 => QuadrantKind::TopLeft,
2 => QuadrantKind::TopRight,
3 => QuadrantKind::BottomRight,
4 => QuadrantKind::BottomLeft,
_ => return Err(format!("UNS teeth value not in range")),
_ => return Err(format!("UNS tooth value not in range")),
};
let number = if quadrant == QuadrantKind::TopRight || quadrant == QuadrantKind::BottomLeft {
((uns - 1) % max) + 1
} else {
max - ((uns - 1) % max)
};
if let Err(err) = Teeth::check_teeth_number(number, permanent) {
if let Err(err) = Tooth::check_tooth_number(number, permanent) {
return Err(err);
}
Ok(Teeth {
Ok(Tooth {
quadrant,
number,
permanent,
@ -152,7 +152,7 @@ impl Teeth {
}
pub fn to_uns(&self) -> String {
let max = Teeth::quadrant_max(self.permanent);
let max = Tooth::quadrant_max(self.permanent);
let value: u8 = match (&self.quadrant, self.number) {
(QuadrantKind::TopLeft, x) => max - x + 1,
(QuadrantKind::TopRight, x) => x + max,
@ -187,9 +187,9 @@ impl Teeth {
}
};
let teeth_string = &value[2..3];
let tooth_string = &value[2..3];
let (number, permanent) = match teeth_string {
let (number, permanent) = match tooth_string {
"1" => (1, true),
"2" => (2, true),
"3" => (3, true),
@ -210,10 +210,10 @@ impl Teeth {
))
}
};
if let Err(err) = Teeth::check_teeth_number(number, permanent) {
if let Err(err) = Tooth::check_tooth_number(number, permanent) {
return Err(err);
}
Ok(Teeth {
Ok(Tooth {
quadrant,
permanent,
number,
@ -245,12 +245,12 @@ mod test {
($name:ident, $func:ident, $quadrant:expr,$number:expr, $permanent:expr, $value:expr) => {
#[test]
fn $name() {
let teeth = Teeth {
let tooth = Tooth {
quadrant: $quadrant,
number: $number,
permanent: $permanent,
};
assert_eq!(teeth.$func(), $value);
assert_eq!(tooth.$func(), $value);
}
};
}
@ -259,11 +259,11 @@ mod test {
($name:ident, $func:ident, $value:expr, $quadrant:expr,$number:expr, $permanent:expr) => {
#[test]
fn $name() {
let teeth = Teeth::$func($value).unwrap();
let tooth = Tooth::$func($value).unwrap();
assert_eq!(teeth.quadrant, $quadrant);
assert_eq!(teeth.number, $number);
assert_eq!(teeth.permanent, $permanent);
assert_eq!(tooth.quadrant, $quadrant);
assert_eq!(tooth.number, $number);
assert_eq!(tooth.permanent, $permanent);
}
};
}
@ -272,7 +272,7 @@ mod test {
($name:ident, $func:ident, $value:expr) => {
#[test]
fn $name() {
assert!(Teeth::$func($value).is_err());
assert!(Tooth::$func($value).is_err());
}
};
}