Skip to content

Commit c9d7f2d

Browse files
committed
Rename filter option to include; add exclude option with inverted logic
1 parent 1adbe01 commit c9d7f2d

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/main.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ struct Column<'a> {
5050
value: RefCell<String>,
5151
attr: Option<&'a str>,
5252
hide: bool,
53-
filter: Option<Regex>,
53+
include: Option<Regex>,
54+
exclude: Option<Regex>,
5455
convert: Option<&'a str>,
5556
find: Option<&'a str>,
5657
replace: Option<&'a str>,
@@ -129,7 +130,8 @@ fn add_table<'a>(rowpath: &str, outfile: Option<&str>, filemode: &str, skip: Opt
129130
}
130131
};
131132
let hide = col["hide"].as_bool().unwrap_or(false);
132-
let filter: Option<Regex> = col["filt"].as_str().map(|str| Regex::new(str).expect("Invalid regex in 'filt' entry in configuration file"));
133+
let include: Option<Regex> = col["incl"].as_str().map(|str| Regex::new(str).expect("Invalid regex in 'incl' entry in configuration file"));
134+
let exclude: Option<Regex> = col["excl"].as_str().map(|str| Regex::new(str).expect("Invalid regex in 'excl' entry in configuration file"));
133135
let attr = col["attr"].as_str();
134136
let convert = col["conv"].as_str();
135137
let find = col["find"].as_str();
@@ -139,19 +141,19 @@ fn add_table<'a>(rowpath: &str, outfile: Option<&str>, filemode: &str, skip: Opt
139141
if convert.is_some() && !vec!("xml-to-text", "gml-to-ewkb").contains(&convert.unwrap()) {
140142
panic!("Option 'convert' contains invalid value {}", convert.unwrap());
141143
}
142-
if filter.is_some() {
144+
if include.is_some() || exclude.is_some() {
143145
if convert.is_some() {
144-
panic!("Option 'filt' and 'conv' cannot be used together on a single column");
146+
panic!("Filtering (incl/excl) and 'conv' cannot be used together on a single column");
145147
}
146148
if find.is_some() {
147-
eprintln!("Notice: when using a filter and find/replace on a single column, the filter is applied before replacements");
149+
eprintln!("Notice: when using filtering (incl/excl) and find/replace on a single column, the filter is checked before replacements");
148150
}
149151
if consol.is_some() {
150-
eprintln!("Notice: when using a filter and consolidation on a single column, the filter is applied to each phase of consolidation separately");
152+
eprintln!("Notice: when using filtering (incl/excl) and consolidation on a single column, the filter is checked at each phase of consolidation separately");
151153
}
152154
}
153155

154-
let column = Column { name: name.to_string(), path, value: RefCell::new(String::new()), attr, hide, filter, convert, find, replace, consol, subtable };
156+
let column = Column { name: name.to_string(), path, value: RefCell::new(String::new()), attr, hide, include, exclude, convert, find, replace, consol, subtable };
155157
table.columns.push(column);
156158
}
157159
table
@@ -310,12 +312,18 @@ fn main() -> std::io::Result<()> {
310312
if table.columns[i].value.borrow().is_empty() {
311313
eprintln!("Column {} requested attribute {} not found", table.columns[i].name, request);
312314
}
313-
if let Some(re) = &table.columns[i].filter {
315+
if let Some(re) = &table.columns[i].include {
314316
if !re.is_match(&table.columns[i].value.borrow()) {
315317
filtered = true;
316318
table.clear_columns();
317319
}
318320
}
321+
if let Some(re) = &table.columns[i].exclude {
322+
if re.is_match(&table.columns[i].value.borrow()) {
323+
filtered = true;
324+
table.clear_columns();
325+
}
326+
}
319327
}
320328

321329
// Set the appropriate convert flag for the following data in case the 'conv' option is present
@@ -367,12 +375,18 @@ fn main() -> std::io::Result<()> {
367375
}
368376
}
369377
table.columns[i].value.borrow_mut().push_str(&e.unescape_and_decode(&reader).unwrap().replace("\\", "\\\\"));
370-
if let Some(re) = &table.columns[i].filter {
378+
if let Some(re) = &table.columns[i].include {
371379
if !re.is_match(&table.columns[i].value.borrow()) {
372380
filtered = true;
373381
table.clear_columns();
374382
}
375383
}
384+
if let Some(re) = &table.columns[i].exclude {
385+
if re.is_match(&table.columns[i].value.borrow()) {
386+
filtered = true;
387+
table.clear_columns();
388+
}
389+
}
376390
break;
377391
}
378392
}
@@ -449,7 +463,7 @@ fn main() -> std::io::Result<()> {
449463
eprintln!("{} rows processed in {} seconds{}{}",
450464
fullcount-filtercount-skipcount,
451465
start.elapsed().as_secs(),
452-
match filtercount { 0 => "".to_owned(), n => format!(" ({} filtered)", n) },
466+
match filtercount { 0 => "".to_owned(), n => format!(" ({} excluded)", n) },
453467
match skipcount { 0 => "".to_owned(), n => format!(" ({} skipped)", n) }
454468
);
455469
Ok(())

0 commit comments

Comments
 (0)