refactor: Separate dental-notation from dental-notation-cli

This commit is contained in:
2022-08-04 17:07:14 +02:00
commit 9012a2a512
5 changed files with 361 additions and 0 deletions

93
src/cli.rs Normal file
View File

@@ -0,0 +1,93 @@
use clap::{Parser, Subcommand, ValueEnum};
use dental_notation::display::{ToothCliFormatter, ToothDisplay};
use dental_notation::NotationKind;
use dental_notation::Tooth;
#[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,
},
Display {
#[clap(value_enum, short = 'n', long)]
notation: NotationKindArg,
#[clap(takes_value = false, short = 'p', long)]
primary: bool,
#[clap(value_parser)]
value: Option<String>,
},
}
#[derive(Parser, Debug)]
struct Args {
#[clap(subcommand)]
action: Action,
}
impl Into<NotationKind> for NotationKindArg {
fn into(self) -> NotationKind {
(&self).into()
}
}
impl Into<NotationKind> for &NotationKindArg {
fn into(self) -> NotationKind {
match self {
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::from(value, &from.into());
let output_string = match tooth_result {
Ok(tooth) => tooth.to(&to.into()),
Err(err) => err,
};
println!("{}", output_string);
}
Action::Display {
notation,
primary,
value,
} => {
let converted_value = match value {
Some(t) => {
let tooth_result = Tooth::from(t, &notation.into());
match tooth_result {
Ok(tr) => Some(tr),
Err(err) => {
eprintln!("{}", err);
return;
}
}
}
None => None,
};
println!(
"{}",
ToothCliFormatter::format(&notation.into(), !primary, &converted_value)
);
}
};
}

5
src/main.rs Normal file
View File

@@ -0,0 +1,5 @@
mod cli;
fn main() {
cli::run_cli()
}