summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Geary <rtgnj42@gmail.com>2020-03-15 16:30:59 -0400
committerRyan Geary <rtgnj42@gmail.com>2020-03-15 16:30:59 -0400
commitf87a3f16585eea8a0b545b9a77c9865cfc8aaccf (patch)
tree91a47e489591cca03e2c8fc28f65655f44074a9b
parentafb4bf47340f4ae6dbfb1dcfd199dcdacc42c378 (diff)
Use lazy_static for parse_choice regex
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml1
-rw-r--r--src/config.rs8
-rw-r--r--src/main.rs3
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<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)),
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;