feat: Separate convert into from and to
This commit is contained in:
17
src/cli.rs
17
src/cli.rs
@@ -1,3 +1,4 @@
|
|||||||
|
use crate::display::format_dentition;
|
||||||
use crate::lib::NotationKind;
|
use crate::lib::NotationKind;
|
||||||
use crate::lib::Tooth;
|
use crate::lib::Tooth;
|
||||||
use clap::{Parser, Subcommand, ValueEnum};
|
use clap::{Parser, Subcommand, ValueEnum};
|
||||||
@@ -20,6 +21,12 @@ enum Action {
|
|||||||
#[clap(value_parser)]
|
#[clap(value_parser)]
|
||||||
value: String,
|
value: String,
|
||||||
},
|
},
|
||||||
|
Display {
|
||||||
|
#[clap(value_enum, short = 'n', long)]
|
||||||
|
notation: NotationKindArg,
|
||||||
|
#[clap(takes_value = false, short = 'p', long)]
|
||||||
|
primary: bool,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
struct Args {
|
struct Args {
|
||||||
@@ -34,17 +41,23 @@ fn convert_kind(kind_arg: &NotationKindArg) -> NotationKind {
|
|||||||
NotationKindArg::Alphanumeric => NotationKind::Alphanumeric,
|
NotationKindArg::Alphanumeric => NotationKind::Alphanumeric,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_cli() {
|
pub fn run_cli() {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
match &args.action {
|
match &args.action {
|
||||||
Action::Convert { from, to, value } => {
|
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 {
|
let output_string = match tooth_result {
|
||||||
|
Ok(tooth) => tooth.to(&convert_kind(to)),
|
||||||
Err(err) => err,
|
Err(err) => err,
|
||||||
Ok(ok) => ok,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("{}", output_string);
|
println!("{}", output_string);
|
||||||
}
|
}
|
||||||
|
Action::Display { notation, primary } => {
|
||||||
|
println!("{}", format_dentition(!primary, &convert_kind(notation)))
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
25
src/lib.rs
25
src/lib.rs
@@ -230,18 +230,27 @@ impl Tooth {
|
|||||||
quadrant.to_owned() + &number
|
quadrant.to_owned() + &number
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn convert(from: &NotationKind, to: &NotationKind, value: &str) -> Result<String, String> {
|
pub fn from(value: &str, from: &NotationKind) -> Result<Self, String> {
|
||||||
let tooth_result = match from {
|
match from {
|
||||||
NotationKind::Iso => Tooth::from_iso(value),
|
NotationKind::Iso => Tooth::from_iso(value),
|
||||||
NotationKind::Uns => Tooth::from_uns(value),
|
NotationKind::Uns => Tooth::from_uns(value),
|
||||||
NotationKind::Alphanumeric => Tooth::from_alphanumeric(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 {
|
match tooth_result {
|
||||||
Ok(tooth) => match to {
|
Ok(tooth) => Ok(tooth.to(to)),
|
||||||
NotationKind::Iso => Ok(tooth.to_iso()),
|
|
||||||
NotationKind::Uns => Ok(tooth.to_uns()),
|
|
||||||
NotationKind::Alphanumeric => Ok(tooth.to_alphanumeric()),
|
|
||||||
},
|
|
||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user