From f87a3f16585eea8a0b545b9a77c9865cfc8aaccf Mon Sep 17 00:00:00 2001 From: Ryan Geary Date: Sun, 15 Mar 2020 16:30:59 -0400 Subject: Use lazy_static for parse_choice regex --- Cargo.lock | 1 + Cargo.toml | 1 + src/config.rs | 8 +++++--- src/main.rs | 3 +++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 99a13bd..fbee6a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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)", ] diff --git a/Cargo.toml b/Cargo.toml index a9bbe48..36026c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ edition = "2018" [dependencies] structopt = "0.3.0" regex = "1" +lazy_static = "1" diff --git a/src/config.rs b/src/config.rs index 9814a85..afca365 100644 --- a/src/config.rs +++ b/src/config.rs @@ -32,6 +32,10 @@ pub struct Opt { pub choice: Vec, } +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 { - 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)), diff --git a/src/main.rs b/src/main.rs index e80f98f..b812462 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; -- cgit v1.2.3