Skip to content

Commit

Permalink
Simplify repl line parsing regex
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Feb 8, 2021
1 parent 1cec709 commit 16f80cb
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions src/bdk_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use regex::Regex;

/// REPL mode
#[derive(Debug, StructOpt, Clone, PartialEq)]
#[structopt(name = "", setting = AppSettings::NoBinaryName,
#[structopt(name = "", setting = AppSettings::NoBinaryName,
version = option_env ! ("CARGO_PKG_VERSION").unwrap_or("unknown"),
author = option_env ! ("CARGO_PKG_AUTHORS").unwrap_or(""))]
pub enum ReplSubCommand {
Expand Down Expand Up @@ -195,8 +195,7 @@ fn main() {
// println!("No previous history.");
// }

let split_regex = Regex::new(r#"[\w\-]+|"[\w\s]*""#).unwrap();
let filter_regex = Regex::new(r#"[\w\s\-]+"#).unwrap();
let split_regex = Regex::new(r#"([\w\-]+)|"([\w\s]*)""#).unwrap();

loop {
let readline = rl.readline(">> ");
Expand All @@ -206,14 +205,12 @@ fn main() {
continue;
}
rl.add_history_entry(line.as_str());
let split_line: Vec<&str> =
split_regex.find_iter(&line).map(|m| m.as_str()).collect();
let filtered_line: Vec<&str> = split_line
.iter()
.flat_map(|s| filter_regex.find_iter(s).map(|m| m.as_str()))
let split_line: Vec<&str> = split_regex
.captures_iter(&line)
.map(|c| c.get(1).or_else(|| c.get(2)).unwrap().as_str())
.collect();
let repl_subcommand: Result<ReplSubCommand, clap::Error> =
ReplSubCommand::from_iter_safe(filtered_line);
ReplSubCommand::from_iter_safe(split_line);
debug!("repl_subcommand = {:?}", repl_subcommand);

if let Err(err) = repl_subcommand {
Expand Down Expand Up @@ -270,21 +267,15 @@ mod test {

#[test]
fn test_regex() {
let split_regex = Regex::new(r#"[\w\-]+|"[\w\s]*""#).unwrap();
//println!("split_regex = {:?}", &split_regex);
let filter_regex = Regex::new(r#"[\w\s\-]+"#).unwrap();
//println!("filter_regex = {:?}", &filter_regex);
let split_regex = Regex::new(r#"([\w\-]+)|"([\w\s]*)""#).unwrap();
let line = r#"restore -m "word1 word2 word3" -p test"#;
let split_line: Vec<&str> = split_regex.find_iter(&line).map(|m| m.as_str()).collect();
//println!("split_line({}) = {:?}", &line, &split_line);
let filtered_line: Vec<&str> = split_line
.iter()
.flat_map(|s| filter_regex.find_iter(s).map(|m| m.as_str()))
let split_line: Vec<&str> = split_regex
.captures_iter(&line)
.map(|c| c.get(1).or_else(|| c.get(2)).unwrap().as_str())
.collect();
//println!("filtered_line({:?}) = {:?}", &split_line, &filtered_line);
assert_eq!(
vec!("restore", "-m", "word1 word2 word3", "-p", "test"),
filtered_line
split_line
);
}
}

0 comments on commit 16f80cb

Please sign in to comment.