use clap::arg_enum;
use heidi::{chi, nhs};
use std::process;
use std::str::FromStr;
use structopt::StructOpt;
arg_enum! {
#[derive(Debug)]
enum Format {
Compact,
Official,
}
}
arg_enum! {
#[derive(Debug)]
enum Typeid {
Nhs,
Chi,
}
}
#[derive(StructOpt, Debug)]
enum Opt {
Check {
#[structopt(possible_values=&["nhs", "chi"])]
_type: Typeid,
number: String,
},
Generate {
#[structopt(long, short="f", possible_values=&["compact", "official"], default_value="compact", case_insensitive=true)]
format: Format,
#[structopt(possible_values=&["nhs", "chi"])]
_type: Typeid,
},
}
fn main() {
match Opt::from_args() {
Opt::Check { _type, number } => match _type {
Typeid::Nhs => {
match nhs::Number::from_str(&number) {
Ok(n) => {
println!("NHS Number '{:#}' is valid.", &n);
}
Err(e) => {
eprintln!("NHS Number '{}' is invalid.", &number);
eprintln!("Error: {}.", &e);
process::exit(1);
}
};
}
Typeid::Chi => {
match chi::Number::from_str(&number) {
Ok(n) => {
println!("Chi Number '{:#}' is valid.", &n);
}
Err(e) => {
eprintln!("Chi Number '{}' is invalid.", &number);
eprintln!("Error: {}.", &e);
process::exit(1);
}
};
}
},
Opt::Generate { _type, format } => match _type {
Typeid::Nhs => {
match nhs::lottery() {
Ok(n) => match format {
Format::Official => println!("{:#}", &n),
_ => println!("{}", &n),
},
Err(e) => {
eprintln!("{}", &e);
process::exit(1);
}
};
}
Typeid::Chi => {
match chi::lottery() {
Ok(n) => println!("{}", &n),
Err(e) => {
eprintln!("{}", &e);
process::exit(1);
}
};
}
},
};
}