Compare commits

..

7 Commits

Author SHA1 Message Date
3e5433a886 feat: Run cli when running cargo run 2022-07-26 18:23:54 +02:00
336a7c651b feat: Add convert cli 2022-07-26 18:23:41 +02:00
51f2641804 feat: Add convert on Tooth 2022-07-26 18:23:03 +02:00
144c56b517 fix: Rename teeth in tooth 2022-07-20 18:04:43 +02:00
efc028fb0d fix: Fix to_uns and associated tests 2022-07-19 19:10:40 +02:00
6782552379 build: Add clap as dep 2022-07-19 18:43:09 +02:00
869286ef9b feat: Replace panic! by Result 2022-06-17 18:19:20 +02:00
5 changed files with 390 additions and 72 deletions

231
Cargo.lock generated
View File

@ -2,6 +2,237 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "clap"
version = "3.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d53da17d37dba964b9b3ecb5c5a1f193a2762c700e6829201e645b9381c99dc7"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"once_cell",
"strsim",
"termcolor",
"textwrap",
]
[[package]]
name = "clap_derive"
version = "3.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c11d40217d16aee8508cc8e5fde8b4ff24639758608e5374e731b53f85749fb9"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5538cd660450ebeb4234cfecf8f2284b844ffc4c50531e66d584ad5b91293613"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "dental-notation"
version = "0.1.0"
dependencies = [
"clap",
]
[[package]]
name = "hashbrown"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db0d4cf898abf0081f964436dc980e96670a0f36863e4b83aaacdb65c9d7ccc3"
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "indexmap"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c6392766afd7964e2531940894cffe4bd8d7d17dbc3c1c4857040fd4b33bdb3"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "libc"
version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
[[package]]
name = "once_cell"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7709cef83f0c1f58f666e746a08b21e0085f7440fa6a29cc194d68aac97a4225"
[[package]]
name = "os_str_bytes"
version = "6.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21326818e99cfe6ce1e524c2a805c189a99b5ae555a35d19f9a284b427d86afa"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1"
dependencies = [
"proc-macro2",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0748dd251e24453cb8717f0354206b91557e4ec8703673a4b30208f2abaf1ebf"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
[[package]]
name = "unicode-ident"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021"
[dependencies]
clap = {version = "3.2.5", features = [ "derive" ] }

50
src/cli.rs Normal file
View File

@ -0,0 +1,50 @@
use crate::lib::NotationKind;
use crate::lib::Tooth;
use clap::{Parser, Subcommand, ValueEnum};
#[derive(Copy, Clone, Debug, ValueEnum)]
enum NotationKindArg {
Iso,
Uns,
Alphanumeric,
}
#[derive(Debug, Subcommand)]
enum Action {
Convert {
#[clap(value_enum, short = 'f', long)]
from: NotationKindArg,
#[clap(value_enum, short = 't', long)]
to: NotationKindArg,
#[clap(value_parser)]
value: String,
},
}
#[derive(Parser, Debug)]
struct Args {
#[clap(subcommand)]
action: Action,
}
fn convert_kind(kind_arg: &NotationKindArg) -> NotationKind {
match kind_arg {
NotationKindArg::Iso => NotationKind::Iso,
NotationKindArg::Uns => NotationKind::Uns,
NotationKindArg::Alphanumeric => NotationKind::Alphanumeric,
}
}
pub fn run_cli() {
let args = Args::parse();
match &args.action {
Action::Convert { from, to, value } => {
let tooth_result = Tooth::convert(&convert_kind(from), &convert_kind(&to), value);
let output_string = match tooth_result {
Err(err) => err,
Ok(ok) => ok,
};
println!("{}", output_string);
}
};
}

View File

@ -1,16 +1,10 @@
#[derive(Copy, Clone)]
pub enum NotationKind {
Iso,
Palmer,
Uns,
Alphanumeric,
}
pub enum TeethKind {
Canine,
Incisor,
Premolar,
Molar,
}
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum QuadrantKind {
TopLeft,
@ -19,28 +13,38 @@ 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) -> Teeth {
Teeth::check_teeth_number(number, permanent);
impl Tooth {
fn _new(number: u8, quadrant: QuadrantKind, permanent: bool) -> Result<Tooth, String> {
if let Err(err) = Tooth::check_tooth_number(number, permanent) {
return Err(err);
}
Teeth {
Ok(Tooth {
number,
quadrant,
permanent,
}
})
}
fn check_teeth_number(number: u8, permanent: bool) {
fn check_tooth_number(number: u8, permanent: bool) -> Result<(), String> {
if permanent && (number < 1 || number > 8) {
panic!("Permanent teeth {} should be in range [1; 8]", number)
Err(format!(
"Permanent tooth {} should be in range [1; 8]",
number
))
} else if !permanent && (number < 1 || number > 5) {
panic!("Primary teeth {} should be in range [1; 5]", number)
Err(format!(
"Primary tooth {} should be in range [1; 5]",
number
))
} else {
Ok(())
}
}
@ -52,9 +56,9 @@ impl Teeth {
}
}
pub fn from_iso(value: &str) -> Self {
pub fn from_iso(value: &str) -> Result<Self, String> {
if value.len() != 2 {
panic!("{} is not a valid ISO dental notation", value);
return Err(format!("{} is not a valid ISO dental notation", value));
}
let mut char_iter = value.chars();
let (quadrant, permanent) = match char_iter.next().unwrap() {
@ -66,22 +70,24 @@ impl Teeth {
'6' => (QuadrantKind::TopRight, false),
'7' => (QuadrantKind::BottomRight, false),
'8' => (QuadrantKind::BottomLeft, false),
x => panic!("Quadrant value {} not included in range [1; 8]", x),
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() {
panic!("{} 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;
Teeth::check_teeth_number(number, permanent);
Teeth {
let number = tooth_number_option.unwrap() as u8;
if let Err(err) = Tooth::check_tooth_number(number, permanent) {
return Err(err);
}
Ok(Tooth {
quadrant,
permanent,
number,
}
})
}
pub fn to_iso(&self) -> String {
@ -98,7 +104,7 @@ impl Teeth {
quadrant_number.to_string() + &self.number.to_string()
}
pub fn from_uns(value: &str) -> Self {
pub fn from_uns(value: &str) -> Result<Self, String> {
let number_value_option = value.parse::<u8>();
let mut permanent = false;
let uns = if number_value_option.is_ok() {
@ -109,40 +115,42 @@ impl Teeth {
};
if permanent && (uns < 1 || uns > 32) {
panic!(
"UNS permanent teeth has to be in range [1; 32] (currently {})",
return Err(format!(
"UNS permanent tooth has to be in range [1; 32] (currently {})",
uns
);
));
} else if !permanent && (uns < 1 || uns > 20) {
panic!("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,
_ => panic!("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)
};
Teeth::check_teeth_number(number, permanent);
Teeth {
if let Err(err) = Tooth::check_tooth_number(number, permanent) {
return Err(err);
}
Ok(Tooth {
quadrant,
number,
permanent,
}
})
}
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) => x,
(QuadrantKind::TopLeft, x) => max - x + 1,
(QuadrantKind::TopRight, x) => x + max,
(QuadrantKind::BottomRight, x) => x + max * 2,
(QuadrantKind::BottomRight, x) => (max - x + 1) + max * 2,
(QuadrantKind::BottomLeft, x) => x + max * 3,
};
@ -153,24 +161,29 @@ impl Teeth {
}
}
pub fn from_alphanumeric(value: &str) -> Self {
pub fn from_alphanumeric(value: &str) -> Result<Self, String> {
if value.len() != 3 {
panic!("{} is not a valid alphanumeric dental notation", value);
return Err(format!(
"{} is not a valid alphanumeric dental notation",
value
));
}
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
),
x => {
return Err(format!(
"Quadrant value {} not a valid value (accepted: ['UL', 'UR', 'LR', 'LL'])",
x
))
}
};
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),
@ -184,17 +197,21 @@ impl Teeth {
"C" => (3, false),
"D" => (4, false),
"E" => (5, false),
x => panic!(
"Number value {} not a valid value (accepted: [1; 8] and ['A'; 'E'])",
x
),
x => {
return Err(format!(
"Number value {} not a valid value (accepted: [1; 8] and ['A'; 'E'])",
x
))
}
};
Teeth::check_teeth_number(number, permanent);
Teeth {
if let Err(err) = Tooth::check_tooth_number(number, permanent) {
return Err(err);
}
Ok(Tooth {
quadrant,
permanent,
number,
}
})
}
pub fn to_alphanumeric(&self) -> String {
@ -212,6 +229,22 @@ impl Teeth {
};
quadrant.to_owned() + &number
}
pub fn convert(from: &NotationKind, to: &NotationKind, value: &str) -> Result<String, String> {
let tooth_result = match from {
NotationKind::Iso => Tooth::from_iso(value),
NotationKind::Uns => Tooth::from_uns(value),
NotationKind::Alphanumeric => Tooth::from_alphanumeric(value),
};
match tooth_result {
Ok(tooth) => match to {
NotationKind::Iso => Ok(tooth.to_iso()),
NotationKind::Uns => Ok(tooth.to_uns()),
NotationKind::Alphanumeric => Ok(tooth.to_alphanumeric()),
},
Err(err) => Err(err),
}
}
}
#[cfg(test)]
@ -222,12 +255,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);
}
};
}
@ -236,10 +269,11 @@ mod test {
($name:ident, $func:ident, $value:expr, $quadrant:expr,$number:expr, $permanent:expr) => {
#[test]
fn $name() {
let teeth = Teeth::$func($value);
assert_eq!(teeth.quadrant, $quadrant);
assert_eq!(teeth.number, $number);
assert_eq!(teeth.permanent, $permanent);
let tooth = Tooth::$func($value).unwrap();
assert_eq!(tooth.quadrant, $quadrant);
assert_eq!(tooth.number, $number);
assert_eq!(tooth.permanent, $permanent);
}
};
}
@ -247,9 +281,8 @@ mod test {
macro_rules! from_fail {
($name:ident, $func:ident, $value:expr) => {
#[test]
#[should_panic]
fn $name() {
Teeth::$func($value);
assert!(Tooth::$func($value).is_err());
}
};
}
@ -375,20 +408,20 @@ mod test {
from_fail!(from_iso_fail_80, from_iso, "80");
from_fail!(from_iso_fail_86, from_iso, "86");
to!(to_uns_1, to_uns, QuadrantKind::TopLeft, 1, true, "1");
to!(to_uns_8, to_uns, QuadrantKind::TopLeft, 8, true, "8");
to!(to_uns_1, to_uns, QuadrantKind::TopLeft, 1, true, "8");
to!(to_uns_8, to_uns, QuadrantKind::TopLeft, 8, true, "1");
to!(to_uns_9, to_uns, QuadrantKind::TopRight, 1, true, "9");
to!(to_uns_16, to_uns, QuadrantKind::TopRight, 8, true, "16");
to!(to_uns_17, to_uns, QuadrantKind::BottomRight, 1, true, "17");
to!(to_uns_24, to_uns, QuadrantKind::BottomRight, 8, true, "24");
to!(to_uns_17, to_uns, QuadrantKind::BottomRight, 1, true, "24");
to!(to_uns_24, to_uns, QuadrantKind::BottomRight, 8, true, "17");
to!(to_uns_25, to_uns, QuadrantKind::BottomLeft, 1, true, "25");
to!(to_uns_32, to_uns, QuadrantKind::BottomLeft, 8, true, "32");
to!(to_uns_a, to_uns, QuadrantKind::TopLeft, 1, false, "A");
to!(to_uns_e, to_uns, QuadrantKind::TopLeft, 5, false, "E");
to!(to_uns_a, to_uns, QuadrantKind::TopLeft, 1, false, "E");
to!(to_uns_e, to_uns, QuadrantKind::TopLeft, 5, false, "A");
to!(to_uns_f, to_uns, QuadrantKind::TopRight, 1, false, "F");
to!(to_uns_j, to_uns, QuadrantKind::TopRight, 5, false, "J");
to!(to_uns_k, to_uns, QuadrantKind::BottomRight, 1, false, "K");
to!(to_uns_o, to_uns, QuadrantKind::BottomRight, 5, false, "O");
to!(to_uns_k, to_uns, QuadrantKind::BottomRight, 1, false, "O");
to!(to_uns_o, to_uns, QuadrantKind::BottomRight, 5, false, "K");
to!(to_uns_p, to_uns, QuadrantKind::BottomLeft, 1, false, "P");
to!(to_uns_t, to_uns, QuadrantKind::BottomLeft, 5, false, "T");

View File

@ -1,3 +1,6 @@
mod cli;
mod lib;
fn main() {}
fn main() {
cli::run_cli()
}