allow listing more than 2 filters (#105)

Closes #101
This commit is contained in:
magicgoose 2018-06-07 04:10:20 +03:00 committed by Josh Holmer
parent e472c82876
commit 5570a24781
3 changed files with 34 additions and 57 deletions

23
Cargo.lock generated
View file

@ -369,7 +369,6 @@ dependencies = [
"miniz_oxide 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"zopfli 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -491,18 +490,6 @@ dependencies = [
"utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.5.6"
@ -511,14 +498,6 @@ dependencies = [
"ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "scopeguard"
version = "0.3.3"
@ -780,9 +759,7 @@ dependencies = [
"checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384"
"checksum regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "75ecf88252dce580404a22444fc7d626c01815debba56a7f4f536772a5ff19d3"
"checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7"
"checksum regex-syntax 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8f1ac0f60d675cc6cf13a20ec076568254472551051ad5dd050364d70671bf6b"
"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27"
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"

View file

@ -54,14 +54,9 @@ default-features = false
features = ["png_codec"]
version = "^0.19.0"
[dependencies.regex]
optional = true
version = "^1.0.0"
[features]
binary = [
"clap",
"regex",
]
default = ["binary"]
cfzlib = ["cloudflare-zlib-sys"]

View file

@ -10,7 +10,6 @@
extern crate clap;
extern crate glob;
extern crate oxipng;
extern crate regex;
use clap::{App, Arg, ArgMatches};
use glob::glob;
@ -18,7 +17,6 @@ use oxipng::AlphaOptim;
use oxipng::Deflaters;
use oxipng::Headers;
use oxipng::{Options, PngError};
use regex::Regex;
use std::collections::HashSet;
use std::fs::DirBuilder;
use std::path::PathBuf;
@ -441,40 +439,47 @@ fn parse_numeric_range_opts(
min_value: u8,
max_value: u8,
) -> Result<HashSet<u8>, String> {
let one_item = Regex::new(format!(r"^[{}-{}]$", min_value, max_value).as_ref()).unwrap();
let multiple_items = Regex::new(
format!(
r"^([{}-{}])(,|-)([{}-{}])$",
min_value, max_value, min_value, max_value
).as_ref(),
).unwrap();
const ERROR_MESSAGE: &str = "Not a valid input";
let mut items = HashSet::new();
if one_item.is_match(input) {
items.insert(input.parse::<u8>().unwrap());
return Ok(items);
}
if let Some(captures) = multiple_items.captures(input) {
let first = captures[1].parse::<u8>().unwrap();
let second = captures[3].parse::<u8>().unwrap();
if first >= second {
return Err("Not a valid input".to_owned());
// one value
if let Ok(one_value) = input.parse::<u8>() {
if (min_value <= one_value) && (one_value <= max_value) {
items.insert(one_value);
return Ok(items);
}
}
match &captures[2] {
"," => {
items.insert(first);
items.insert(second);
// a range ("A-B")
let range_values = input.split('-').collect::<Vec<&str>>();
if range_values.len() == 2 {
let first_opt = range_values[0].parse::<u8>();
let second_opt = range_values[1].parse::<u8>();
if let (Ok(first), Ok(second)) = (first_opt, second_opt) {
if min_value <= first && first < second && second <= max_value {
for i in first..second + 1 {
items.insert(i);
}
return Ok(items);
}
"-" => for i in first..second + 1 {
items.insert(i);
},
_ => unreachable!(),
};
}
return Err(ERROR_MESSAGE.to_owned());
}
// a list ("A,B[,…]")
let list_items = input.split(',').collect::<Vec<&str>>();;
if list_items.len() > 1 {
for value in list_items {
if let Ok(value_int) = value.parse::<u8>() {
if (min_value <= value_int) && (value_int <= max_value) && !items.contains(&value_int) {
items.insert(value_int);
continue;
}
}
return Err(ERROR_MESSAGE.to_owned());
}
return Ok(items);
}
Err("Not a valid input".to_owned())
return Err(ERROR_MESSAGE.to_owned());
}