feat: Separate convert into from and to

This commit is contained in:
Guilhem 2022-08-03 15:54:52 +02:00
parent 3e5433a886
commit 970b7f6f64
2 changed files with 32 additions and 10 deletions

View File

@ -1,3 +1,4 @@
use crate::display::format_dentition;
use crate::lib::NotationKind;
use crate::lib::Tooth;
use clap::{Parser, Subcommand, ValueEnum};
@ -20,6 +21,12 @@ enum Action {
#[clap(value_parser)]
value: String,
},
Display {
#[clap(value_enum, short = 'n', long)]
notation: NotationKindArg,
#[clap(takes_value = false, short = 'p', long)]
primary: bool,
},
}
#[derive(Parser, Debug)]
struct Args {
@ -34,17 +41,23 @@ fn convert_kind(kind_arg: &NotationKindArg) -> NotationKind {
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 tooth_result = Tooth::from(value, &convert_kind(from));
let output_string = match tooth_result {
Ok(tooth) => tooth.to(&convert_kind(to)),
Err(err) => err,
Ok(ok) => ok,
};
println!("{}", output_string);
}
Action::Display { notation, primary } => {
println!("{}", format_dentition(!primary, &convert_kind(notation)))
}
};
}

View File

@ -230,18 +230,27 @@ impl Tooth {
quadrant.to_owned() + &number
}
pub fn convert(from: &NotationKind, to: &NotationKind, value: &str) -> Result<String, String> {
let tooth_result = match from {
pub fn from(value: &str, from: &NotationKind) -> Result<Self, String> {
match from {
NotationKind::Iso => Tooth::from_iso(value),
NotationKind::Uns => Tooth::from_uns(value),
NotationKind::Alphanumeric => Tooth::from_alphanumeric(value),
};
}
}
pub fn to(&self, to: &NotationKind) -> String {
match to {
NotationKind::Iso => self.to_iso(),
NotationKind::Uns => self.to_uns(),
NotationKind::Alphanumeric => self.to_alphanumeric(),
}
}
#[deprecated(since = "1.0.0", note = "Use 'to' and 'from' directly")]
pub fn convert(from: &NotationKind, to: &NotationKind, value: &str) -> Result<String, String> {
let tooth_result = Tooth::from(value, from);
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()),
},
Ok(tooth) => Ok(tooth.to(to)),
Err(err) => Err(err),
}
}