From 8b20a25461de60d32d0949fc68e4461cccf903f9 Mon Sep 17 00:00:00 2001 From: Mauro D Date: Sun, 17 Jul 2022 17:20:24 +0000 Subject: [PATCH] Use of take() instead of unwrap() --- src/blob/upload.rs | 7 +++-- src/core/changes.rs | 8 ++--- src/core/copy.rs | 6 ++++ src/core/get.rs | 6 ++-- src/core/query.rs | 18 +++++++++-- src/core/response.rs | 4 +++ src/core/set.rs | 10 ++++-- src/email/get.rs | 52 +++++++++++++++++++++++++++++--- src/email/helpers.rs | 6 ++-- src/email/mod.rs | 2 +- src/email/query.rs | 43 ++++++++++++++++++++++++++ src/email_submission/get.rs | 4 +-- src/email_submission/helpers.rs | 2 +- src/event_source/mod.rs | 10 ++++++ src/identity/get.rs | 4 +-- src/identity/helpers.rs | 2 +- src/mailbox/get.rs | 4 +-- src/mailbox/helpers.rs | 2 +- src/principal/get.rs | 4 +-- src/principal/helpers.rs | 2 +- src/push_subscription/get.rs | 4 +-- src/thread/helpers.rs | 2 +- src/vacation_response/helpers.rs | 2 +- 23 files changed, 165 insertions(+), 39 deletions(-) diff --git a/src/blob/upload.rs b/src/blob/upload.rs index c47448d..d887941 100644 --- a/src/blob/upload.rs +++ b/src/blob/upload.rs @@ -23,10 +23,11 @@ pub struct UploadResponse { impl Client { pub async fn upload( &self, - account_id: &str, + account_id: Option<&str>, blob: Vec, content_type: Option<&str>, ) -> crate::Result { + 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) } } diff --git a/src/core/changes.rs b/src/core/changes.rs index 0884747..7e4cf51 100644 --- a/src/core/changes.rs +++ b/src/core/changes.rs @@ -68,8 +68,8 @@ impl ChangesResponse { &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 ChangesResponse { &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 { diff --git a/src/core/copy.rs b/src/core/copy.rs index f95ba6e..07e4bf8 100644 --- a/src/core/copy.rs +++ b/src/core/copy.rs @@ -132,6 +132,12 @@ impl CopyResponse { } } + pub fn take_created(&mut self) -> Option> { + self.created + .take() + .map(|map| map.into_iter().map(|(_, v)| v).collect()) + } + pub fn created_ids(&self) -> Option> { self.created.as_ref().map(|map| map.keys()) } diff --git a/src/core/get.rs b/src/core/get.rs index 669291a..84f202a 100644 --- a/src/core/get.rs +++ b/src/core/get.rs @@ -111,7 +111,7 @@ impl GetResponse { &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 GetResponse { &self.not_found } - pub fn unwrap_list(&mut self) -> Vec { + pub fn take_list(&mut self) -> Vec { std::mem::take(&mut self.list) } pub fn pop(&mut self) -> Option { self.list.pop() } - pub fn unwrap_not_found(&mut self) -> Vec { + pub fn take_not_found(&mut self) -> Vec { std::mem::take(&mut self.not_found) } } diff --git a/src/core/query.rs b/src/core/query.rs index c3f1a1a..1000564 100644 --- a/src/core/query.rs +++ b/src/core/query.rs @@ -187,8 +187,8 @@ impl QueryResponse { self.ids[pos].as_str() } - pub fn unwrap_ids(self) -> Vec { - self.ids + pub fn take_ids(&mut self) -> Vec { + std::mem::take(&mut self.ids) } pub fn total(&self) -> Option { @@ -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 Comparator { 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 From for Filter { } impl Filter { + pub fn operator(operator: Operator, conditions: Vec>) -> Self { + Filter::FilterOperator(FilterOperator { + operator, + conditions, + }) + } + pub fn and(conditions: U) -> Self where U: IntoIterator, diff --git a/src/core/response.rs b/src/core/response.rs index 94814d2..38b7194 100644 --- a/src/core/response.rs +++ b/src/core/response.rs @@ -262,6 +262,10 @@ impl TaggedMethodResponse { ) } + pub fn unwrap_method_response(self) -> MethodResponse { + self.response + } + pub fn unwrap_copy_blob(self) -> crate::Result { match self.response { MethodResponse::CopyBlob(response) => Ok(response), diff --git a/src/core/set.rs b/src/core/set.rs index 79a9cc1..d22e50b 100644 --- a/src/core/set.rs +++ b/src/core/set.rs @@ -234,7 +234,7 @@ impl SetResponse { self.new_state.as_deref() } - pub fn unwrap_new_state(&mut self) -> Option { + pub fn take_new_state(&mut self) -> Option { self.new_state.take() } @@ -280,11 +280,17 @@ impl SetResponse { self.updated.as_ref().map(|map| map.keys()) } + pub fn take_updated_ids(&mut self) -> Option> { + self.updated + .take() + .map(|map| map.into_iter().map(|(k, _)| k).collect()) + } + pub fn destroyed_ids(&self) -> Option> { self.destroyed.as_ref().map(|list| list.iter()) } - pub fn unwrap_destroyed_ids(&mut self) -> Option> { + pub fn take_destroyed_ids(&mut self) -> Option> { self.destroyed.take() } diff --git a/src/email/get.rs b/src/email/get.rs index 8ce7e6c..9979276 100644 --- a/src/email/get.rs +++ b/src/email/get.rs @@ -10,22 +10,26 @@ impl Email { 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 { + self.thread_id.take() + } + pub fn mailbox_ids(&self) -> Vec<&str> { self.mailbox_ids .as_ref() @@ -74,26 +78,58 @@ impl Email { self.sender.as_deref() } + pub fn take_sender(&mut self) -> Option> { + self.sender.take() + } + pub fn from(&self) -> Option<&[EmailAddress]> { self.from.as_deref() } + pub fn take_from(&mut self) -> Option> { + self.from.take() + } + + pub fn reply_to(&self) -> Option<&[EmailAddress]> { + self.reply_to.as_deref() + } + + pub fn take_reply_to(&mut self) -> Option> { + self.reply_to.take() + } + pub fn to(&self) -> Option<&[EmailAddress]> { self.to.as_deref() } + pub fn take_to(&mut self) -> Option> { + self.to.take() + } + pub fn cc(&self) -> Option<&[EmailAddress]> { self.cc.as_deref() } + pub fn take_cc(&mut self) -> Option> { + self.cc.take() + } + pub fn bcc(&self) -> Option<&[EmailAddress]> { self.bcc.as_deref() } + pub fn take_bcc(&mut self) -> Option> { + self.bcc.take() + } + pub fn subject(&self) -> Option<&str> { self.subject.as_deref() } + pub fn take_subject(&mut self) -> Option { + self.subject.take() + } + pub fn sent_at(&self) -> Option { self.sent_at.as_ref().map(|v| v.timestamp()) } @@ -134,6 +170,10 @@ impl Email { self.preview.as_deref() } + pub fn take_preview(&mut self) -> Option { + self.preview.take() + } + #[cfg(feature = "debug")] pub fn into_test(self) -> super::TestEmail { self.into() @@ -216,6 +256,10 @@ impl EmailAddress { pub fn email(&self) -> &str { self.email.as_str() } + + pub fn unwrap(self) -> (String, Option) { + (self.email, self.name) + } } impl EmailAddressGroup { diff --git a/src/email/helpers.rs b/src/email/helpers.rs index d08861f..af341cd 100644 --- a/src/email/helpers.rs +++ b/src/email/helpers.rs @@ -59,9 +59,9 @@ impl Client { W: Into, { 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::() .await - .map(|mut r| r.unwrap_list().pop()) + .map(|mut r| r.take_list().pop()) } pub async fn email_changes( diff --git a/src/email/mod.rs b/src/email/mod.rs index f5cd067..a8a04e4 100644 --- a/src/email/mod.rs +++ b/src/email/mod.rs @@ -613,7 +613,7 @@ impl Display for HeaderForm { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum BodyProperty { PartId, BlobId, diff --git a/src/email/query.rs b/src/email/query.rs index f7fd3ae..0560223 100644 --- a/src/email/query.rs +++ b/src/email/query.rs @@ -94,6 +94,20 @@ pub enum Filter { #[serde(rename = "header")] value: Vec, }, + + // Stalwart specific + Id { + #[serde(rename = "id")] + value: Vec, + }, + SentBefore { + #[serde(rename = "sentBefore")] + value: DateTime, + }, + SentAfter { + #[serde(rename = "sentAfter")] + value: DateTime, + }, } #[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(value: U) -> Self + where + U: IntoIterator, + V: Into, + { + 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 { + query::Comparator::new(Comparator::Cc) + } + pub fn subject() -> query::Comparator { query::Comparator::new(Comparator::Subject) } diff --git a/src/email_submission/get.rs b/src/email_submission/get.rs index 59141b3..cf4883e 100644 --- a/src/email_submission/get.rs +++ b/src/email_submission/get.rs @@ -9,8 +9,8 @@ impl EmailSubmission { 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> { diff --git a/src/email_submission/helpers.rs b/src/email_submission/helpers.rs index fd0bde8..e2d8182 100644 --- a/src/email_submission/helpers.rs +++ b/src/email_submission/helpers.rs @@ -99,7 +99,7 @@ impl Client { request .send_single::() .await - .map(|mut r| r.unwrap_list().pop()) + .map(|mut r| r.take_list().pop()) } pub async fn email_submission_query( diff --git a/src/event_source/mod.rs b/src/event_source/mod.rs index fde0133..7da049f 100644 --- a/src/event_source/mod.rs +++ b/src/event_source/mod.rs @@ -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> { self.changes } + + pub fn is_empty(&self) -> bool { + !self.changes.values().any(|changes| !changes.is_empty()) + } } diff --git a/src/identity/get.rs b/src/identity/get.rs index bd400c1..af70b28 100644 --- a/src/identity/get.rs +++ b/src/identity/get.rs @@ -7,8 +7,8 @@ impl Identity { 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> { diff --git a/src/identity/helpers.rs b/src/identity/helpers.rs index 62fe542..4e2c9bf 100644 --- a/src/identity/helpers.rs +++ b/src/identity/helpers.rs @@ -54,7 +54,7 @@ impl Client { request .send_single::() .await - .map(|mut r| r.unwrap_list().pop()) + .map(|mut r| r.take_list().pop()) } pub async fn identity_changes( diff --git a/src/mailbox/get.rs b/src/mailbox/get.rs index cbeeb4f..555cbed 100644 --- a/src/mailbox/get.rs +++ b/src/mailbox/get.rs @@ -9,8 +9,8 @@ impl Mailbox { 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> { diff --git a/src/mailbox/helpers.rs b/src/mailbox/helpers.rs index 478b8ca..47860a8 100644 --- a/src/mailbox/helpers.rs +++ b/src/mailbox/helpers.rs @@ -145,7 +145,7 @@ impl Client { request .send_single::() .await - .map(|mut r| r.unwrap_list().pop()) + .map(|mut r| r.take_list().pop()) } pub async fn mailbox_query( diff --git a/src/principal/get.rs b/src/principal/get.rs index 5f151ba..f6faf79 100644 --- a/src/principal/get.rs +++ b/src/principal/get.rs @@ -9,8 +9,8 @@ impl Principal { 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> { diff --git a/src/principal/helpers.rs b/src/principal/helpers.rs index 0999245..cc0f6b7 100644 --- a/src/principal/helpers.rs +++ b/src/principal/helpers.rs @@ -212,7 +212,7 @@ impl Client { request .send_single::() .await - .map(|mut r| r.unwrap_list().pop()) + .map(|mut r| r.take_list().pop()) } pub async fn principal_query( diff --git a/src/push_subscription/get.rs b/src/push_subscription/get.rs index dfd0de2..2ef1264 100644 --- a/src/push_subscription/get.rs +++ b/src/push_subscription/get.rs @@ -7,8 +7,8 @@ impl PushSubscription { 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> { diff --git a/src/thread/helpers.rs b/src/thread/helpers.rs index ddb1cff..330f3b6 100644 --- a/src/thread/helpers.rs +++ b/src/thread/helpers.rs @@ -18,7 +18,7 @@ impl Client { request .send_single::() .await - .map(|mut r| r.unwrap_list().pop()) + .map(|mut r| r.take_list().pop()) } } diff --git a/src/vacation_response/helpers.rs b/src/vacation_response/helpers.rs index 7bb9d52..982f8e8 100644 --- a/src/vacation_response/helpers.rs +++ b/src/vacation_response/helpers.rs @@ -100,7 +100,7 @@ impl Client { request .send_single::() .await - .map(|mut r| r.unwrap_list().pop()) + .map(|mut r| r.take_list().pop()) } pub async fn vacation_response_destroy(&self) -> crate::Result<()> {