feat: Add from_alphanumeric & to_alphanumeric

This commit is contained in:
Guilhem 2022-06-15 19:45:04 +02:00
parent b7b69883ce
commit 68c4dbaf04

View File

@ -44,6 +44,14 @@ impl Teeth {
} }
} }
fn quadrant_max(permanent: bool) -> u8 {
if permanent {
8
} else {
5
}
}
pub fn from_iso(value: &str) -> Self { pub fn from_iso(value: &str) -> Self {
if value.len() != 2 { if value.len() != 2 {
panic!("{} is not a valid ISO dental notation", value); panic!("{} is not a valid ISO dental notation", value);
@ -145,55 +153,67 @@ impl Teeth {
} }
} }
fn quadrant_max(permanent: bool) -> u8 { pub fn from_alphanumeric(value: &str) -> Self {
if permanent { if value.len() != 3 {
8 panic!("{} is not a valid alphanumeric dental notation", value);
} else {
5
} }
let quadrant = match &value[0..2] {
"UL" => QuadrantKind::TopLeft,
"UR" => QuadrantKind::TopRight,
"LR" => QuadrantKind::BottomRight,
"LL" => QuadrantKind::BottomLeft,
x => panic!(
"Quadrant value {} not a valid value (accepted: ['UL', 'UR', 'LR', 'LL'])",
x
),
};
let teeth_string = &value[2..3];
let (number, permanent) = match teeth_string {
"1" => (1, true),
"2" => (2, true),
"3" => (3, true),
"4" => (4, true),
"5" => (5, true),
"6" => (6, true),
"7" => (7, true),
"8" => (8, true),
"A" => (1, false),
"B" => (2, false),
"C" => (3, false),
"D" => (4, false),
"E" => (5, false),
x => panic!(
"Number value {} not a valid value (accepted: [1; 8] and ['A'; 'E'])",
x
),
};
Teeth::check_teeth_number(number, permanent);
Teeth {
quadrant,
permanent,
number,
}
}
pub fn to_alphanumeric(&self) -> String {
let quadrant = match &self.quadrant {
QuadrantKind::TopLeft => "UL",
QuadrantKind::TopRight => "UR",
QuadrantKind::BottomRight => "LR",
QuadrantKind::BottomLeft => "LL",
};
let number = if self.permanent {
self.number.to_string()
} else {
((self.number + 64) as char).to_string()
};
quadrant.to_owned() + &number
} }
} }
#[cfg(test)]
mod test {
use super::*;
macro_rules! to_iso {
($name:ident, $quadrant:expr,$number:expr, $permanent:expr, $iso:expr) => {
#[test]
fn $name() {
let teeth = Teeth {
quadrant: $quadrant,
number: $number,
permanent: $permanent,
};
assert_eq!(teeth.to_iso(), $iso);
}
};
}
macro_rules! from_iso {
($name:ident, $iso:expr, $quadrant:expr,$number:expr, $permanent:expr) => {
#[test]
fn $name() {
let teeth = Teeth::from_iso($iso);
assert_eq!(teeth.quadrant, $quadrant);
assert_eq!(teeth.number, $number);
assert_eq!(teeth.permanent, $permanent);
}
};
}
macro_rules! from_iso_fail {
($name:ident, $iso:expr) => {
#[test]
#[should_panic]
fn $name() {
Teeth::from_iso($iso);
}
};
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
@ -449,4 +469,277 @@ mod test {
from_fail!(from_uns_fail_33, from_uns, "33"); from_fail!(from_uns_fail_33, from_uns, "33");
from_fail!(from_uns_fail_at, from_uns, "@"); from_fail!(from_uns_fail_at, from_uns, "@");
from_fail!(from_uns_fail_u, from_uns, "U"); from_fail!(from_uns_fail_u, from_uns, "U");
to!(
to_alphanumeric_ul1,
to_alphanumeric,
QuadrantKind::TopLeft,
1,
true,
"UL1"
);
to!(
to_alphanumeric_ul8,
to_alphanumeric,
QuadrantKind::TopLeft,
8,
true,
"UL8"
);
to!(
to_alphanumeric_ur1,
to_alphanumeric,
QuadrantKind::TopRight,
1,
true,
"UR1"
);
to!(
to_alphanumeric_ur8,
to_alphanumeric,
QuadrantKind::TopRight,
8,
true,
"UR8"
);
to!(
to_alphanumeric_lr1,
to_alphanumeric,
QuadrantKind::BottomRight,
1,
true,
"LR1"
);
to!(
to_alphanumeric_lr8,
to_alphanumeric,
QuadrantKind::BottomRight,
8,
true,
"LR8"
);
to!(
to_alphanumeric_ll1,
to_alphanumeric,
QuadrantKind::BottomLeft,
1,
true,
"LL1"
);
to!(
to_alphanumeric_ll8,
to_alphanumeric,
QuadrantKind::BottomLeft,
8,
true,
"LL8"
);
to!(
to_alphanumeric_ula,
to_alphanumeric,
QuadrantKind::TopLeft,
1,
false,
"ULA"
);
to!(
to_alphanumeric_ule,
to_alphanumeric,
QuadrantKind::TopLeft,
5,
false,
"ULE"
);
to!(
to_alphanumeric_ura,
to_alphanumeric,
QuadrantKind::TopRight,
1,
false,
"URA"
);
to!(
to_alphanumeric_ure,
to_alphanumeric,
QuadrantKind::TopRight,
5,
false,
"URE"
);
to!(
to_alphanumeric_lra,
to_alphanumeric,
QuadrantKind::BottomRight,
1,
false,
"LRA"
);
to!(
to_alphanumeric_lre,
to_alphanumeric,
QuadrantKind::BottomRight,
5,
false,
"LRE"
);
to!(
to_alphanumeric_lla,
to_alphanumeric,
QuadrantKind::BottomLeft,
1,
false,
"LLA"
);
to!(
to_alphanumeric_lle,
to_alphanumeric,
QuadrantKind::BottomLeft,
5,
false,
"LLE"
);
from!(
from_alphanumeric_ul1,
from_alphanumeric,
"UL1",
QuadrantKind::TopLeft,
1,
true
);
from!(
from_alphanumeric_ul8,
from_alphanumeric,
"UL8",
QuadrantKind::TopLeft,
8,
true
);
from!(
from_alphanumeric_ur1,
from_alphanumeric,
"UR1",
QuadrantKind::TopRight,
1,
true
);
from!(
from_alphanumeric_ur8,
from_alphanumeric,
"UR8",
QuadrantKind::TopRight,
8,
true
);
from!(
from_alphanumeric_lr1,
from_alphanumeric,
"LR1",
QuadrantKind::BottomRight,
1,
true
);
from!(
from_alphanumeric_lr8,
from_alphanumeric,
"LR8",
QuadrantKind::BottomRight,
8,
true
);
from!(
from_alphanumeric_ll1,
from_alphanumeric,
"LL1",
QuadrantKind::BottomLeft,
1,
true
);
from!(
from_alphanumeric_ll8,
from_alphanumeric,
"LL8",
QuadrantKind::BottomLeft,
8,
true
);
from!(
from_alphanumeric_ula,
from_alphanumeric,
"ULA",
QuadrantKind::TopLeft,
1,
false
);
from!(
from_alphanumeric_ule,
from_alphanumeric,
"ULE",
QuadrantKind::TopLeft,
5,
false
);
from!(
from_alphanumeric_ura,
from_alphanumeric,
"URA",
QuadrantKind::TopRight,
1,
false
);
from!(
from_alphanumeric_ure,
from_alphanumeric,
"URE",
QuadrantKind::TopRight,
5,
false
);
from!(
from_alphanumeric_lra,
from_alphanumeric,
"LRA",
QuadrantKind::BottomRight,
1,
false
);
from!(
from_alphanumeric_lre,
from_alphanumeric,
"LRE",
QuadrantKind::BottomRight,
5,
false
);
from!(
from_alphanumeric_lla,
from_alphanumeric,
"LLA",
QuadrantKind::BottomLeft,
1,
false
);
from!(
from_alphanumeric_lle,
from_alphanumeric,
"LLE",
QuadrantKind::BottomLeft,
5,
false
);
from_fail!(from_alphanumeric_fail_ul0, from_alphanumeric, "UL0");
from_fail!(from_alphanumeric_fail_ul9, from_alphanumeric, "UL9");
from_fail!(from_alphanumeric_fail_ur0, from_alphanumeric, "UR0");
from_fail!(from_alphanumeric_fail_ur9, from_alphanumeric, "UR9");
from_fail!(from_alphanumeric_fail_lr0, from_alphanumeric, "LR0");
from_fail!(from_alphanumeric_fail_lr9, from_alphanumeric, "LR9");
from_fail!(from_alphanumeric_fail_ll0, from_alphanumeric, "LL0");
from_fail!(from_alphanumeric_fail_ll9, from_alphanumeric, "LL9");
from_fail!(from_alphanumeric_fail_ulat, from_alphanumeric, "UL@");
from_fail!(from_alphanumeric_fail_ulf, from_alphanumeric, "ULF");
from_fail!(from_alphanumeric_fail_urat, from_alphanumeric, "UR@");
from_fail!(from_alphanumeric_fail_urf, from_alphanumeric, "URF");
from_fail!(from_alphanumeric_fail_lrat, from_alphanumeric, "LR@");
from_fail!(from_alphanumeric_fail_lrf, from_alphanumeric, "LRF");
from_fail!(from_alphanumeric_fail_llat, from_alphanumeric, "LL@");
from_fail!(from_alphanumeric_fail_llf, from_alphanumeric, "LLF");
} }