Skip to content

Commit b647753

Browse files
Merge pull request #18 from aaronriekenberg/autoregex
autoregex to main
2 parents 1d5fd2b + ad85ee2 commit b647753

File tree

5 files changed

+266
-33
lines changed

5 files changed

+266
-33
lines changed

src/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub struct Parsers {
5353
impl Parsers {
5454
pub fn new(command_line_args: &'static CommandLineArgs) -> anyhow::Result<Self> {
5555
let regex_processor = RegexProcessor::new(command_line_args)?;
56+
5657
Ok(Self {
5758
buffered_input_line_parser: OnceCell::new(),
5859
regex_processor,

src/parser/buffered.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ impl BufferedInputLineParser {
5959

6060
cmd_and_args
6161
} else {
62-
self.regex_processor
63-
.apply_regex_to_arguments(&self.command_and_initial_arguments, input_line)?
62+
let apply_regex_result = self
63+
.regex_processor
64+
.apply_regex_to_arguments(&self.command_and_initial_arguments, input_line)?;
65+
apply_regex_result.arguments
6466
};
6567

6668
super::build_owned_command_and_args(&self.shell_command_and_args, cmd_and_args)

src/parser/command_line.rs

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,22 @@ impl CommandLineArgsParser {
7070
}
7171

7272
fn parse_argument_group(&self, argument_group: Vec<String>) -> Option<OwnedCommandAndArgs> {
73+
let first_command_and_args = &self.argument_groups.first_command_and_args;
74+
7375
let cmd_and_args = if !self.regex_processor.regex_mode() {
74-
[
75-
self.argument_groups.first_command_and_args.clone(),
76-
argument_group,
77-
]
78-
.concat()
76+
[first_command_and_args.clone(), argument_group].concat()
7977
} else {
8078
let input_line = argument_group.join(" ");
8179

82-
self.regex_processor.apply_regex_to_arguments(
83-
&self.argument_groups.first_command_and_args,
84-
&input_line,
85-
)?
80+
let apply_regex_result = self
81+
.regex_processor
82+
.apply_regex_to_arguments(first_command_and_args, &input_line)?;
83+
84+
if apply_regex_result.modified_arguments {
85+
apply_regex_result.arguments
86+
} else {
87+
[first_command_and_args.clone(), argument_group].concat()
88+
}
8689
};
8790

8891
super::build_owned_command_and_args(&self.shell_command_and_args, cmd_and_args)
@@ -455,4 +458,58 @@ mod test {
455458
]
456459
);
457460
}
461+
462+
#[test]
463+
fn test_auto_regex() {
464+
let command_line_args = CommandLineArgs {
465+
command_and_initial_arguments: [
466+
"echo", "got", "arg1={1}", "arg2={2}", ":::", "foo", "bar", ":::", "baz", "qux",
467+
]
468+
.into_iter()
469+
.map_into()
470+
.collect(),
471+
..Default::default()
472+
};
473+
474+
let parser = CommandLineArgsParser::new(
475+
&command_line_args,
476+
&RegexProcessor::new(&command_line_args).unwrap(),
477+
);
478+
479+
let result = collect_into_vec(parser);
480+
481+
assert_eq!(
482+
result,
483+
vec![
484+
OwnedCommandAndArgs {
485+
command_path: PathBuf::from("echo"),
486+
args: ["got", "arg1=foo", "arg2=baz"]
487+
.into_iter()
488+
.map_into()
489+
.collect(),
490+
},
491+
OwnedCommandAndArgs {
492+
command_path: PathBuf::from("echo"),
493+
args: ["got", "arg1=foo", "arg2=qux"]
494+
.into_iter()
495+
.map_into()
496+
.collect(),
497+
},
498+
OwnedCommandAndArgs {
499+
command_path: PathBuf::from("echo"),
500+
args: ["got", "arg1=bar", "arg2=baz"]
501+
.into_iter()
502+
.map_into()
503+
.collect(),
504+
},
505+
OwnedCommandAndArgs {
506+
command_path: PathBuf::from("echo"),
507+
args: ["got", "arg1=bar", "arg2=qux"]
508+
.into_iter()
509+
.map_into()
510+
.collect(),
511+
},
512+
]
513+
);
514+
}
458515
}

0 commit comments

Comments
 (0)