Use of take() instead of unwrap()

main
Mauro D 2022-07-17 17:20:24 +00:00
parent 5cc21ed1b5
commit 8b20a25461
23 changed files with 165 additions and 39 deletions

View File

@ -23,10 +23,11 @@ pub struct UploadResponse {
impl Client {
pub async fn upload(
&self,
account_id: &str,
account_id: Option<&str>,
blob: Vec<u8>,
content_type: Option<&str>,
) -> crate::Result<UploadResponse> {
let account_id = account_id.unwrap_or(self.default_account_id());
let mut upload_url =
String::with_capacity(self.session().upload_url().len() + account_id.len());
@ -83,7 +84,7 @@ impl UploadResponse {
self.size
}
pub fn unwrap_blob_id(self) -> String {
self.blob_id
pub fn take_blob_id(&mut self) -> String {
std::mem::take(&mut self.blob_id)
}
}

View File

@ -68,8 +68,8 @@ impl<O: ChangesObject> ChangesResponse<O> {
&self.account_id
}
pub fn unwrap_account_id(self) -> String {
self.account_id
pub fn take_account_id(&mut self) -> String {
std::mem::take(&mut self.account_id)
}
pub fn old_state(&self) -> &str {
@ -80,8 +80,8 @@ impl<O: ChangesObject> ChangesResponse<O> {
&self.new_state
}
pub fn unwrap_new_state(self) -> String {
self.new_state
pub fn take_new_state(&mut self) -> String {
std::mem::take(&mut self.new_state)
}
pub fn has_more_changes(&self) -> bool {

View File

@ -132,6 +132,12 @@ impl<O: SetObject> CopyResponse<O> {
}
}
pub fn take_created(&mut self) -> Option<Vec<O>> {
self.created
.take()
.map(|map| map.into_iter().map(|(_, v)| v).collect())
}
pub fn created_ids(&self) -> Option<impl Iterator<Item = &String>> {
self.created.as_ref().map(|map| map.keys())
}

View File

@ -111,7 +111,7 @@ impl<O> GetResponse<O> {
&self.state
}
pub fn unwrap_state(&mut self) -> String {
pub fn take_state(&mut self) -> String {
std::mem::take(&mut self.state)
}
@ -123,14 +123,14 @@ impl<O> GetResponse<O> {
&self.not_found
}
pub fn unwrap_list(&mut self) -> Vec<O> {
pub fn take_list(&mut self) -> Vec<O> {
std::mem::take(&mut self.list)
}
pub fn pop(&mut self) -> Option<O> {
self.list.pop()
}
pub fn unwrap_not_found(&mut self) -> Vec<String> {
pub fn take_not_found(&mut self) -> Vec<String> {
std::mem::take(&mut self.not_found)
}
}

View File

@ -187,8 +187,8 @@ impl QueryResponse {
self.ids[pos].as_str()
}
pub fn unwrap_ids(self) -> Vec<String> {
self.ids
pub fn take_ids(&mut self) -> Vec<String> {
std::mem::take(&mut self.ids)
}
pub fn total(&self) -> Option<usize> {
@ -203,7 +203,7 @@ impl QueryResponse {
self.position
}
pub fn unwrap_query_state(&mut self) -> String {
pub fn take_query_state(&mut self) -> String {
std::mem::take(&mut self.query_state)
}
@ -235,6 +235,11 @@ impl<A> Comparator<A> {
self
}
pub fn is_ascending(mut self, is_ascending: bool) -> Self {
self.is_ascending = is_ascending;
self
}
pub fn collation(mut self, collation: String) -> Self {
self.collation = Some(collation);
self
@ -254,6 +259,13 @@ impl<T> From<T> for Filter<T> {
}
impl<T> Filter<T> {
pub fn operator(operator: Operator, conditions: Vec<Filter<T>>) -> Self {
Filter::FilterOperator(FilterOperator {
operator,
conditions,
})
}
pub fn and<U, V>(conditions: U) -> Self
where
U: IntoIterator<Item = V>,

View File

@ -262,6 +262,10 @@ impl TaggedMethodResponse {
)
}
pub fn unwrap_method_response(self) -> MethodResponse {
self.response
}
pub fn unwrap_copy_blob(self) -> crate::Result<CopyBlobResponse> {
match self.response {
MethodResponse::CopyBlob(response) => Ok(response),

View File

@ -234,7 +234,7 @@ impl<O: SetObject> SetResponse<O> {
self.new_state.as_deref()
}
pub fn unwrap_new_state(&mut self) -> Option<String> {
pub fn take_new_state(&mut self) -> Option<String> {
self.new_state.take()
}
@ -280,11 +280,17 @@ impl<O: SetObject> SetResponse<O> {
self.updated.as_ref().map(|map| map.keys())
}
pub fn take_updated_ids(&mut self) -> Option<Vec<String>> {
self.updated
.take()
.map(|map| map.into_iter().map(|(k, _)| k).collect())
}
pub fn destroyed_ids(&self) -> Option<impl Iterator<Item = &String>> {
self.destroyed.as_ref().map(|list| list.iter())
}
pub fn unwrap_destroyed_ids(&mut self) -> Option<Vec<String>> {
pub fn take_destroyed_ids(&mut self) -> Option<Vec<String>> {
self.destroyed.take()
}

View File

@ -10,22 +10,26 @@ impl Email<Get> {
self.id.as_deref()
}
pub fn unwrap_id(self) -> String {
self.id.unwrap_or_default()
pub fn take_id(&mut self) -> String {
self.id.take().unwrap_or_default()
}
pub fn blob_id(&self) -> Option<&str> {
self.blob_id.as_deref()
}
pub fn unwrap_blob_id(self) -> String {
self.blob_id.unwrap()
pub fn take_blob_id(&mut self) -> String {
self.blob_id.take().unwrap_or_default()
}
pub fn thread_id(&self) -> Option<&str> {
self.thread_id.as_deref()
}
pub fn take_thread_id(&mut self) -> Option<String> {
self.thread_id.take()
}
pub fn mailbox_ids(&self) -> Vec<&str> {
self.mailbox_ids
.as_ref()
@ -74,26 +78,58 @@ impl Email<Get> {
self.sender.as_deref()
}
pub fn take_sender(&mut self) -> Option<Vec<EmailAddress>> {
self.sender.take()
}
pub fn from(&self) -> Option<&[EmailAddress]> {
self.from.as_deref()
}
pub fn take_from(&mut self) -> Option<Vec<EmailAddress>> {
self.from.take()
}
pub fn reply_to(&self) -> Option<&[EmailAddress]> {
self.reply_to.as_deref()
}
pub fn take_reply_to(&mut self) -> Option<Vec<EmailAddress>> {
self.reply_to.take()
}
pub fn to(&self) -> Option<&[EmailAddress]> {
self.to.as_deref()
}
pub fn take_to(&mut self) -> Option<Vec<EmailAddress>> {
self.to.take()
}
pub fn cc(&self) -> Option<&[EmailAddress]> {
self.cc.as_deref()
}
pub fn take_cc(&mut self) -> Option<Vec<EmailAddress>> {
self.cc.take()
}
pub fn bcc(&self) -> Option<&[EmailAddress]> {
self.bcc.as_deref()
}
pub fn take_bcc(&mut self) -> Option<Vec<EmailAddress>> {
self.bcc.take()
}
pub fn subject(&self) -> Option<&str> {
self.subject.as_deref()
}
pub fn take_subject(&mut self) -> Option<String> {
self.subject.take()
}
pub fn sent_at(&self) -> Option<i64> {
self.sent_at.as_ref().map(|v| v.timestamp())
}
@ -134,6 +170,10 @@ impl Email<Get> {
self.preview.as_deref()
}
pub fn take_preview(&mut self) -> Option<String> {
self.preview.take()
}
#[cfg(feature = "debug")]
pub fn into_test(self) -> super::TestEmail {
self.into()
@ -216,6 +256,10 @@ impl EmailAddress<Get> {
pub fn email(&self) -> &str {
self.email.as_str()
}
pub fn unwrap(self) -> (String, Option<String>) {
(self.email, self.name)
}
}
impl EmailAddressGroup<Get> {

View File

@ -59,9 +59,9 @@ impl Client {
W: Into<String>,
{
let blob_id = self
.upload(account_id, raw_message, None)
.upload(account_id.into(), raw_message, None)
.await?
.unwrap_blob_id();
.take_blob_id();
let mut request = self.build();
let import_request = request
.import_email()
@ -156,7 +156,7 @@ impl Client {
request
.send_single::<EmailGetResponse>()
.await
.map(|mut r| r.unwrap_list().pop())
.map(|mut r| r.take_list().pop())
}
pub async fn email_changes(

View File

@ -613,7 +613,7 @@ impl Display for HeaderForm {
}
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum BodyProperty {
PartId,
BlobId,

View File

@ -94,6 +94,20 @@ pub enum Filter {
#[serde(rename = "header")]
value: Vec<String>,
},
// Stalwart specific
Id {
#[serde(rename = "id")]
value: Vec<String>,
},
SentBefore {
#[serde(rename = "sentBefore")]
value: DateTime<Utc>,
},
SentAfter {
#[serde(rename = "sentAfter")]
value: DateTime<Utc>,
},
}
#[derive(Serialize, Debug, Clone)]
@ -107,6 +121,8 @@ pub enum Comparator {
From,
#[serde(rename = "to")]
To,
#[serde(rename = "cc")]
Cc,
#[serde(rename = "subject")]
Subject,
#[serde(rename = "sentAt")]
@ -240,6 +256,29 @@ impl Filter {
}
Filter::Header { value }
}
// Stalwart JMAP specific
pub fn id<U, V>(value: U) -> Self
where
U: IntoIterator<Item = V>,
V: Into<String>,
{
Filter::Id {
value: value.into_iter().map(|v| v.into()).collect(),
}
}
pub fn sent_before(value: i64) -> Self {
Filter::SentBefore {
value: from_timestamp(value),
}
}
pub fn sent_after(value: i64) -> Self {
Filter::SentAfter {
value: from_timestamp(value),
}
}
}
impl Comparator {
@ -259,6 +298,10 @@ impl Comparator {
query::Comparator::new(Comparator::To)
}
pub fn cc() -> query::Comparator<Comparator> {
query::Comparator::new(Comparator::Cc)
}
pub fn subject() -> query::Comparator<Comparator> {
query::Comparator::new(Comparator::Subject)
}

View File

@ -9,8 +9,8 @@ impl EmailSubmission<Get> {
self.id.as_deref()
}
pub fn unwrap_id(self) -> String {
self.id.unwrap_or_default()
pub fn take_id(&mut self) -> String {
self.id.take().unwrap_or_default()
}
pub fn identity_id(&self) -> Option<&str> {

View File

@ -99,7 +99,7 @@ impl Client {
request
.send_single::<EmailSubmissionGetResponse>()
.await
.map(|mut r| r.unwrap_list().pop())
.map(|mut r| r.take_list().pop())
}
pub async fn email_submission_query(

View File

@ -51,7 +51,17 @@ impl Changes {
self.changes.get(account_id).map(|changes| changes.iter())
}
pub fn has_type(&self, type_: TypeState) -> bool {
self.changes
.values()
.any(|changes| changes.contains_key(&type_))
}
pub fn into_inner(self) -> HashMap<String, HashMap<TypeState, String>> {
self.changes
}
pub fn is_empty(&self) -> bool {
!self.changes.values().any(|changes| !changes.is_empty())
}
}

View File

@ -7,8 +7,8 @@ impl Identity<Get> {
self.id.as_deref()
}
pub fn unwrap_id(self) -> String {
self.id.unwrap_or_default()
pub fn take_id(&mut self) -> String {
self.id.take().unwrap_or_default()
}
pub fn name(&self) -> Option<&str> {

View File

@ -54,7 +54,7 @@ impl Client {
request
.send_single::<IdentityGetResponse>()
.await
.map(|mut r| r.unwrap_list().pop())
.map(|mut r| r.take_list().pop())
}
pub async fn identity_changes(

View File

@ -9,8 +9,8 @@ impl Mailbox<Get> {
self.id.as_deref()
}
pub fn unwrap_id(self) -> String {
self.id.unwrap_or_default()
pub fn take_id(&mut self) -> String {
self.id.take().unwrap_or_default()
}
pub fn name(&self) -> Option<&str> {

View File

@ -145,7 +145,7 @@ impl Client {
request
.send_single::<MailboxGetResponse>()
.await
.map(|mut r| r.unwrap_list().pop())
.map(|mut r| r.take_list().pop())
}
pub async fn mailbox_query(

View File

@ -9,8 +9,8 @@ impl Principal<Get> {
self.id.as_deref()
}
pub fn unwrap_id(self) -> String {
self.id.unwrap_or_default()
pub fn take_id(&mut self) -> String {
self.id.take().unwrap_or_default()
}
pub fn ptype(&self) -> Option<&Type> {

View File

@ -212,7 +212,7 @@ impl Client {
request
.send_single::<PrincipalGetResponse>()
.await
.map(|mut r| r.unwrap_list().pop())
.map(|mut r| r.take_list().pop())
}
pub async fn principal_query(

View File

@ -7,8 +7,8 @@ impl PushSubscription<Get> {
self.id.as_deref()
}
pub fn unwrap_id(self) -> String {
self.id.unwrap_or_default()
pub fn take_id(&mut self) -> String {
self.id.take().unwrap_or_default()
}
pub fn device_client_id(&self) -> Option<&str> {

View File

@ -18,7 +18,7 @@ impl Client {
request
.send_single::<ThreadGetResponse>()
.await
.map(|mut r| r.unwrap_list().pop())
.map(|mut r| r.take_list().pop())
}
}

View File

@ -100,7 +100,7 @@ impl Client {
request
.send_single::<VacationResponseGetResponse>()
.await
.map(|mut r| r.unwrap_list().pop())
.map(|mut r| r.take_list().pop())
}
pub async fn vacation_response_destroy(&self) -> crate::Result<()> {