Factor out the unnecessary ModArg struct

This commit is contained in:
Dustin J. Mitchell 2020-12-30 00:28:48 +00:00
parent fc977a0fe6
commit 83d8fc3b4e

View file

@ -33,13 +33,6 @@ pub(crate) enum Condition {
IdList(Vec<TaskId>), IdList(Vec<TaskId>),
} }
/// Internal struct representing a parsed filter argument
enum FilterArg {
// TODO: get rid of this whole enum
IdList(Vec<TaskId>),
Condition(Condition),
}
impl Filter { impl Filter {
pub(super) fn parse(input: ArgList) -> IResult<ArgList, Filter> { pub(super) fn parse(input: ArgList) -> IResult<ArgList, Filter> {
fold_many0( fold_many0(
@ -52,27 +45,25 @@ impl Filter {
} }
/// fold multiple filter args into a single Filter instance /// fold multiple filter args into a single Filter instance
fn with_arg(mut self, mod_arg: FilterArg) -> Filter { fn with_arg(mut self, cond: Condition) -> Filter {
match mod_arg { if let Condition::IdList(mut id_list) = cond {
FilterArg::IdList(mut id_list) => { // If there is already an IdList condition, concatenate this one
// If there is already an IdList condition, concatenate this one // to it. Thus multiple IdList command-line args represent an OR
// to it. Thus multiple IdList command-line args represent an OR // operation. This assumes that the filter is still being built
// operation. This assumes that the filter is still being built // from command-line arguments and thus has at most one IdList
// from command-line arguments and thus has at most one IdList // condition.
// condition. if let Some(Condition::IdList(existing)) = self
if let Some(Condition::IdList(existing)) = self .conditions
.conditions .iter_mut()
.iter_mut() .find(|c| matches!(c, Condition::IdList(_)))
.find(|c| matches!(c, Condition::IdList(_))) {
{ existing.append(&mut id_list);
existing.append(&mut id_list); } else {
} else { self.conditions.push(Condition::IdList(id_list));
self.conditions.push(Condition::IdList(id_list));
}
}
FilterArg::Condition(cond) => {
self.conditions.push(cond);
} }
} else {
// all other command-line conditions are AND'd together
self.conditions.push(cond);
} }
self self
} }
@ -87,23 +78,23 @@ impl Filter {
// parsers // parsers
fn id_list(input: ArgList) -> IResult<ArgList, FilterArg> { fn id_list(input: ArgList) -> IResult<ArgList, Condition> {
fn to_filterarg(input: Vec<TaskId>) -> Result<FilterArg, ()> { fn to_filterarg(input: Vec<TaskId>) -> Result<Condition, ()> {
Ok(FilterArg::IdList(input)) Ok(Condition::IdList(input))
} }
map_res(arg_matching(id_list), to_filterarg)(input) map_res(arg_matching(id_list), to_filterarg)(input)
} }
fn plus_tag(input: ArgList) -> IResult<ArgList, FilterArg> { fn plus_tag(input: ArgList) -> IResult<ArgList, Condition> {
fn to_filterarg(input: &str) -> Result<FilterArg, ()> { fn to_filterarg(input: &str) -> Result<Condition, ()> {
Ok(FilterArg::Condition(Condition::HasTag(input.to_owned()))) Ok(Condition::HasTag(input.to_owned()))
} }
map_res(arg_matching(plus_tag), to_filterarg)(input) map_res(arg_matching(plus_tag), to_filterarg)(input)
} }
fn minus_tag(input: ArgList) -> IResult<ArgList, FilterArg> { fn minus_tag(input: ArgList) -> IResult<ArgList, Condition> {
fn to_filterarg(input: &str) -> Result<FilterArg, ()> { fn to_filterarg(input: &str) -> Result<Condition, ()> {
Ok(FilterArg::Condition(Condition::NoTag(input.to_owned()))) Ok(Condition::NoTag(input.to_owned()))
} }
map_res(arg_matching(minus_tag), to_filterarg)(input) map_res(arg_matching(minus_tag), to_filterarg)(input)
} }