Add not operator (bang) for index filter
This commit is contained in:
parent
482d86aedc
commit
dd5f15c745
3 changed files with 30 additions and 5 deletions
|
@ -323,7 +323,7 @@ impl<P: MpsFilterPredicate + 'static, F: MpsFilterFactory<P> + 'static>
|
||||||
pub fn new(factory: F) -> Self {
|
pub fn new(factory: F) -> Self {
|
||||||
Self {
|
Self {
|
||||||
filter_factory: factory,
|
filter_factory: factory,
|
||||||
idc: PhantomData::default(),
|
idc: PhantomData::<P>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ use std::fmt::{Debug, Display, Error, Formatter};
|
||||||
use crate::lang::{MpsLanguageDictionary, MpsTypePrimitive};
|
use crate::lang::{MpsLanguageDictionary, MpsTypePrimitive};
|
||||||
use crate::lang::{MpsFilterFactory, MpsFilterPredicate, MpsFilterStatementFactory};
|
use crate::lang::{MpsFilterFactory, MpsFilterPredicate, MpsFilterStatementFactory};
|
||||||
use crate::lang::{RuntimeError, SyntaxError};
|
use crate::lang::{RuntimeError, SyntaxError};
|
||||||
use crate::lang::Lookup;
|
use crate::lang::{Lookup, utility::assert_token_raw};
|
||||||
use crate::processing::{OpGetter, general::MpsType};
|
use crate::processing::{OpGetter, general::MpsType};
|
||||||
use crate::tokens::MpsToken;
|
use crate::tokens::MpsToken;
|
||||||
use crate::MpsContext;
|
use crate::MpsContext;
|
||||||
|
@ -16,6 +16,7 @@ pub struct IndexFilter {
|
||||||
// state
|
// state
|
||||||
current: u64,
|
current: u64,
|
||||||
complete: bool,
|
complete: bool,
|
||||||
|
is_opposite: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for IndexFilter {
|
impl Display for IndexFilter {
|
||||||
|
@ -48,10 +49,13 @@ impl MpsFilterPredicate for IndexFilter {
|
||||||
msg: format!("Cannot use {} as index", val),
|
msg: format!("Cannot use {} as index", val),
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
if self.current == index {
|
if self.current == index && !self.is_opposite {
|
||||||
self.current += 1;
|
self.current += 1;
|
||||||
self.complete = true;
|
self.complete = true;
|
||||||
Ok(true)
|
Ok(true)
|
||||||
|
} else if self.current != index && self.is_opposite {
|
||||||
|
self.current += 1;
|
||||||
|
Ok(true)
|
||||||
} else {
|
} else {
|
||||||
self.current += 1;
|
self.current += 1;
|
||||||
Ok(false)
|
Ok(false)
|
||||||
|
@ -73,8 +77,14 @@ pub struct IndexFilterFactory;
|
||||||
|
|
||||||
impl MpsFilterFactory<IndexFilter> for IndexFilterFactory {
|
impl MpsFilterFactory<IndexFilter> for IndexFilterFactory {
|
||||||
fn is_filter(&self, tokens: &VecDeque<&MpsToken>) -> bool {
|
fn is_filter(&self, tokens: &VecDeque<&MpsToken>) -> bool {
|
||||||
tokens.len() == 1
|
(
|
||||||
&& Lookup::check_is(&tokens[0])
|
tokens.len() == 1
|
||||||
|
&& Lookup::check_is(&tokens[0])
|
||||||
|
) || (
|
||||||
|
tokens.len() == 2
|
||||||
|
&& tokens[0].is_exclamation()
|
||||||
|
&& Lookup::check_is(&tokens[1])
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_filter(
|
fn build_filter(
|
||||||
|
@ -82,11 +92,16 @@ impl MpsFilterFactory<IndexFilter> for IndexFilterFactory {
|
||||||
tokens: &mut VecDeque<MpsToken>,
|
tokens: &mut VecDeque<MpsToken>,
|
||||||
_dict: &MpsLanguageDictionary,
|
_dict: &MpsLanguageDictionary,
|
||||||
) -> Result<IndexFilter, SyntaxError> {
|
) -> Result<IndexFilter, SyntaxError> {
|
||||||
|
let is_inverted = if tokens[0].is_exclamation() {
|
||||||
|
assert_token_raw(MpsToken::Exclamation, tokens)?;
|
||||||
|
true
|
||||||
|
} else {false};
|
||||||
let lookup = Lookup::parse(tokens)?;
|
let lookup = Lookup::parse(tokens)?;
|
||||||
Ok(IndexFilter {
|
Ok(IndexFilter {
|
||||||
index: lookup,
|
index: lookup,
|
||||||
current: 0,
|
current: 0,
|
||||||
complete: false,
|
complete: false,
|
||||||
|
is_opposite: is_inverted,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -210,6 +210,11 @@ fn execute_indexfilter_line() -> Result<(), Box<dyn MpsLanguageError>> {
|
||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
)?;
|
)?;
|
||||||
|
execute_single_line(
|
||||||
|
"files(`~/Music/MusicFlac/Bruno Mars/24K Magic/`).(!0)",
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
)?;
|
||||||
execute_single_line(
|
execute_single_line(
|
||||||
"files(`~/Music/MusicFlac/Bruno Mars/24K Magic/`).(200)",
|
"files(`~/Music/MusicFlac/Bruno Mars/24K Magic/`).(200)",
|
||||||
true,
|
true,
|
||||||
|
@ -252,5 +257,10 @@ fn execute_orfilter_line() -> Result<(), Box<dyn MpsLanguageError>> {
|
||||||
"files(`~/Music/MusicFlac/Bruno Mars/24K Magic/`).(year != 2020 || 5)",
|
"files(`~/Music/MusicFlac/Bruno Mars/24K Magic/`).(year != 2020 || 5)",
|
||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
|
)?;
|
||||||
|
execute_single_line(
|
||||||
|
"files(`~/Music/MusicFlac/Bruno Mars/24K Magic/`).(year != 2020 || 5 || 4 || 12)",
|
||||||
|
false,
|
||||||
|
true,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue