expose the ability to create search groups

This commit is contained in:
Damien Elmes 2021-02-11 11:21:33 +10:00
parent 5c69bccfcf
commit 8852359fa9
3 changed files with 28 additions and 4 deletions

View file

@ -809,9 +809,17 @@ message SearchTerm {
message IdList { message IdList {
repeated int64 ids = 1; repeated int64 ids = 1;
} }
message Group {
enum Operator {
AND = 0;
OR = 1;
}
repeated SearchTerm terms = 1;
Operator op = 2;
}
oneof filter { oneof filter {
string tag = 1; Group group = 1;
string deck = 2; SearchTerm negated = 2;
string note = 3; string note = 3;
uint32 template = 4; uint32 template = 4;
int64 nid = 5; int64 nid = 5;
@ -824,8 +832,9 @@ message SearchTerm {
CardState card_state = 12; CardState card_state = 12;
IdList nids = 13; IdList nids = 13;
uint32 edited_in_days = 14; uint32 edited_in_days = 14;
SearchTerm negated = 15; string deck = 15;
int32 due_on_day = 16; int32 due_on_day = 16;
string tag = 17;
} }
} }

View file

@ -55,6 +55,7 @@ use crate::{
}; };
use fluent::FluentValue; use fluent::FluentValue;
use futures::future::{AbortHandle, AbortRegistration, Abortable}; use futures::future::{AbortHandle, AbortRegistration, Abortable};
use itertools::Itertools;
use log::error; use log::error;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use pb::{sync_status_out, BackendService}; use pb::{sync_status_out, BackendService};
@ -297,6 +298,7 @@ impl From<pb::DeckConfigId> for DeckConfID {
impl From<pb::SearchTerm> for Node<'_> { impl From<pb::SearchTerm> for Node<'_> {
fn from(msg: pb::SearchTerm) -> Self { fn from(msg: pb::SearchTerm) -> Self {
use pb::search_term::group::Operator;
use pb::search_term::Filter; use pb::search_term::Filter;
use pb::search_term::Flag; use pb::search_term::Flag;
if let Some(filter) = msg.filter { if let Some(filter) = msg.filter {
@ -359,6 +361,19 @@ impl From<pb::SearchTerm> for Node<'_> {
Flag::Blue => Node::Search(SearchNode::Flag(4)), Flag::Blue => Node::Search(SearchNode::Flag(4)),
}, },
Filter::Negated(term) => Node::Not(Box::new((*term).into())), Filter::Negated(term) => Node::Not(Box::new((*term).into())),
Filter::Group(group) => {
let operator = match group.op() {
Operator::And => Node::And,
Operator::Or => Node::Or,
};
let joined = group
.terms
.into_iter()
.map(Into::into)
.intersperse(operator)
.collect();
Node::Group(joined)
}
} }
} else { } else {
Node::Search(SearchNode::WholeCollection) Node::Search(SearchNode::WholeCollection)

View file

@ -30,7 +30,7 @@ fn parse_error(input: &str) -> nom::Err<ParseError<'_>> {
nom::Err::Error(ParseError::Anki(input, FailKind::Other(None))) nom::Err::Error(ParseError::Anki(input, FailKind::Other(None)))
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Clone)]
pub enum Node<'a> { pub enum Node<'a> {
And, And,
Or, Or,