mirror of
https://github.com/theryangeary/choose
synced 2024-11-10 13:34:13 +00:00
Use lazy_static for parse_choice regex
This commit is contained in:
parent
afb4bf4734
commit
f87a3f1658
4 changed files with 10 additions and 3 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -34,6 +34,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
name = "choose"
|
||||
version = "0.1.3"
|
||||
dependencies = [
|
||||
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"structopt 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
|
|
@ -9,3 +9,4 @@ edition = "2018"
|
|||
[dependencies]
|
||||
structopt = "0.3.0"
|
||||
regex = "1"
|
||||
lazy_static = "1"
|
||||
|
|
|
@ -32,6 +32,10 @@ pub struct Opt {
|
|||
pub choice: Vec<Choice>,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref PARSE_CHOICE_RE: Regex = Regex::new(r"^(\d*):(\d*)$").unwrap();
|
||||
}
|
||||
|
||||
pub struct Config {
|
||||
pub opt: Opt,
|
||||
pub separator: Regex,
|
||||
|
@ -77,9 +81,7 @@ impl Config {
|
|||
}
|
||||
|
||||
pub fn parse_choice(src: &str) -> Result<Choice, ParseIntError> {
|
||||
let re = Regex::new(r"^(\d*):(\d*)$").unwrap();
|
||||
|
||||
let cap = match re.captures_iter(src).next() {
|
||||
let cap = match PARSE_CHOICE_RE.captures_iter(src).next() {
|
||||
Some(v) => v,
|
||||
None => match src.parse() {
|
||||
Ok(x) => return Ok(Choice::new(x, x)),
|
||||
|
|
|
@ -2,6 +2,9 @@ use std::fs::File;
|
|||
use std::io::{self, Read, Write};
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
mod choice;
|
||||
mod config;
|
||||
mod reader;
|
||||
|
|
Loading…
Reference in a new issue