summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-01-05 23:42:26 +0100
committerMatthias Beyer <mail@beyermatthias.de>2020-01-05 23:42:58 +0100
commit2916debd5536274959bfe903e8bb30307deb45be (patch)
treeacffe5ad7f54658abe16ac2be7c54d084aff6cfc
parentdbba9ae9ea19a36fae9778c630f4234029040e6e (diff)
Replace execute{,_atomic}() functions with new implementation that can return objects
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--lib/domain/libimagmail/src/mail.rs4
-rw-r--r--lib/domain/libimagmail/src/notmuch/connection.rs21
-rw-r--r--lib/domain/libimagmail/src/store.rs40
3 files changed, 18 insertions, 47 deletions
diff --git a/lib/domain/libimagmail/src/mail.rs b/lib/domain/libimagmail/src/mail.rs
index 159821ea..082e6067 100644
--- a/lib/domain/libimagmail/src/mail.rs
+++ b/lib/domain/libimagmail/src/mail.rs
@@ -81,7 +81,7 @@ impl<'a> LoadedMail<'a> {
.read_string("mail.id")?
.ok_or_else(|| format_err!("Missing header: 'mail.id' in {}", entry.get_location()))?;
- conn.process(|db| {
+ conn.execute(|db| {
if let Some(msg) = db.find_message(&id)? {
let id = msg.id().into_owned();
let thread_id = msg.thread_id().into_owned();
@@ -144,7 +144,7 @@ impl<'a> LoadedMail<'a> {
pub fn threads(&self, conn: &NotmuchConnection) -> Result<Vec<Thread>> {
let id = self.get_id();
- conn.process(|db| {
+ conn.execute(|db| {
db.create_query(&format!("thread:{}", id))?
.search_threads()
.map_err(Error::from)
diff --git a/lib/domain/libimagmail/src/notmuch/connection.rs b/lib/domain/libimagmail/src/notmuch/connection.rs
index c8d77f7d..06aebb98 100644
--- a/lib/domain/libimagmail/src/notmuch/connection.rs
+++ b/lib/domain/libimagmail/src/notmuch/connection.rs
@@ -81,8 +81,8 @@ impl NotmuchConnection {
Ok(())
}
- pub(crate) fn execute<F>(&self, f: F) -> Result<()>
- where F: FnOnce(&Database) -> Result<()>
+ pub(crate) fn execute<F, T>(&self, f: F) -> Result<T>
+ where F: FnOnce(&Database) -> Result<T>
{
self.db
.lock()
@@ -91,22 +91,7 @@ impl NotmuchConnection {
.and_then(|lock| f(&lock))
}
- pub(crate) fn execute_atomic<F>(&self, f: F) -> Result<()>
- where F: FnOnce(&Database) -> Result<()>
- {
- self.db
- .lock()
- .map_err(|_| ErrorMsg::LockError)
- .map_err(Error::from)
- .and_then(|lock| {
- lock.begin_atomic()?;
- let r = f(&lock);
- lock.end_atomic()?;
- r
- })
- }
-
- pub(crate) fn process<F, T>(&self, f: F) -> Result<T>
+ pub(crate) fn execute_atomic<F, T>(&self, f: F) -> Result<T>
where F: FnOnce(&Database) -> Result<T>
{
self.db
diff --git a/lib/domain/libimagmail/src/store.rs b/lib/domain/libimagmail/src/store.rs
index 95626e88..86e4c6c3 100644
--- a/lib/domain/libimagmail/src/store.rs
+++ b/lib/domain/libimagmail/src/store.rs
@@ -22,6 +22,7 @@ use std::borrow::Cow;
use failure::Fallible as Result;
use toml_query::insert::TomlValueInsertExt;
use toml::Value;
+use notmuch_rs::Query;
use libimagstore::store::Store;
use libimagstore::store::FileLockEntry;
@@ -71,43 +72,28 @@ impl<'a> MailStoreWithConnection<'a> {
}
pub fn query(&self, q: &str) -> Result<Vec<FileLockEntry<'a>>> {
- let mut entries = vec![];
-
self.connection
.execute(|db| {
let query = db.create_query(q)?;
- query.search_messages()?
- .map(|msg| {
- self.get_entry_for_id(msg.id())?
- .ok_or_else(|| format_err!("Message with id '{}' not found", msg.id()))
- .map(|entry| entries.push(entry))
- })
- .collect::<Result<Vec<_>>>()?;
-
- Ok(())
- })?;
-
- Ok(entries)
+ self.execute_query(query)
+ })
}
pub fn query_atomic(&self, q: &str) -> Result<Vec<FileLockEntry<'a>>> {
- let mut entries = vec![];
-
self.connection
.execute_atomic(|db| {
let query = db.create_query(q)?;
- query.search_messages()?
- .map(|msg| {
- self.get_entry_for_id(msg.id())?
- .ok_or_else(|| format_err!("Message with id '{}' not found", msg.id()))
- .map(|entry| entries.push(entry))
- })
- .collect::<Result<Vec<_>>>()?;
-
- Ok(())
- })?;
+ self.execute_query(query)
+ })
+ }
- Ok(entries)
+ fn execute_query<'d>(&self, q: Query<'d>) -> Result<Vec<FileLockEntry<'a>>> {
+ q.search_messages()?
+ .map(|msg| {
+ self.get_entry_for_id(msg.id())?
+ .ok_or_else(|| format_err!("Message with id '{}' not found", msg.id()))
+ })
+ .collect::<Result<Vec<_>>>()
}
pub fn get_mail_by_id(&self, id: &str) -> Result<Option<FileLockEntry>> {