MailBackend: change get() ret type to Result<_>

async
Manos Pitsidianakis 2020-07-05 17:22:06 +03:00
parent a7e177586a
commit 94e0aa4fe7
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
9 changed files with 74 additions and 34 deletions

View File

@ -302,7 +302,7 @@ pub trait MailBackend: ::std::fmt::Debug + Send + Sync {
Err(MeliError::new("Unimplemented."))
}
fn connect(&mut self) {}
fn get(&mut self, mailbox: &Mailbox) -> Async<Result<Vec<Envelope>>>;
fn get(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>>;
fn get_async(
&mut self,
_mailbox: &Mailbox,

View File

@ -208,7 +208,7 @@ impl MailBackend for ImapType {
});
}
fn get(&mut self, mailbox: &Mailbox) -> Async<Result<Vec<Envelope>>> {
fn get(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
let mut w = AsyncBuilder::new();
let handle = {
let tx = w.tx();
@ -517,7 +517,7 @@ impl MailBackend for ImapType {
};
Box::new(closure)
};
w.build(handle)
Ok(w.build(handle))
}
fn refresh(

View File

@ -304,8 +304,8 @@ impl MailBackend for ImapType {
*/
}
fn get(&mut self, _mailbox: &Mailbox) -> Async<Result<Vec<Envelope>>> {
unimplemented!()
fn get(&mut self, _mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
Err(MeliError::new("Unimplemented."))
}
fn refresh(
@ -321,7 +321,7 @@ impl MailBackend for ImapType {
_sender: RefreshEventConsumer,
_work_context: WorkContext,
) -> Result<std::thread::ThreadId> {
Ok(std::thread::current().id())
Err(MeliError::new("Unimplemented."))
}
fn watch_async(&self, sender: RefreshEventConsumer) -> ResultFuture<()> {

View File

@ -203,7 +203,7 @@ impl MailBackend for JmapType {
}
}
fn get(&mut self, mailbox: &Mailbox) -> Async<Result<Vec<Envelope>>> {
fn get(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
let mut w = AsyncBuilder::new();
let mailboxes = self.mailboxes.clone();
let store = self.store.clone();
@ -224,7 +224,7 @@ impl MailBackend for JmapType {
};
Box::new(closure)
};
w.build(handle)
Ok(w.build(handle))
}
fn watch(

View File

@ -195,8 +195,8 @@ impl MailBackend for MaildirType {
Ok(Box::pin(async { res }))
}
fn get(&mut self, mailbox: &Mailbox) -> Async<Result<Vec<Envelope>>> {
self.multicore(4, mailbox)
fn get(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
Ok(self.multicore(4, mailbox))
}
fn get_async(

View File

@ -651,7 +651,7 @@ impl MailBackend for MboxType {
Ok(())
}
fn get(&mut self, mailbox: &Mailbox) -> Async<Result<Vec<Envelope>>> {
fn get(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
let mut w = AsyncBuilder::new();
let handle = {
let tx = w.tx();
@ -707,7 +707,7 @@ impl MailBackend for MboxType {
};
Box::new(closure)
};
w.build(handle)
Ok(w.build(handle))
}
fn watch(

View File

@ -321,7 +321,7 @@ impl MailBackend for NotmuchDb {
fn is_online(&self) -> Result<()> {
Ok(())
}
fn get(&mut self, mailbox: &Mailbox) -> Async<Result<Vec<Envelope>>> {
fn get(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
let mut w = AsyncBuilder::new();
let mailbox_hash = mailbox.hash();
let database = NotmuchDb::new_connection(self.path.as_path(), self.lib.clone(), false);
@ -394,7 +394,7 @@ impl MailBackend for NotmuchDb {
};
Box::new(closure)
};
w.build(handle)
Ok(w.build(handle))
}
fn watch(

View File

@ -507,12 +507,18 @@ impl Account {
self.active_jobs.insert(job_id, JobRequest::Get(*h, rcvr));
}
} else {
entry.worker = Account::new_worker(
entry.worker = match Account::new_worker(
f.clone(),
&mut self.backend,
&self.work_context,
self.notify_fn.clone(),
);
) {
Ok(v) => v,
Err(err) => {
entry.status = MailboxStatus::Failed(err);
None
}
};
}
}
});
@ -532,8 +538,8 @@ impl Account {
backend: &Arc<RwLock<Box<dyn MailBackend>>>,
work_context: &WorkContext,
notify_fn: Arc<NotifyFn>,
) -> Worker {
let mut mailbox_handle = backend.write().unwrap().get(&mailbox);
) -> Result<Worker> {
let mut mailbox_handle = backend.write().unwrap().get(&mailbox)?;
let mut builder = AsyncBuilder::new();
let our_tx = builder.tx();
let mailbox_hash = mailbox.hash();
@ -597,7 +603,7 @@ impl Account {
if let Some(w) = w.work() {
work_context.new_work.send(w).unwrap();
}
Some(w)
Ok(Some(w))
}
pub fn reload(&mut self, event: RefreshEvent, mailbox_hash: MailboxHash) -> Option<UIEvent> {
if !self.mailbox_entries[&mailbox_hash].status.is_available() {
@ -777,20 +783,35 @@ impl Account {
return Some(EnvelopeRemove(env_hash, thread_hash));
}
RefreshEventKind::Rescan => {
let handle = Account::new_worker(
let handle = match Account::new_worker(
self.mailbox_entries[&mailbox_hash].ref_mailbox.clone(),
&mut self.backend,
&self.work_context,
self.notify_fn.clone(),
);
) {
Ok(v) => v,
Err(err) => {
let ret = Some(Notification(
None,
err.to_string(),
Some(crate::types::NotificationType::ERROR),
));
self.mailbox_entries
.entry(mailbox_hash)
.and_modify(|entry| {
entry.status = MailboxStatus::Failed(err);
});
return ret;
}
};
self.mailbox_entries
.entry(mailbox_hash)
.and_modify(|entry| {
entry.worker = handle;
});
}
RefreshEventKind::Failure(e) => {
debug!("RefreshEvent Failure: {}", e.to_string());
RefreshEventKind::Failure(err) => {
debug!("RefreshEvent Failure: {}", err.to_string());
/*
context
.1
@ -802,6 +823,11 @@ impl Account {
.expect("Could not send event on main channel");
*/
self.watch();
return Some(Notification(
None,
err.to_string(),
Some(crate::types::NotificationType::ERROR),
));
}
}
}
@ -982,12 +1008,22 @@ impl Account {
}
}
} else if self.mailbox_entries[&mailbox_hash].worker.is_none() {
let handle = Account::new_worker(
let handle = match Account::new_worker(
self.mailbox_entries[&mailbox_hash].ref_mailbox.clone(),
&mut self.backend,
&self.work_context,
self.notify_fn.clone(),
);
) {
Ok(v) => v,
Err(err) => {
self.mailbox_entries.entry(mailbox_hash).and_modify(
|entry| {
entry.status = MailboxStatus::Failed(err);
},
);
return Err(0);
}
};
self.mailbox_entries
.entry(mailbox_hash)
.and_modify(|entry| {
@ -1233,19 +1269,23 @@ impl Account {
parent.ref_mailbox = mailboxes.remove(&parent_hash).unwrap();
});
}
let (status, worker) = match Account::new_worker(
mailboxes[&mailbox_hash].clone(),
&mut self.backend,
&self.work_context,
self.notify_fn.clone(),
) {
Ok(v) => (MailboxStatus::Parsing(0, 0), v),
Err(err) => (MailboxStatus::Failed(err), None),
};
self.mailbox_entries.insert(
mailbox_hash,
MailboxEntry {
name: mailboxes[&mailbox_hash].path().to_string(),
status: MailboxStatus::Parsing(0, 0),
status,
conf: new,
worker: Account::new_worker(
mailboxes[&mailbox_hash].clone(),
&mut self.backend,
&self.work_context,
self.notify_fn.clone(),
),
worker,
ref_mailbox: mailboxes.remove(&mailbox_hash).unwrap(),
},
);

View File

@ -85,7 +85,7 @@ impl MailBackend for PluginBackend {
fn connect(&mut self) {}
fn get(&mut self, mailbox: &Mailbox) -> Async<Result<Vec<Envelope>>> {
fn get(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
let mut w = AsyncBuilder::new();
let _mailbox_hash = mailbox.hash();
let channel = self.channel.clone();
@ -179,7 +179,7 @@ impl MailBackend for PluginBackend {
};
Box::new(closure)
};
w.build(handle)
Ok(w.build(handle))
}
fn refresh(