conf/accounts.rs: use QueryTrait when search_backend is None

memfd
Manos Pitsidianakis 2020-07-25 18:06:42 +03:00
parent 3f8aa560f0
commit 1cc1b0604c
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
2 changed files with 11 additions and 25 deletions

View File

@ -1606,10 +1606,11 @@ impl Account {
mailbox_hash: MailboxHash,
) -> ResultFuture<SmallVec<[EnvelopeHash; 512]>> {
use melib::parsec::Parser;
use melib::search::QueryTrait;
let query = melib::search::query().parse(search_term)?.1;
match self.settings.conf.search_backend {
#[cfg(feature = "sqlite3")]
crate::conf::SearchBackend::Sqlite3 => crate::sqlite3::search(search_term, _sort),
crate::conf::SearchBackend::Sqlite3 => crate::sqlite3::search(&query, _sort),
crate::conf::SearchBackend::None => {
if self.backend_capabilities.supports_search {
self.backend
@ -1619,27 +1620,11 @@ impl Account {
} else {
let mut ret = SmallVec::new();
let envelopes = self.collection.envelopes.read().unwrap();
for &env_hash in self.collection.get_mailbox(mailbox_hash).iter() {
let envelope = &envelopes[&env_hash];
if envelope.subject().contains(&search_term) {
ret.push(env_hash);
continue;
}
if envelope.field_from_to_string().contains(&search_term) {
ret.push(env_hash);
continue;
}
let op = if let Ok(op) = self.operation(env_hash) {
op
} else {
continue;
};
let body = envelope.body(op)?;
let decoded = decode_rec(&body, None);
let body_text = String::from_utf8_lossy(&decoded);
if body_text.contains(&search_term) {
ret.push(env_hash);
if let Some(envelope) = envelopes.get(&env_hash) {
if envelope.is_match(&query) {
ret.push(env_hash);
}
}
}
Ok(Box::pin(async { Ok(ret) }))

View File

@ -21,10 +21,9 @@
/*! Use an sqlite3 database for fast searching.
*/
use crate::melib::parsec::Parser;
use crate::melib::ResultIntoMeliError;
use melib::search::{
escape_double_quote, query,
escape_double_quote,
Query::{self, *},
};
use melib::{
@ -338,7 +337,7 @@ pub fn index(context: &mut crate::state::Context, account_index: usize) -> Resul
}
pub fn search(
term: &str,
query: &Query,
(sort_field, sort_order): (SortField, SortOrder),
) -> ResultFuture<SmallVec<[EnvelopeHash; 512]>> {
let db_path = melib_sqlite3::db_path(DB_NAME)?;
@ -364,7 +363,7 @@ pub fn search(
.prepare(
debug!(format!(
"SELECT hash FROM envelopes WHERE {} ORDER BY {} {};",
query_to_sql(&query().parse(term)?.1),
query_to_sql(&query),
sort_field,
sort_order
))
@ -496,6 +495,8 @@ pub fn query_to_sql(q: &Query) -> String {
#[test]
fn test_query_to_sql() {
use melib::parsec::Parser;
use melib::search::query;
assert_eq!(
"(subject LIKE \"%test%\" ) AND (body_text LIKE \"%i%\" ) ",
&query_to_sql(&query().parse_complete("subject: test and i").unwrap().1)