Replace unstable try_trait with a job_try! macro

This commit is contained in:
Alexander Krotov
2019-12-27 11:14:05 +01:00
parent fe2011742d
commit 83c98c2d55
3 changed files with 26 additions and 33 deletions

View File

@@ -49,26 +49,19 @@ pub enum Try {
RetryLater,
}
impl std::ops::Try for Try {
type Ok = Try;
type Error = Error;
fn into_result(self) -> std::result::Result<Self::Ok, Self::Error> {
match self {
Self::Finished(Ok(())) => Ok(Self::Finished(Ok(()))),
Self::Finished(Err(err)) => Err(err),
Self::RetryNow => Ok(Self::RetryNow),
Self::RetryLater => Ok(Self::RetryLater),
#[macro_export]
macro_rules! job_try {
($expr:expr) => {
match $expr {
::std::result::Result::Ok(val) => val,
::std::result::Result::Err(err) => {
return $crate::job::Try::Finished(Err(err.into()));
}
}
}
fn from_error(e: Self::Error) -> Self {
Self::Finished(Err(e))
}
fn from_ok(_: Self::Ok) -> Self {
Self::Finished(Ok(()))
}
};
($expr:expr,) => {
$crate::job_try!($expr)
};
}
impl Default for Thread {
@@ -189,16 +182,16 @@ impl Job {
}
}
let filename = self
let filename = job_try!(job_try!(self
.param
.get_path(Param::File, context)
.map_err(|_| format_err!("Can't get filename"))?
.ok_or_else(|| format_err!("Can't get filename"))?;
let body = dc_read_file(context, &filename)?;
let recipients = self.param.get(Param::Recipients).ok_or_else(|| {
.map_err(|_| format_err!("Can't get filename")))
.ok_or_else(|| format_err!("Can't get filename")));
let body = job_try!(dc_read_file(context, &filename));
let recipients = job_try!(self.param.get(Param::Recipients).ok_or_else(|| {
warn!(context, "Missing recipients for job {}", self.job_id);
format_err!("Missing recipients")
})?;
}));
let recipients_list = recipients
.split('\x1e')
@@ -268,7 +261,7 @@ impl Job {
fn MoveMsg(&mut self, context: &Context) -> Try {
let imap_inbox = &context.inbox_thread.read().unwrap().imap;
let msg = Message::load_from_db(context, MsgId::new(self.foreign_id))?;
let msg = job_try!(Message::load_from_db(context, MsgId::new(self.foreign_id)));
if let Err(err) = imap_inbox.ensure_configured_folders(context, true) {
warn!(context, "could not configure folders: {:?}", err);
@@ -306,7 +299,7 @@ impl Job {
fn DeleteMsgOnImap(&mut self, context: &Context) -> Try {
let imap_inbox = &context.inbox_thread.read().unwrap().imap;
let mut msg = Message::load_from_db(context, MsgId::new(self.foreign_id))?;
let mut msg = job_try!(Message::load_from_db(context, MsgId::new(self.foreign_id)));
if !msg.rfc724_mid.is_empty() {
if message::rfc724_mid_cnt(context, &msg.rfc724_mid) > 1 {
@@ -354,7 +347,7 @@ impl Job {
fn MarkseenMsgOnImap(&mut self, context: &Context) -> Try {
let imap_inbox = &context.inbox_thread.read().unwrap().imap;
let msg = Message::load_from_db(context, MsgId::new(self.foreign_id))?;
let msg = job_try!(Message::load_from_db(context, MsgId::new(self.foreign_id)));
let folder = msg.server_folder.as_ref().unwrap();
match imap_inbox.set_seen(context, folder, msg.server_uid) {

View File

@@ -4,7 +4,6 @@
#![allow(clippy::match_bool)]
#![feature(ptr_wrapping_offset_from)]
#![feature(drain_filter)]
#![feature(try_trait)]
#[macro_use]
extern crate failure_derive;
@@ -43,6 +42,7 @@ mod e2ee;
mod imap;
mod imap_client;
pub mod imex;
#[macro_use]
pub mod job;
mod job_thread;
pub mod key;

View File

@@ -640,11 +640,11 @@ pub fn JobMaybeSendLocationsEnded(context: &Context, job: &mut Job) -> Try {
let chat_id = job.foreign_id;
let (send_begin, send_until) = context.sql.query_row(
let (send_begin, send_until) = job_try!(context.sql.query_row(
"SELECT locations_send_begin, locations_send_until FROM chats WHERE id=?",
params![chat_id as i32],
|row| Ok((row.get::<_, i64>(0)?, row.get::<_, i64>(1)?)),
)?;
));
if !(send_begin != 0 && time() <= send_until) {
// still streaming -
@@ -652,10 +652,10 @@ pub fn JobMaybeSendLocationsEnded(context: &Context, job: &mut Job) -> Try {
// do not un-schedule pending DC_MAYBE_SEND_LOC_ENDED jobs
if !(send_begin == 0 && send_until == 0) {
// not streaming, device-message already sent
context.sql.execute(
job_try!(context.sql.execute(
"UPDATE chats SET locations_send_begin=0, locations_send_until=0 WHERE id=?",
params![chat_id as i32],
)?;
));
let stock_str = context.stock_system_msg(StockMessage::MsgLocationDisabled, "", "", 0);
chat::add_info_msg(context, chat_id, stock_str);