mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
Add native enum for concatenate_search's separator
This commit is contained in:
parent
4d55826495
commit
76c16180ae
3 changed files with 33 additions and 18 deletions
|
@ -33,7 +33,8 @@ use crate::{
|
||||||
sched::cutoff::local_minutes_west_for_stamp,
|
sched::cutoff::local_minutes_west_for_stamp,
|
||||||
sched::timespan::{answer_button_time, time_span},
|
sched::timespan::{answer_button_time, time_span},
|
||||||
search::{
|
search::{
|
||||||
concatenate_searches, negate_search, normalize_search, replace_search_term, SortMode,
|
concatenate_searches, negate_search, normalize_search, replace_search_term, BoolSeparator,
|
||||||
|
SortMode,
|
||||||
},
|
},
|
||||||
stats::studied_today,
|
stats::studied_today,
|
||||||
sync::{
|
sync::{
|
||||||
|
@ -273,6 +274,17 @@ impl From<pb::DeckConfigId> for DeckConfID {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<i32> for BoolSeparator {
|
||||||
|
fn from(sep: i32) -> Self {
|
||||||
|
use pb::concatenate_searches_in::Separator;
|
||||||
|
match Separator::from_i32(sep) {
|
||||||
|
Some(Separator::And) => BoolSeparator::And,
|
||||||
|
Some(Separator::Or) => BoolSeparator::Or,
|
||||||
|
None => BoolSeparator::And,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl BackendService for Backend {
|
impl BackendService for Backend {
|
||||||
fn latest_progress(&self, _input: Empty) -> BackendResult<pb::Progress> {
|
fn latest_progress(&self, _input: Empty) -> BackendResult<pb::Progress> {
|
||||||
let progress = self.progress_state.lock().unwrap().last_progress;
|
let progress = self.progress_state.lock().unwrap().last_progress;
|
||||||
|
@ -437,7 +449,7 @@ impl BackendService for Backend {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn concatenate_searches(&self, input: pb::ConcatenateSearchesIn) -> Result<pb::String> {
|
fn concatenate_searches(&self, input: pb::ConcatenateSearchesIn) -> Result<pb::String> {
|
||||||
Ok(concatenate_searches(input.sep, &input.searches)?.into())
|
Ok(concatenate_searches(input.sep.into(), &input.searches)?.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replace_search_term(&self, input: pb::ReplaceSearchTermIn) -> Result<pb::String> {
|
fn replace_search_term(&self, input: pb::ReplaceSearchTermIn) -> Result<pb::String> {
|
||||||
|
|
|
@ -5,4 +5,6 @@ mod sqlwriter;
|
||||||
mod writer;
|
mod writer;
|
||||||
|
|
||||||
pub use cards::SortMode;
|
pub use cards::SortMode;
|
||||||
pub use writer::{concatenate_searches, negate_search, normalize_search, replace_search_term};
|
pub use writer::{
|
||||||
|
concatenate_searches, negate_search, normalize_search, replace_search_term, BoolSeparator,
|
||||||
|
};
|
||||||
|
|
|
@ -2,15 +2,20 @@
|
||||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
backend_proto::concatenate_searches_in::Separator,
|
|
||||||
decks::DeckID as DeckIDType,
|
decks::DeckID as DeckIDType,
|
||||||
err::{AnkiError, Result},
|
err::Result,
|
||||||
notetype::NoteTypeID as NoteTypeIDType,
|
notetype::NoteTypeID as NoteTypeIDType,
|
||||||
search::parser::{parse, Node, PropertyKind, SearchNode, StateKind, TemplateKind},
|
search::parser::{parse, Node, PropertyKind, SearchNode, StateKind, TemplateKind},
|
||||||
};
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum BoolSeparator {
|
||||||
|
And,
|
||||||
|
Or,
|
||||||
|
}
|
||||||
|
|
||||||
/// Take an Anki-style search string and convert it into an equivalent
|
/// Take an Anki-style search string and convert it into an equivalent
|
||||||
/// search string with normalized syntax.
|
/// search string with normalized syntax.
|
||||||
pub fn normalize_search(input: &str) -> Result<String> {
|
pub fn normalize_search(input: &str) -> Result<String> {
|
||||||
|
@ -38,11 +43,10 @@ pub fn negate_search(input: &str) -> Result<String> {
|
||||||
/// Take arbitrary Anki-style search strings and return their concatenation where they
|
/// Take arbitrary Anki-style search strings and return their concatenation where they
|
||||||
/// are separated by the provided boolean operator.
|
/// are separated by the provided boolean operator.
|
||||||
/// Empty searches (whole collection) are left out.
|
/// Empty searches (whole collection) are left out.
|
||||||
pub fn concatenate_searches(sep: i32, searches: &[String]) -> Result<String> {
|
pub fn concatenate_searches(sep: BoolSeparator, searches: &[String]) -> Result<String> {
|
||||||
let bool_node = vec![match Separator::from_i32(sep) {
|
let bool_node = vec![match sep {
|
||||||
Some(Separator::Or) => Node::Or,
|
BoolSeparator::And => Node::And,
|
||||||
Some(Separator::And) => Node::And,
|
BoolSeparator::Or => Node::Or,
|
||||||
None => return Err(AnkiError::SearchError(None)),
|
|
||||||
}];
|
}];
|
||||||
Ok(write_nodes(
|
Ok(write_nodes(
|
||||||
searches
|
searches
|
||||||
|
@ -221,25 +225,22 @@ mod test {
|
||||||
fn concatenating() -> Result<()> {
|
fn concatenating() -> Result<()> {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
r#""foo" AND "bar""#,
|
r#""foo" AND "bar""#,
|
||||||
concatenate_searches(
|
concatenate_searches(BoolSeparator::And, &["foo".to_string(), "bar".to_string()])
|
||||||
Separator::And as i32,
|
|
||||||
&["foo".to_string(), "bar".to_string()]
|
|
||||||
)
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
r#""foo" OR "bar""#,
|
r#""foo" OR "bar""#,
|
||||||
concatenate_searches(
|
concatenate_searches(
|
||||||
Separator::Or as i32,
|
BoolSeparator::Or,
|
||||||
&["foo".to_string(), "".to_string(), "bar".to_string()]
|
&["foo".to_string(), "".to_string(), "bar".to_string()]
|
||||||
)
|
)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"",
|
"",
|
||||||
concatenate_searches(Separator::Or as i32, &["".to_string()]).unwrap()
|
concatenate_searches(BoolSeparator::Or, &["".to_string()]).unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!("", concatenate_searches(Separator::Or as i32, &[]).unwrap());
|
assert_eq!("", concatenate_searches(BoolSeparator::Or, &[]).unwrap());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue