Skip to content

Commit ac6f02d

Browse files
committed
Refactor filter command line option handling
1 parent 9455e90 commit ac6f02d

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

rewatch/src/cli.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ use std::{ffi::OsString, ops::Deref};
22

33
use clap::{Args, Parser, Subcommand};
44
use clap_verbosity_flag::InfoLevel;
5+
use regex::Regex;
6+
7+
fn parse_regex(s: &str) -> Result<Regex, regex::Error> {
8+
Regex::new(s)
9+
}
510

611
/// ReScript - Fast, Simple, Fully Typed JavaScript from the Future
712
#[derive(Parser, Debug)]
@@ -39,8 +44,8 @@ pub struct FilterArg {
3944
///
4045
/// Filter allows for a regex to be supplied which will filter the files to be compiled. For
4146
/// instance, to filter out test files for compilation while doing feature work.
42-
#[arg(short, long)]
43-
pub filter: Option<String>,
47+
#[arg(short, long, value_parser = parse_regex)]
48+
pub filter: Option<Regex>,
4449
}
4550

4651
#[derive(Args, Debug, Clone)]
@@ -203,14 +208,6 @@ impl Deref for FolderArg {
203208
}
204209
}
205210

206-
impl Deref for FilterArg {
207-
type Target = Option<String>;
208-
209-
fn deref(&self) -> &Self::Target {
210-
&self.filter
211-
}
212-
}
213-
214211
impl Deref for AfterBuildArg {
215212
type Target = Option<String>;
216213

rewatch/src/main.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use anyhow::Result;
22
use clap::Parser;
33
use log::LevelFilter;
4-
use regex::Regex;
54
use std::{io::Write, path::Path};
65

76
use rewatch::{build, cli, cmd, lock, watcher};
@@ -38,13 +37,8 @@ fn main() -> Result<()> {
3837
cli::Command::Build(build_args) => {
3938
let _lock = get_lock(&build_args.folder);
4039

41-
let filter = build_args
42-
.filter
43-
.as_ref()
44-
.map(|filter| Regex::new(&filter).expect("Could not parse regex"));
45-
4640
match build::build(
47-
&filter,
41+
&build_args.filter.filter,
4842
Path::new(&build_args.folder as &str),
4943
show_progress,
5044
build_args.no_timing,
@@ -67,12 +61,8 @@ fn main() -> Result<()> {
6761
cli::Command::Watch(watch_args) => {
6862
let _lock = get_lock(&watch_args.folder);
6963

70-
let filter = watch_args
71-
.filter
72-
.as_ref()
73-
.map(|filter| Regex::new(&filter).expect("Could not parse regex"));
7464
watcher::start(
75-
&filter,
65+
&watch_args.filter.filter,
7666
show_progress,
7767
&watch_args.folder,
7868
(*watch_args.after_build).clone(),

0 commit comments

Comments
 (0)