diff --git a/src/core/changes.rs b/src/core/changes.rs index 0d3356d..0884747 100644 --- a/src/core/changes.rs +++ b/src/core/changes.rs @@ -68,6 +68,10 @@ impl ChangesResponse { &self.account_id } + pub fn unwrap_account_id(self) -> String { + self.account_id + } + pub fn old_state(&self) -> &str { &self.old_state } @@ -76,6 +80,10 @@ impl ChangesResponse { &self.new_state } + pub fn unwrap_new_state(self) -> String { + self.new_state + } + pub fn has_more_changes(&self) -> bool { self.has_more_changes } @@ -95,4 +103,8 @@ impl ChangesResponse { pub fn arguments(&self) -> &O::ChangesResponse { &self.arguments } + + pub fn total_changes(&self) -> usize { + self.created.len() + self.updated.len() + self.destroyed.len() + } } diff --git a/src/core/get.rs b/src/core/get.rs index f541edf..669291a 100644 --- a/src/core/get.rs +++ b/src/core/get.rs @@ -103,14 +103,18 @@ impl GetRequest { } impl GetResponse { - pub fn account_id(&self) -> &str { - self.account_id.as_ref().unwrap() + pub fn account_id(&self) -> Option<&str> { + self.account_id.as_deref() } pub fn state(&self) -> &str { &self.state } + pub fn unwrap_state(&mut self) -> String { + std::mem::take(&mut self.state) + } + pub fn list(&self) -> &[O] { &self.list } diff --git a/src/core/response.rs b/src/core/response.rs index fbcba70..94814d2 100644 --- a/src/core/response.rs +++ b/src/core/response.rs @@ -64,8 +64,8 @@ impl Response { self.method_responses.remove(index) } - pub fn pop_method_response(&mut self) -> T { - self.method_responses.pop().unwrap() + pub fn pop_method_response(&mut self) -> Option { + self.method_responses.pop() } pub fn created_ids(&self) -> Option> { diff --git a/src/core/set.rs b/src/core/set.rs index 7fb41a1..739387f 100644 --- a/src/core/set.rs +++ b/src/core/set.rs @@ -222,16 +222,16 @@ impl SetRequest { } impl SetResponse { - pub fn account_id(&self) -> &str { - self.account_id.as_ref().unwrap() + pub fn account_id(&self) -> Option<&str> { + self.account_id.as_deref() } pub fn old_state(&self) -> Option<&str> { self.old_state.as_deref() } - pub fn new_state(&self) -> &str { - self.new_state.as_ref().unwrap() + pub fn new_state(&self) -> Option<&str> { + self.new_state.as_deref() } pub fn created(&mut self, id: &str) -> crate::Result { diff --git a/src/email/get.rs b/src/email/get.rs index 702b7a9..a8ec751 100644 --- a/src/email/get.rs +++ b/src/email/get.rs @@ -6,48 +6,52 @@ use super::{ }; impl Email { - pub fn id(&self) -> &str { - self.id.as_ref().unwrap() + pub fn id(&self) -> Option<&str> { + self.id.as_deref() } pub fn unwrap_id(self) -> String { self.id.unwrap() } - pub fn blob_id(&self) -> &str { - self.blob_id.as_ref().unwrap() + 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 thread_id(&self) -> &str { - self.thread_id.as_ref().unwrap() + pub fn thread_id(&self) -> Option<&str> { + self.thread_id.as_deref() } pub fn mailbox_ids(&self) -> Vec<&str> { self.mailbox_ids .as_ref() - .unwrap() - .iter() - .filter(|(_, v)| **v) - .map(|(k, _)| k.as_str()) - .collect() + .map(|m| { + m.iter() + .filter(|(_, v)| **v) + .map(|(k, _)| k.as_str()) + .collect() + }) + .unwrap_or_default() } pub fn keywords(&self) -> Vec<&str> { self.keywords .as_ref() - .unwrap() - .iter() - .filter(|(_, v)| **v) - .map(|(k, _)| k.as_str()) - .collect() + .map(|k| { + k.iter() + .filter(|(_, v)| **v) + .map(|(k, _)| k.as_str()) + .collect() + }) + .unwrap_or_default() } pub fn size(&self) -> usize { - self.size.unwrap() + self.size.unwrap_or(0) } pub fn received_at(&self) -> Option { diff --git a/src/email_submission/get.rs b/src/email_submission/get.rs index 6a2926c..85fbbb6 100644 --- a/src/email_submission/get.rs +++ b/src/email_submission/get.rs @@ -5,24 +5,24 @@ use crate::{core::get::GetObject, Get, Set}; use super::{Address, Delivered, DeliveryStatus, Displayed, EmailSubmission, UndoStatus}; impl EmailSubmission { - pub fn id(&self) -> &str { - self.id.as_ref().unwrap() + pub fn id(&self) -> Option<&str> { + self.id.as_deref() } pub fn unwrap_id(self) -> String { self.id.unwrap() } - pub fn identity_id(&self) -> &str { - self.identity_id.as_ref().unwrap() + pub fn identity_id(&self) -> Option<&str> { + self.identity_id.as_deref() } - pub fn email_id(&self) -> &str { - self.email_id.as_ref().unwrap() + pub fn email_id(&self) -> Option<&str> { + self.email_id.as_deref() } - pub fn thread_id(&self) -> &str { - self.thread_id.as_ref().unwrap() + pub fn thread_id(&self) -> Option<&str> { + self.thread_id.as_deref() } pub fn mail_from(&self) -> Option<&Address> { @@ -33,12 +33,12 @@ impl EmailSubmission { self.envelope.as_ref().map(|e| e.rcpt_to.as_ref()) } - pub fn send_at(&self) -> i64 { - self.send_at.as_ref().unwrap().timestamp() + pub fn send_at(&self) -> Option { + self.send_at.as_ref().map(|t| t.timestamp()) } - pub fn undo_status(&self) -> &UndoStatus { - self.undo_status.as_ref().unwrap() + pub fn undo_status(&self) -> Option<&UndoStatus> { + self.undo_status.as_ref() } pub fn delivery_status_email(&self, email: &str) -> Option<&DeliveryStatus> { diff --git a/src/identity/get.rs b/src/identity/get.rs index 5715af3..c436e71 100644 --- a/src/identity/get.rs +++ b/src/identity/get.rs @@ -3,8 +3,8 @@ use crate::{core::get::GetObject, email::EmailAddress, Get, Set}; use super::Identity; impl Identity { - pub fn id(&self) -> &str { - self.id.as_ref().unwrap() + pub fn id(&self) -> Option<&str> { + self.id.as_deref() } pub fn unwrap_id(self) -> String { @@ -15,8 +15,8 @@ impl Identity { self.name.as_deref() } - pub fn email(&self) -> &str { - self.email.as_ref().unwrap() + pub fn email(&self) -> Option<&str> { + self.email.as_deref() } pub fn reply_to(&self) -> Option<&[EmailAddress]> { diff --git a/src/lib.rs b/src/lib.rs index 6bf7ced..cd620d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -226,12 +226,12 @@ impl Display for Error { Error::Transport(e) => write!(f, "Transport error: {}", e), Error::Parse(e) => write!(f, "Parse error: {}", e), Error::Internal(e) => write!(f, "Internal error: {}", e), - Error::Problem(e) => write!(f, "Problem details: {}", e), - Error::Server(e) => write!(f, "Server error: {}", e), - Error::Method(e) => write!(f, "Method error: {}", e), - Error::Set(e) => write!(f, "Set error: {}", e), + Error::Problem(e) => write!(f, "Request failed: {}", e), + Error::Server(e) => write!(f, "Server failed: {}", e), + Error::Method(e) => write!(f, "Request failed: {}", e), + Error::Set(e) => write!(f, "Set failed: {}", e), #[cfg(feature = "websockets")] - Error::WebSocket(e) => write!(f, "WebSocket error: {}", e), + Error::WebSocket(e) => write!(f, "WebSockets error: {}", e), } } } diff --git a/src/mailbox/get.rs b/src/mailbox/get.rs index fe80859..92a9d47 100644 --- a/src/mailbox/get.rs +++ b/src/mailbox/get.rs @@ -5,8 +5,8 @@ use crate::{core::get::GetObject, principal::ACL, Get, Set}; use super::{Mailbox, MailboxRights, Role}; impl Mailbox { - pub fn id(&self) -> &str { - self.id.as_ref().unwrap() + pub fn id(&self) -> Option<&str> { + self.id.as_deref() } pub fn unwrap_id(self) -> String { diff --git a/src/mailbox/mod.rs b/src/mailbox/mod.rs index d1398f0..4bfdbe1 100644 --- a/src/mailbox/mod.rs +++ b/src/mailbox/mod.rs @@ -116,6 +116,12 @@ pub enum Role { None, } +impl Default for Role { + fn default() -> Self { + Role::None + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct MailboxRights { #[serde(rename = "mayReadItems")] @@ -174,6 +180,18 @@ pub enum Property { ACL, } +impl Property { + pub fn is_count(&self) -> bool { + matches!( + self, + Property::TotalEmails + | Property::UnreadEmails + | Property::TotalThreads + | Property::UnreadThreads + ) + } +} + impl Display for Property { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { diff --git a/src/principal/get.rs b/src/principal/get.rs index 69386b6..a86eb1f 100644 --- a/src/principal/get.rs +++ b/src/principal/get.rs @@ -5,8 +5,8 @@ use crate::{core::get::GetObject, Get, Set}; use super::{Principal, Type, ACL, DKIM}; impl Principal { - pub fn id(&self) -> &str { - self.id.as_ref().unwrap() + pub fn id(&self) -> Option<&str> { + self.id.as_deref() } pub fn unwrap_id(self) -> String { diff --git a/src/push_subscription/get.rs b/src/push_subscription/get.rs index 3413850..e29047d 100644 --- a/src/push_subscription/get.rs +++ b/src/push_subscription/get.rs @@ -1,22 +1,22 @@ -use crate::{core::get::GetObject, Get, TypeState, Set}; +use crate::{core::get::GetObject, Get, Set, TypeState}; use super::{Keys, PushSubscription}; impl PushSubscription { - pub fn id(&self) -> &str { - self.id.as_ref().unwrap() + pub fn id(&self) -> Option<&str> { + self.id.as_deref() } pub fn unwrap_id(self) -> String { self.id.unwrap() } - pub fn device_client_id(&self) -> &str { - self.device_client_id.as_ref().unwrap() + pub fn device_client_id(&self) -> Option<&str> { + self.device_client_id.as_deref() } - pub fn url(&self) -> &str { - self.url.as_ref().unwrap() + pub fn url(&self) -> Option<&str> { + self.url.as_deref() } pub fn keys(&self) -> Option<&Keys> { diff --git a/src/vacation_response/get.rs b/src/vacation_response/get.rs index 21b4f69..d59ef2e 100644 --- a/src/vacation_response/get.rs +++ b/src/vacation_response/get.rs @@ -3,8 +3,8 @@ use crate::{core::get::GetObject, Get, Set}; use super::VacationResponse; impl VacationResponse { - pub fn id(&self) -> &str { - self.id.as_ref().unwrap() + pub fn id(&self) -> Option<&str> { + self.id.as_deref() } pub fn is_enabled(&self) -> bool {