Return a Result<Option<Message>> for get_draft

The function can fail, so we need to still have an error return as
well as a no-draft return.

Add tests.
This commit is contained in:
Floris Bruynooghe
2019-09-14 00:50:24 +02:00
parent db20ecf985
commit 8a7143b791
3 changed files with 68 additions and 16 deletions

View File

@@ -553,12 +553,23 @@ pub unsafe extern "C" fn dc_get_draft(context: *mut dc_context_t, chat_id: u32)
}
let context = &*context;
if let Some(message) = chat::get_draft(context, chat_id) {
let ffi_msg = MessageWrapper { context, message };
return Box::into_raw(Box::new(ffi_msg));
};
ptr::null_mut()
match chat::get_draft(context, chat_id) {
Ok(Some(draft)) => {
let ffi_msg = MessageWrapper {
context,
message: draft,
};
Box::into_raw(Box::new(ffi_msg))
}
Ok(None) => ptr::null_mut(),
Err(err) => {
error!(
context,
"Failed to get draft for chat #{}: {}", chat_id, err
);
ptr::null_mut()
}
}
}
#[no_mangle]