From 1b27e6e14679b838e1cc8c10891c63011ef023c3 Mon Sep 17 00:00:00 2001 From: Mauro D Date: Wed, 1 Jun 2022 11:48:02 +0000 Subject: [PATCH] Use of associated types + Identity and EmailSubmission helpers. --- src/core/changes.rs | 14 +-- src/core/copy.rs | 22 ++--- src/core/get.rs | 36 ++++---- src/core/mod.rs | 7 +- src/core/query.rs | 26 +++--- src/core/query_changes.rs | 20 ++--- src/core/request.rs | 140 ++++++++++-------------------- src/core/response.rs | 38 ++++----- src/core/set.rs | 62 +++++++------- src/email/get.rs | 14 ++- src/email/helpers.rs | 23 ++--- src/email/mod.rs | 38 ++++++--- src/email/query.rs | 18 +++- src/email/set.rs | 20 ++++- src/email_submission/get.rs | 10 ++- src/email_submission/helpers.rs | 141 ++++++++++++++++++++++++++++--- src/email_submission/mod.rs | 22 ++++- src/email_submission/query.rs | 18 +++- src/email_submission/set.rs | 22 ++++- src/identity/get.rs | 14 ++- src/identity/helpers.rs | 82 ++++++++++++++++-- src/identity/mod.rs | 19 ++++- src/identity/set.rs | 34 +++++--- src/mailbox/get.rs | 10 ++- src/mailbox/helpers.rs | 25 ++---- src/mailbox/mod.rs | 20 ++++- src/mailbox/query.rs | 15 +++- src/mailbox/set.rs | 18 +++- src/push_subscription/get.rs | 10 ++- src/push_subscription/helpers.rs | 6 +- src/push_subscription/mod.rs | 19 ++++- src/push_subscription/set.rs | 21 ++++- src/thread/helpers.rs | 10 ++- src/thread/mod.rs | 11 ++- src/vacation_response/get.rs | 10 ++- src/vacation_response/helpers.rs | 4 +- src/vacation_response/mod.rs | 19 ++++- src/vacation_response/set.rs | 21 ++++- 38 files changed, 734 insertions(+), 325 deletions(-) diff --git a/src/core/changes.rs b/src/core/changes.rs index ade0a27..0d3356d 100644 --- a/src/core/changes.rs +++ b/src/core/changes.rs @@ -1,6 +1,10 @@ use serde::{Deserialize, Serialize}; -use super::RequestParams; +use super::{Object, RequestParams}; + +pub trait ChangesObject: Object { + type ChangesResponse; +} #[derive(Debug, Clone, Serialize)] pub struct ChangesRequest { @@ -16,7 +20,7 @@ pub struct ChangesRequest { } #[derive(Debug, Clone, Deserialize)] -pub struct ChangesResponse { +pub struct ChangesResponse { #[serde(rename = "accountId")] account_id: String, @@ -36,7 +40,7 @@ pub struct ChangesResponse { destroyed: Vec, #[serde(flatten)] - arguments: A, + arguments: O::ChangesResponse, } impl ChangesRequest { @@ -59,7 +63,7 @@ impl ChangesRequest { } } -impl ChangesResponse { +impl ChangesResponse { pub fn account_id(&self) -> &str { &self.account_id } @@ -88,7 +92,7 @@ impl ChangesResponse { &self.destroyed } - pub fn arguments(&self) -> &A { + pub fn arguments(&self) -> &O::ChangesResponse { &self.arguments } } diff --git a/src/core/copy.rs b/src/core/copy.rs index 6fab1c4..211d0be 100644 --- a/src/core/copy.rs +++ b/src/core/copy.rs @@ -1,14 +1,14 @@ -use std::{collections::HashMap, fmt::Display}; +use std::collections::HashMap; use serde::{Deserialize, Serialize}; use super::{ - set::{Create, SetError}, + set::{SetError, SetObject}, RequestParams, }; #[derive(Debug, Clone, Serialize)] -pub struct CopyRequest { +pub struct CopyRequest { #[serde(rename = "fromAccountId")] from_account_id: String, @@ -24,7 +24,7 @@ pub struct CopyRequest { if_in_state: Option, #[serde(rename = "create")] - create: HashMap, + create: HashMap, #[serde(rename = "onSuccessDestroyOriginal")] on_success_destroy_original: bool, @@ -35,7 +35,7 @@ pub struct CopyRequest { } #[derive(Debug, Clone, Deserialize)] -pub struct CopyResponse { +pub struct CopyResponse { #[serde(rename = "fromAccountId")] from_account_id: String, @@ -49,13 +49,13 @@ pub struct CopyResponse { new_state: String, #[serde(rename = "created")] - created: Option>, + created: Option>, #[serde(rename = "notCreated")] - not_created: Option>>, + not_created: Option>>, } -impl CopyRequest { +impl CopyRequest { pub fn new(params: RequestParams, from_account_id: String) -> Self { CopyRequest { from_account_id, @@ -105,7 +105,7 @@ impl CopyRequest { } } -impl CopyResponse { +impl CopyResponse { pub fn from_account_id(&self) -> &str { &self.from_account_id } @@ -122,11 +122,11 @@ impl CopyResponse { &self.new_state } - pub fn created(&self, id: &str) -> Option<&T> { + pub fn created(&self, id: &str) -> Option<&O> { self.created.as_ref().and_then(|created| created.get(id)) } - pub fn not_created(&self, id: &str) -> Option<&SetError> { + pub fn not_created(&self, id: &str) -> Option<&SetError> { self.not_created .as_ref() .and_then(|not_created| not_created.get(id)) diff --git a/src/core/get.rs b/src/core/get.rs index a95934b..f541edf 100644 --- a/src/core/get.rs +++ b/src/core/get.rs @@ -1,13 +1,15 @@ -use std::fmt::Display; - use serde::{Deserialize, Serialize}; use crate::Method; -use super::{request::ResultReference, RequestParams, Type}; +use super::{request::ResultReference, Object, RequestParams}; + +pub trait GetObject: Object { + type GetArguments: Default; +} #[derive(Debug, Clone, Serialize)] -pub struct GetRequest { +pub struct GetRequest { #[serde(skip)] method: (Method, usize), @@ -24,10 +26,10 @@ pub struct GetRequest { ids_ref: Option, #[serde(skip_serializing_if = "Option::is_none")] - properties: Option>, + properties: Option>, #[serde(flatten)] - arguments: A, + arguments: O::GetArguments, } #[derive(Debug, Clone, Deserialize)] @@ -43,10 +45,10 @@ pub struct GetResponse { not_found: Vec, } -impl GetRequest { +impl GetRequest { pub fn new(params: RequestParams) -> Self { GetRequest { - account_id: if T::requires_account_id() { + account_id: if O::requires_account_id() { params.account_id.into() } else { None @@ -55,12 +57,12 @@ impl GetRequest { ids: None, ids_ref: None, properties: None, - arguments: A::default(), + arguments: O::GetArguments::default(), } } pub fn account_id(&mut self, account_id: impl Into) -> &mut Self { - if T::requires_account_id() { + if O::requires_account_id() { self.account_id = Some(account_id.into()); } self @@ -82,16 +84,16 @@ impl GetRequest { self } - pub fn properties(&mut self, properties: impl IntoIterator) -> &mut Self { + pub fn properties(&mut self, properties: impl IntoIterator) -> &mut Self { self.properties = Some(properties.into_iter().collect()); self } - pub fn arguments(&mut self) -> &mut A { + pub fn arguments(&mut self) -> &mut O::GetArguments { &mut self.arguments } - pub fn result_reference(&self, property: T) -> ResultReference { + pub fn result_reference(&self, property: O::Property) -> ResultReference { ResultReference::new( self.method.0, self.method.1, @@ -100,7 +102,7 @@ impl GetRequest { } } -impl GetResponse { +impl GetResponse { pub fn account_id(&self) -> &str { self.account_id.as_ref().unwrap() } @@ -109,7 +111,7 @@ impl GetResponse { &self.state } - pub fn list(&self) -> &[T] { + pub fn list(&self) -> &[O] { &self.list } @@ -117,10 +119,10 @@ impl GetResponse { &self.not_found } - pub fn unwrap_list(&mut self) -> Vec { + pub fn unwrap_list(&mut self) -> Vec { std::mem::take(&mut self.list) } - pub fn pop(&mut self) -> Option { + pub fn pop(&mut self) -> Option { self.list.pop() } diff --git a/src/core/mod.rs b/src/core/mod.rs index efb45a1..91439c0 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -1,3 +1,7 @@ +use std::fmt::Display; + +use serde::{Deserialize, Serialize}; + use crate::Method; pub mod changes; @@ -27,6 +31,7 @@ impl RequestParams { } } -pub trait Type { +pub trait Object: Sized { + type Property: Display + Serialize + for<'de> Deserialize<'de>; fn requires_account_id() -> bool; } diff --git a/src/core/query.rs b/src/core/query.rs index 40fdd76..1f8fa0e 100644 --- a/src/core/query.rs +++ b/src/core/query.rs @@ -2,10 +2,16 @@ use serde::{Deserialize, Serialize}; use crate::Method; -use super::{request::ResultReference, RequestParams}; +use super::{request::ResultReference, Object, RequestParams}; + +pub trait QueryObject: Object { + type QueryArguments: Default + Serialize; + type Filter: Serialize; + type Sort: Serialize; +} #[derive(Debug, Clone, Serialize)] -pub struct QueryRequest { +pub struct QueryRequest { #[serde(skip)] method: (Method, usize), @@ -14,11 +20,11 @@ pub struct QueryRequest { #[serde(rename = "filter")] #[serde(skip_serializing_if = "Option::is_none")] - filter: Option>, + filter: Option>, #[serde(rename = "sort")] #[serde(skip_serializing_if = "Option::is_none")] - sort: Option>>, + sort: Option>>, #[serde(rename = "position")] #[serde(skip_serializing_if = "Option::is_none")] @@ -41,7 +47,7 @@ pub struct QueryRequest { calculate_total: Option, #[serde(flatten)] - arguments: A, + arguments: O::QueryArguments, } #[derive(Debug, Clone, Serialize)] @@ -103,7 +109,7 @@ pub struct QueryResponse { limit: Option, } -impl QueryRequest { +impl QueryRequest { pub fn new(params: RequestParams) -> Self { QueryRequest { account_id: params.account_id, @@ -115,7 +121,7 @@ impl QueryRequest { anchor_offset: None, limit: None, calculate_total: None, - arguments: A::default(), + arguments: O::QueryArguments::default(), } } @@ -124,12 +130,12 @@ impl QueryRequest { self } - pub fn filter(&mut self, filter: impl Into>) -> &mut Self { + pub fn filter(&mut self, filter: impl Into>) -> &mut Self { self.filter = Some(filter.into()); self } - pub fn sort(&mut self, sort: impl IntoIterator>) -> &mut Self { + pub fn sort(&mut self, sort: impl IntoIterator>) -> &mut Self { self.sort = Some(sort.into_iter().collect()); self } @@ -159,7 +165,7 @@ impl QueryRequest { self } - pub fn arguments(&mut self) -> &mut A { + pub fn arguments(&mut self) -> &mut O::QueryArguments { &mut self.arguments } diff --git a/src/core/query_changes.rs b/src/core/query_changes.rs index a37213e..2ab03f5 100644 --- a/src/core/query_changes.rs +++ b/src/core/query_changes.rs @@ -1,22 +1,22 @@ use serde::{Deserialize, Serialize}; use super::{ - query::{Comparator, Filter}, + query::{Comparator, Filter, QueryObject}, RequestParams, }; #[derive(Debug, Clone, Serialize)] -pub struct QueryChangesRequest { +pub struct QueryChangesRequest { #[serde(rename = "accountId")] account_id: String, #[serde(rename = "filter")] #[serde(skip_serializing_if = "Option::is_none")] - filter: Option>, + filter: Option>, #[serde(rename = "sort")] #[serde(skip_serializing_if = "Option::is_none")] - sort: Option>>, + sort: Option>>, #[serde(rename = "sinceQueryState")] since_query_state: String, @@ -33,7 +33,7 @@ pub struct QueryChangesRequest { calculate_total: bool, #[serde(flatten)] - arguments: A, + arguments: O::QueryArguments, } #[derive(Debug, Clone, Deserialize)] @@ -58,7 +58,7 @@ pub struct AddedItem { index: usize, } -impl QueryChangesRequest { +impl QueryChangesRequest { pub fn new(params: RequestParams, since_query_state: String) -> Self { QueryChangesRequest { account_id: params.account_id, @@ -68,7 +68,7 @@ impl QueryChangesRequest { max_changes: None, up_to_id: None, calculate_total: false, - arguments: A::default(), + arguments: O::QueryArguments::default(), } } @@ -77,12 +77,12 @@ impl QueryChangesRequest { self } - pub fn filter(&mut self, filter: impl Into>) -> &mut Self { + pub fn filter(&mut self, filter: impl Into>) -> &mut Self { self.filter = Some(filter.into()); self } - pub fn sort(&mut self, sort: impl IntoIterator>) -> &mut Self { + pub fn sort(&mut self, sort: impl IntoIterator>) -> &mut Self { self.sort = Some(sort.into_iter().collect()); self } @@ -102,7 +102,7 @@ impl QueryChangesRequest { self } - pub fn arguments(&mut self) -> &mut A { + pub fn arguments(&mut self) -> &mut O::QueryArguments { &mut self.arguments } } diff --git a/src/core/request.rs b/src/core/request.rs index ac357db..0145791 100644 --- a/src/core/request.rs +++ b/src/core/request.rs @@ -5,13 +5,13 @@ use serde::{de::DeserializeOwned, Serialize}; use crate::{ blob::copy::CopyBlobRequest, client::Client, - email::{self, import::EmailImportRequest, parse::EmailParseRequest, Email}, - email_submission::{self, EmailSubmission}, - identity::{self, Identity}, - mailbox::{self, Mailbox}, - push_subscription::{self, PushSubscription}, - thread, - vacation_response::{self, VacationResponse}, + email::{import::EmailImportRequest, parse::EmailParseRequest, Email}, + email_submission::EmailSubmission, + identity::Identity, + mailbox::Mailbox, + push_subscription::PushSubscription, + thread::Thread, + vacation_response::VacationResponse, Error, Method, Set, URI, }; @@ -55,47 +55,29 @@ pub struct ResultReference { #[serde(untagged)] pub enum Arguments { Changes(ChangesRequest), - PushGet(GetRequest), - PushSet(SetRequest, ()>), + PushGet(GetRequest>), + PushSet(SetRequest>), BlobCopy(CopyBlobRequest), - MailboxGet(GetRequest), - MailboxQuery( - QueryRequest, - ), - MailboxQueryChanges( - QueryChangesRequest< - mailbox::query::Filter, - mailbox::query::Comparator, - mailbox::QueryArguments, - >, - ), - MailboxSet(SetRequest, mailbox::SetArguments>), - ThreadGet(GetRequest), - EmailGet(GetRequest), - EmailQuery(QueryRequest), - EmailQueryChanges( - QueryChangesRequest, - ), - EmailSet(SetRequest, ()>), + MailboxGet(GetRequest>), + MailboxQuery(QueryRequest>), + MailboxQueryChanges(QueryChangesRequest>), + MailboxSet(SetRequest>), + ThreadGet(GetRequest), + EmailGet(GetRequest>), + EmailQuery(QueryRequest>), + EmailQueryChanges(QueryChangesRequest>), + EmailSet(SetRequest>), EmailCopy(CopyRequest>), EmailImport(EmailImportRequest), EmailParse(EmailParseRequest), - IdentityGet(GetRequest), - IdentitySet(SetRequest, ()>), - EmailSubmissionGet(GetRequest), - EmailSubmissionQuery( - QueryRequest, - ), - EmailSubmissionQueryChanges( - QueryChangesRequest< - email_submission::query::Filter, - email_submission::query::Comparator, - (), - >, - ), - EmailSubmissionSet(SetRequest, email_submission::SetArguments>), - VacationResponseGet(GetRequest), - VacationResponseSet(SetRequest, ()>), + IdentityGet(GetRequest>), + IdentitySet(SetRequest>), + EmailSubmissionGet(GetRequest>), + EmailSubmissionQuery(QueryRequest>), + EmailSubmissionQueryChanges(QueryChangesRequest>), + EmailSubmissionSet(SetRequest>), + VacationResponseGet(GetRequest>), + VacationResponseSet(SetRequest>), } impl Arguments { @@ -205,14 +187,14 @@ impl Arguments { } } - pub fn push_get_mut(&mut self) -> &mut GetRequest { + pub fn push_get_mut(&mut self) -> &mut GetRequest> { match self { Arguments::PushGet(ref mut r) => r, _ => unreachable!(), } } - pub fn push_set_mut(&mut self) -> &mut SetRequest, ()> { + pub fn push_set_mut(&mut self) -> &mut SetRequest> { match self { Arguments::PushSet(ref mut r) => r, _ => unreachable!(), @@ -226,84 +208,63 @@ impl Arguments { } } - pub fn mailbox_get_mut(&mut self) -> &mut GetRequest { + pub fn mailbox_get_mut(&mut self) -> &mut GetRequest> { match self { Arguments::MailboxGet(ref mut r) => r, _ => unreachable!(), } } - pub fn mailbox_query_mut( - &mut self, - ) -> &mut QueryRequest< - mailbox::query::Filter, - mailbox::query::Comparator, - mailbox::QueryArguments, - > { + pub fn mailbox_query_mut(&mut self) -> &mut QueryRequest> { match self { Arguments::MailboxQuery(ref mut r) => r, _ => unreachable!(), } } - pub fn mailbox_query_changes_mut( - &mut self, - ) -> &mut QueryChangesRequest< - mailbox::query::Filter, - mailbox::query::Comparator, - mailbox::QueryArguments, - > { + pub fn mailbox_query_changes_mut(&mut self) -> &mut QueryChangesRequest> { match self { Arguments::MailboxQueryChanges(ref mut r) => r, _ => unreachable!(), } } - pub fn mailbox_set_mut(&mut self) -> &mut SetRequest, mailbox::SetArguments> { + pub fn mailbox_set_mut(&mut self) -> &mut SetRequest> { match self { Arguments::MailboxSet(ref mut r) => r, _ => unreachable!(), } } - pub fn thread_get_mut(&mut self) -> &mut GetRequest { + pub fn thread_get_mut(&mut self) -> &mut GetRequest { match self { Arguments::ThreadGet(ref mut r) => r, _ => unreachable!(), } } - pub fn email_get_mut(&mut self) -> &mut GetRequest { + pub fn email_get_mut(&mut self) -> &mut GetRequest> { match self { Arguments::EmailGet(ref mut r) => r, _ => unreachable!(), } } - pub fn email_query_mut( - &mut self, - ) -> &mut QueryRequest - { + pub fn email_query_mut(&mut self) -> &mut QueryRequest> { match self { Arguments::EmailQuery(ref mut r) => r, _ => unreachable!(), } } - pub fn email_query_changes_mut( - &mut self, - ) -> &mut QueryChangesRequest< - email::query::Filter, - email::query::Comparator, - email::QueryArguments, - > { + pub fn email_query_changes_mut(&mut self) -> &mut QueryChangesRequest> { match self { Arguments::EmailQueryChanges(ref mut r) => r, _ => unreachable!(), } } - pub fn email_set_mut(&mut self) -> &mut SetRequest, ()> { + pub fn email_set_mut(&mut self) -> &mut SetRequest> { match self { Arguments::EmailSet(ref mut r) => r, _ => unreachable!(), @@ -331,31 +292,28 @@ impl Arguments { } } - pub fn identity_get_mut(&mut self) -> &mut GetRequest { + pub fn identity_get_mut(&mut self) -> &mut GetRequest> { match self { Arguments::IdentityGet(ref mut r) => r, _ => unreachable!(), } } - pub fn identity_set_mut(&mut self) -> &mut SetRequest, ()> { + pub fn identity_set_mut(&mut self) -> &mut SetRequest> { match self { Arguments::IdentitySet(ref mut r) => r, _ => unreachable!(), } } - pub fn email_submission_get_mut(&mut self) -> &mut GetRequest { + pub fn email_submission_get_mut(&mut self) -> &mut GetRequest> { match self { Arguments::EmailSubmissionGet(ref mut r) => r, _ => unreachable!(), } } - pub fn email_submission_query_mut( - &mut self, - ) -> &mut QueryRequest - { + pub fn email_submission_query_mut(&mut self) -> &mut QueryRequest> { match self { Arguments::EmailSubmissionQuery(ref mut r) => r, _ => unreachable!(), @@ -364,36 +322,28 @@ impl Arguments { pub fn email_submission_query_changes_mut( &mut self, - ) -> &mut QueryChangesRequest< - email_submission::query::Filter, - email_submission::query::Comparator, - (), - > { + ) -> &mut QueryChangesRequest> { match self { Arguments::EmailSubmissionQueryChanges(ref mut r) => r, _ => unreachable!(), } } - pub fn email_submission_set_mut( - &mut self, - ) -> &mut SetRequest, email_submission::SetArguments> { + pub fn email_submission_set_mut(&mut self) -> &mut SetRequest> { match self { Arguments::EmailSubmissionSet(ref mut r) => r, _ => unreachable!(), } } - pub fn vacation_response_get_mut( - &mut self, - ) -> &mut GetRequest { + pub fn vacation_response_get_mut(&mut self) -> &mut GetRequest> { match self { Arguments::VacationResponseGet(ref mut r) => r, _ => unreachable!(), } } - pub fn vacation_response_set_mut(&mut self) -> &mut SetRequest, ()> { + pub fn vacation_response_set_mut(&mut self) -> &mut SetRequest> { match self { Arguments::VacationResponseSet(ref mut r) => r, _ => unreachable!(), diff --git a/src/core/response.rs b/src/core/response.rs index 9467dfc..906c4a4 100644 --- a/src/core/response.rs +++ b/src/core/response.rs @@ -4,13 +4,13 @@ use serde::{de::Visitor, Deserialize}; use crate::{ blob::copy::CopyBlobResponse, - email::{self, import::EmailImportResponse, parse::EmailParseResponse, Email}, - email_submission::{self, EmailSubmission}, - identity::{self, Identity}, - mailbox::{self, Mailbox}, - push_subscription::{self, PushSubscription}, + email::{import::EmailImportResponse, parse::EmailParseResponse, Email}, + email_submission::EmailSubmission, + identity::Identity, + mailbox::Mailbox, + push_subscription::PushSubscription, thread::Thread, - vacation_response::{self, VacationResponse}, + vacation_response::VacationResponse, Get, Method, }; @@ -93,28 +93,26 @@ pub enum Error { Error, } -pub type PushSubscriptionSetResponse = - SetResponse, push_subscription::Property>; +pub type PushSubscriptionSetResponse = SetResponse>; pub type PushSubscriptionGetResponse = GetResponse>; -pub type MaiboxChangesResponse = ChangesResponse; -pub type MailboxSetResponse = SetResponse, mailbox::Property>; +pub type MaiboxChangesResponse = ChangesResponse>; +pub type MailboxSetResponse = SetResponse>; pub type MailboxGetResponse = GetResponse>; pub type ThreadGetResponse = GetResponse; -pub type ThreadChangesResponse = ChangesResponse<()>; +pub type ThreadChangesResponse = ChangesResponse; pub type EmailGetResponse = GetResponse>; -pub type EmailSetResponse = SetResponse, email::Property>; -pub type EmailCopyResponse = CopyResponse, email::Property>; -pub type EmailChangesResponse = ChangesResponse<()>; +pub type EmailSetResponse = SetResponse>; +pub type EmailCopyResponse = CopyResponse>; +pub type EmailChangesResponse = ChangesResponse>; pub type SearchSnippetGetResponse = GetResponse; -pub type IdentitySetResponse = SetResponse, identity::Property>; +pub type IdentitySetResponse = SetResponse>; pub type IdentityGetResponse = GetResponse>; -pub type IdentityChangesResponse = ChangesResponse<()>; -pub type EmailSubmissionSetResponse = SetResponse, email_submission::Property>; +pub type IdentityChangesResponse = ChangesResponse>; +pub type EmailSubmissionSetResponse = SetResponse>; pub type EmailSubmissionGetResponse = GetResponse>; -pub type EmailSubmissionChangesResponse = ChangesResponse<()>; +pub type EmailSubmissionChangesResponse = ChangesResponse>; pub type VacationResponseGetResponse = GetResponse>; -pub type VacationResponseSetResponse = - SetResponse, vacation_response::Property>; +pub type VacationResponseSetResponse = SetResponse>; #[derive(Debug)] pub struct TaggedMethodResponse { diff --git a/src/core/set.rs b/src/core/set.rs index 4deef2a..03846c2 100644 --- a/src/core/set.rs +++ b/src/core/set.rs @@ -7,10 +7,17 @@ use std::{ use crate::Error; -use super::{request::ResultReference, RequestParams, Type}; +use super::{request::ResultReference, Object, RequestParams}; + +pub trait SetObject: Object { + type SetArguments: Default; + + fn new(create_id: Option) -> Self; + fn create_id(&self) -> Option; +} #[derive(Debug, Clone, Serialize)] -pub struct SetRequest { +pub struct SetRequest { #[serde(rename = "accountId")] #[serde(skip_serializing_if = "Option::is_none")] account_id: Option, @@ -20,10 +27,10 @@ pub struct SetRequest { if_in_state: Option, #[serde(skip_serializing_if = "Option::is_none")] - create: Option>, + create: Option>, #[serde(skip_serializing_if = "Option::is_none")] - update: Option>, + update: Option>, #[serde(skip_serializing_if = "Option::is_none")] destroy: Option>, @@ -34,11 +41,11 @@ pub struct SetRequest { destroy_ref: Option, #[serde(flatten)] - arguments: A, + arguments: O::SetArguments, } #[derive(Debug, Clone, Deserialize)] -pub struct SetResponse { +pub struct SetResponse { #[serde(rename = "accountId")] account_id: Option, @@ -49,22 +56,22 @@ pub struct SetResponse { new_state: Option, #[serde(rename = "created")] - created: Option>, + created: Option>, #[serde(rename = "updated")] - updated: Option>>, + updated: Option>>, #[serde(rename = "destroyed")] destroyed: Option>, #[serde(rename = "notCreated")] - not_created: Option>>, + not_created: Option>>, #[serde(rename = "notUpdated")] - not_updated: Option>>, + not_updated: Option>>, #[serde(rename = "notDestroyed")] - not_destroyed: Option>>, + not_destroyed: Option>>, } #[derive(Debug, Clone, Deserialize)] @@ -126,15 +133,10 @@ pub enum SetErrorType { CannotUnsend, } -pub trait Create: Sized { - fn new(create_id: Option) -> Self; - fn create_id(&self) -> Option; -} - -impl SetRequest { +impl SetRequest { pub fn new(params: RequestParams) -> Self { Self { - account_id: if T::requires_account_id() { + account_id: if O::requires_account_id() { params.account_id.into() } else { None @@ -149,7 +151,7 @@ impl SetRequest { } pub fn account_id(&mut self, account_id: impl Into) -> &mut Self { - if T::requires_account_id() { + if O::requires_account_id() { self.account_id = Some(account_id.into()); } self @@ -160,12 +162,12 @@ impl SetRequest { self } - pub fn create(&mut self) -> &mut T { + pub fn create(&mut self) -> &mut O { let create_id = self.create.as_ref().map_or(0, |c| c.len()); let create_id_str = format!("c{}", create_id); self.create .get_or_insert_with(HashMap::new) - .insert(create_id_str.clone(), T::new(create_id.into())); + .insert(create_id_str.clone(), O::new(create_id.into())); self.create .as_mut() .unwrap() @@ -173,7 +175,7 @@ impl SetRequest { .unwrap() } - pub fn create_item(&mut self, item: T) -> String { + pub fn create_item(&mut self, item: O) -> String { let create_id = self.create.as_ref().map_or(0, |c| c.len()); let create_id_str = format!("c{}", create_id); self.create @@ -182,15 +184,15 @@ impl SetRequest { create_id_str } - pub fn update(&mut self, id: impl Into) -> &mut T { + pub fn update(&mut self, id: impl Into) -> &mut O { let id: String = id.into(); self.update .get_or_insert_with(HashMap::new) - .insert(id.clone(), T::new(None)); + .insert(id.clone(), O::new(None)); self.update.as_mut().unwrap().get_mut(&id).unwrap() } - pub fn update_item(&mut self, id: impl Into, item: T) { + pub fn update_item(&mut self, id: impl Into, item: O) { self.update .get_or_insert_with(HashMap::new) .insert(id.into(), item); @@ -214,12 +216,12 @@ impl SetRequest { self } - pub fn arguments(&mut self) -> &mut A { + pub fn arguments(&mut self) -> &mut O::SetArguments { &mut self.arguments } } -impl SetResponse { +impl SetResponse { pub fn account_id(&self) -> &str { self.account_id.as_ref().unwrap() } @@ -232,7 +234,7 @@ impl SetResponse { self.new_state.as_ref().unwrap() } - pub fn created(&mut self, id: &str) -> crate::Result { + pub fn created(&mut self, id: &str) -> crate::Result { if let Some(result) = self.created.as_mut().and_then(|r| r.remove(id)) { Ok(result) } else if let Some(error) = self.not_created.as_mut().and_then(|r| r.remove(id)) { @@ -242,7 +244,7 @@ impl SetResponse { } } - pub fn updated(&mut self, id: &str) -> crate::Result> { + pub fn updated(&mut self, id: &str) -> crate::Result> { if let Some(result) = self.updated.as_mut().and_then(|r| r.remove(id)) { Ok(result) } else if let Some(error) = self.not_updated.as_mut().and_then(|r| r.remove(id)) { @@ -378,6 +380,6 @@ pub fn date_not_set(date: &Option>) -> bool { matches!(date, Some(date) if date.timestamp() == 0) } -pub fn list_not_set(list: &Option>) -> bool { +pub fn list_not_set(list: &Option>) -> bool { matches!(list, Some(list) if list.is_empty() ) } diff --git a/src/email/get.rs b/src/email/get.rs index 490a22a..dba8e8f 100644 --- a/src/email/get.rs +++ b/src/email/get.rs @@ -1,8 +1,8 @@ -use crate::Get; +use crate::{core::get::GetObject, Get, Set}; use super::{ - Email, EmailAddress, EmailAddressGroup, EmailBodyPart, EmailBodyValue, EmailHeader, Header, - HeaderValue, + Email, EmailAddress, EmailAddressGroup, EmailBodyPart, EmailBodyValue, EmailHeader, + GetArguments, Header, HeaderValue, }; impl Email { @@ -229,3 +229,11 @@ impl EmailHeader { self.value.as_str() } } + +impl GetObject for Email { + type GetArguments = GetArguments; +} + +impl GetObject for Email { + type GetArguments = GetArguments; +} diff --git a/src/email/helpers.rs b/src/email/helpers.rs index 0ef4081..fcee5a5 100644 --- a/src/email/helpers.rs +++ b/src/email/helpers.rs @@ -10,7 +10,7 @@ use crate::{ response::{EmailCopyResponse, EmailGetResponse, EmailSetResponse}, set::SetRequest, }, - Method, Set, + Get, Method, Set, }; use super::{ @@ -118,7 +118,7 @@ impl Client { &mut self, id: &str, properties: Option>, - ) -> crate::Result> { + ) -> crate::Result>> { let mut request = self.build(); let get_request = request.get_email().ids([id]); if let Some(properties) = properties { @@ -134,7 +134,7 @@ impl Client { &mut self, since_state: impl Into, max_changes: usize, - ) -> crate::Result> { + ) -> crate::Result>> { let mut request = self.build(); request.changes_email(since_state).max_changes(max_changes); request.send_single().await @@ -187,7 +187,7 @@ impl Client { } impl Request<'_> { - pub fn get_email(&mut self) -> &mut GetRequest { + pub fn get_email(&mut self) -> &mut GetRequest> { self.add_method_call( Method::GetEmail, Arguments::email_get(self.params(Method::GetEmail)), @@ -207,14 +207,11 @@ impl Request<'_> { .changes_mut() } - pub async fn send_changes_email(self) -> crate::Result> { + pub async fn send_changes_email(self) -> crate::Result>> { self.send_single().await } - pub fn query_email( - &mut self, - ) -> &mut QueryRequest - { + pub fn query_email(&mut self) -> &mut QueryRequest> { self.add_method_call( Method::QueryEmail, Arguments::email_query(self.params(Method::QueryEmail)), @@ -229,11 +226,7 @@ impl Request<'_> { pub fn query_email_changes( &mut self, since_query_state: impl Into, - ) -> &mut QueryChangesRequest< - super::query::Filter, - super::query::Comparator, - super::QueryArguments, - > { + ) -> &mut QueryChangesRequest> { self.add_method_call( Method::QueryChangesEmail, Arguments::email_query_changes( @@ -248,7 +241,7 @@ impl Request<'_> { self.send_single().await } - pub fn set_email(&mut self) -> &mut SetRequest, ()> { + pub fn set_email(&mut self) -> &mut SetRequest> { self.add_method_call( Method::SetEmail, Arguments::email_set(self.params(Method::SetEmail)), diff --git a/src/email/mod.rs b/src/email/mod.rs index abe6f03..7eb6b41 100644 --- a/src/email/mod.rs +++ b/src/email/mod.rs @@ -14,10 +14,34 @@ use std::{ }; use crate::{ - core::{request::ResultReference, Type}, + core::{changes::ChangesObject, request::ResultReference, Object}, Get, Set, }; +impl Object for Email { + type Property = Property; + + fn requires_account_id() -> bool { + true + } +} + +impl Object for Email { + type Property = Property; + + fn requires_account_id() -> bool { + true + } +} + +impl ChangesObject for Email { + type ChangesResponse = (); +} + +impl ChangesObject for Email { + type ChangesResponse = (); +} + #[derive(Debug, Default, Clone, Serialize, Deserialize)] pub struct Email { #[serde(skip)] @@ -816,18 +840,6 @@ impl SubmissionCapabilities { } } -impl Type for Email { - fn requires_account_id() -> bool { - true - } -} - -impl Type for Property { - fn requires_account_id() -> bool { - true - } -} - #[cfg(feature = "debug")] use std::collections::BTreeMap; diff --git a/src/email/query.rs b/src/email/query.rs index d67f1c6..f7fd3ae 100644 --- a/src/email/query.rs +++ b/src/email/query.rs @@ -1,7 +1,15 @@ use chrono::{DateTime, Utc}; use serde::Serialize; -use crate::core::{query, set::from_timestamp}; +use crate::{ + core::{ + query::{self, QueryObject}, + set::from_timestamp, + }, + Set, +}; + +use super::{Email, QueryArguments}; #[derive(Serialize, Clone, Debug)] #[serde(untagged)] @@ -279,3 +287,11 @@ impl Comparator { }) } } + +impl QueryObject for Email { + type QueryArguments = QueryArguments; + + type Filter = Filter; + + type Sort = Comparator; +} diff --git a/src/email/set.rs b/src/email/set.rs index 32f44b0..907cdcc 100644 --- a/src/email/set.rs +++ b/src/email/set.rs @@ -3,9 +3,9 @@ use std::collections::HashMap; use crate::{ core::{ request::ResultReference, - set::{from_timestamp, Create}, + set::{from_timestamp, SetObject}, }, - Set, + Get, Set, }; use super::{ @@ -177,7 +177,9 @@ impl Email { } } -impl Create for Email { +impl SetObject for Email { + type SetArguments = (); + fn new(_create_id: Option) -> Email { Email { _create_id, @@ -218,6 +220,18 @@ impl Create for Email { } } +impl SetObject for Email { + type SetArguments = (); + + fn new(_create_id: Option) -> Email { + unimplemented!() + } + + fn create_id(&self) -> Option { + None + } +} + impl EmailBodyPart { pub fn new() -> EmailBodyPart { EmailBodyPart { diff --git a/src/email_submission/get.rs b/src/email_submission/get.rs index 287415e..7dd54ef 100644 --- a/src/email_submission/get.rs +++ b/src/email_submission/get.rs @@ -1,4 +1,4 @@ -use crate::Get; +use crate::{Get, core::get::GetObject, Set}; use super::{Address, Delivered, DeliveryStatus, Displayed, EmailSubmission, UndoStatus}; @@ -78,3 +78,11 @@ impl DeliveryStatus { &self.displayed } } + +impl GetObject for EmailSubmission { + type GetArguments = (); +} + +impl GetObject for EmailSubmission { + type GetArguments = (); +} diff --git a/src/email_submission/helpers.rs b/src/email_submission/helpers.rs index 056c5fa..f93ee51 100644 --- a/src/email_submission/helpers.rs +++ b/src/email_submission/helpers.rs @@ -1,20 +1,137 @@ use crate::{ + client::Client, core::{ changes::{ChangesRequest, ChangesResponse}, get::GetRequest, - query::{QueryRequest, QueryResponse}, + query::{Comparator, Filter, QueryRequest, QueryResponse}, query_changes::{QueryChangesRequest, QueryChangesResponse}, request::{Arguments, Request}, response::{EmailSubmissionGetResponse, EmailSubmissionSetResponse}, - set::SetRequest, + set::{SetObject, SetRequest}, }, - Method, Set, URI, + Get, Method, Set, URI, }; -use super::EmailSubmission; +use super::{Address, EmailSubmission, Property, UndoStatus}; + +impl Client { + pub async fn email_submission_create( + &mut self, + email_id: impl Into, + identity_id: impl Into, + ) -> crate::Result> { + let mut request = self.build(); + let id = request + .set_email_submission() + .create() + .email_id(email_id) + .identity_id(identity_id) + .create_id() + .unwrap(); + request + .send_single::() + .await? + .created(&id) + } + + pub async fn email_submission_create_envelope( + &mut self, + email_id: impl Into, + identity_id: impl Into, + mail_from: U, + rcpt_to: T, + ) -> crate::Result> + where + T: IntoIterator, + U: Into
, + { + let mut request = self.build(); + let id = request + .set_email_submission() + .create() + .email_id(email_id) + .identity_id(identity_id) + .envelope(mail_from, rcpt_to) + .create_id() + .unwrap(); + request + .send_single::() + .await? + .created(&id) + } + + pub async fn email_submission_change_status( + &mut self, + id: &str, + undo_status: UndoStatus, + ) -> crate::Result> { + let mut request = self.build(); + request + .set_email_submission() + .update(id) + .undo_status(undo_status); + request + .send_single::() + .await? + .updated(id) + } + + pub async fn email_submission_destroy(&mut self, id: &str) -> crate::Result<()> { + let mut request = self.build(); + request.set_email_submission().destroy([id]); + request + .send_single::() + .await? + .destroyed(id) + } + + pub async fn email_submission_get( + &mut self, + id: &str, + properties: Option>, + ) -> crate::Result> { + let mut request = self.build(); + let get_request = request.get_email_submission().ids([id]); + if let Some(properties) = properties { + get_request.properties(properties.into_iter()); + } + request + .send_single::() + .await + .map(|mut r| r.unwrap_list().pop()) + } + + pub async fn email_submission_query( + &mut self, + filter: Option>>, + sort: Option>>, + ) -> crate::Result { + let mut request = self.build(); + let query_request = request.query_email_submission(); + if let Some(filter) = filter { + query_request.filter(filter); + } + if let Some(sort) = sort { + query_request.sort(sort.into_iter()); + } + request.send_single::().await + } + + pub async fn email_submission_changes( + &mut self, + since_state: impl Into, + max_changes: usize, + ) -> crate::Result>> { + let mut request = self.build(); + request + .changes_email_submission(since_state) + .max_changes(max_changes); + request.send_single().await + } +} impl Request<'_> { - pub fn get_email_submission(&mut self) -> &mut GetRequest { + pub fn get_email_submission(&mut self) -> &mut GetRequest> { self.add_capability(URI::Submission); self.add_method_call( Method::GetEmailSubmission, @@ -42,13 +159,13 @@ impl Request<'_> { .changes_mut() } - pub async fn send_changes_email_submission(self) -> crate::Result> { + pub async fn send_changes_email_submission( + self, + ) -> crate::Result>> { self.send_single().await } - pub fn query_email_submission( - &mut self, - ) -> &mut QueryRequest { + pub fn query_email_submission(&mut self) -> &mut QueryRequest> { self.add_capability(URI::Submission); self.add_method_call( Method::QueryEmailSubmission, @@ -64,7 +181,7 @@ impl Request<'_> { pub fn query_email_submission_changes( &mut self, since_query_state: impl Into, - ) -> &mut QueryChangesRequest { + ) -> &mut QueryChangesRequest> { self.add_capability(URI::Submission); self.add_method_call( Method::QueryChangesEmailSubmission, @@ -80,9 +197,7 @@ impl Request<'_> { self.send_single().await } - pub fn set_email_submission( - &mut self, - ) -> &mut SetRequest, super::SetArguments> { + pub fn set_email_submission(&mut self) -> &mut SetRequest> { self.add_capability(URI::Submission); self.add_method_call( Method::SetEmailSubmission, diff --git a/src/email_submission/mod.rs b/src/email_submission/mod.rs index 62d94f9..732f274 100644 --- a/src/email_submission/mod.rs +++ b/src/email_submission/mod.rs @@ -8,7 +8,11 @@ use std::{collections::HashMap, fmt::Display}; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; -use crate::{core::Type, email::Email, Get, Set}; +use crate::{ + core::{changes::ChangesObject, Object}, + email::Email, + Get, Set, +}; #[derive(Debug, Clone, Serialize, Default)] pub struct SetArguments { @@ -168,14 +172,26 @@ impl Display for Property { } } -impl Type for EmailSubmission { +impl Object for EmailSubmission { + type Property = Property; + fn requires_account_id() -> bool { true } } -impl Type for Property { +impl Object for EmailSubmission { + type Property = Property; + fn requires_account_id() -> bool { true } } + +impl ChangesObject for EmailSubmission { + type ChangesResponse = (); +} + +impl ChangesObject for EmailSubmission { + type ChangesResponse = (); +} diff --git a/src/email_submission/query.rs b/src/email_submission/query.rs index 2232d3c..74a79bd 100644 --- a/src/email_submission/query.rs +++ b/src/email_submission/query.rs @@ -1,9 +1,15 @@ use chrono::{DateTime, Utc}; use serde::Serialize; -use crate::core::{query, set::from_timestamp}; +use crate::{ + core::{ + query::{self, QueryObject}, + set::from_timestamp, + }, + Set, +}; -use super::UndoStatus; +use super::{EmailSubmission, UndoStatus}; #[derive(Serialize, Clone, Debug)] #[serde(untagged)] @@ -106,3 +112,11 @@ impl Comparator { query::Comparator::new(Comparator::SentAt) } } + +impl QueryObject for EmailSubmission { + type QueryArguments = (); + + type Filter = Filter; + + type Sort = Comparator; +} diff --git a/src/email_submission/set.rs b/src/email_submission/set.rs index ebeba9e..e0b398d 100644 --- a/src/email_submission/set.rs +++ b/src/email_submission/set.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use crate::{core::set::Create, Set}; +use crate::{core::set::SetObject, Get, Set}; use super::{Address, EmailSubmission, Envelope, UndoStatus}; @@ -17,12 +17,12 @@ impl EmailSubmission { pub fn envelope(&mut self, mail_from: U, rcpt_to: T) -> &mut Self where - T: Iterator, + T: IntoIterator, U: Into
, { self.envelope = Some(Envelope { mail_from: mail_from.into(), - rcpt_to: rcpt_to.map(|s| s.into()).collect(), + rcpt_to: rcpt_to.into_iter().map(|s| s.into()).collect(), }); self } @@ -33,7 +33,9 @@ impl EmailSubmission { } } -impl Create for EmailSubmission { +impl SetObject for EmailSubmission { + type SetArguments = (); + fn new(_create_id: Option) -> Self { EmailSubmission { _create_id, @@ -56,6 +58,18 @@ impl Create for EmailSubmission { } } +impl SetObject for EmailSubmission { + type SetArguments = (); + + fn new(_create_id: Option) -> Self { + unimplemented!() + } + + fn create_id(&self) -> Option { + None + } +} + impl Address { pub fn new(email: String) -> Address { Address { diff --git a/src/identity/get.rs b/src/identity/get.rs index eb5f8b3..5715af3 100644 --- a/src/identity/get.rs +++ b/src/identity/get.rs @@ -1,4 +1,4 @@ -use crate::{email::EmailAddress, Get}; +use crate::{core::get::GetObject, email::EmailAddress, Get, Set}; use super::Identity; @@ -7,6 +7,10 @@ impl Identity { self.id.as_ref().unwrap() } + pub fn unwrap_id(self) -> String { + self.id.unwrap() + } + pub fn name(&self) -> Option<&str> { self.name.as_deref() } @@ -35,3 +39,11 @@ impl Identity { self.may_delete.unwrap_or(false) } } + +impl GetObject for Identity { + type GetArguments = (); +} + +impl GetObject for Identity { + type GetArguments = (); +} diff --git a/src/identity/helpers.rs b/src/identity/helpers.rs index d2036f3..e3f9802 100644 --- a/src/identity/helpers.rs +++ b/src/identity/helpers.rs @@ -1,17 +1,77 @@ use crate::{ + client::Client, core::{ + changes::{ChangesRequest, ChangesResponse}, get::GetRequest, request::{Arguments, Request}, response::{IdentityGetResponse, IdentitySetResponse}, - set::SetRequest, + set::{SetObject, SetRequest}, }, - Method, Set, + Get, Method, Set, }; -use super::Identity; +use super::{Identity, Property}; + +impl Client { + pub async fn identity_create( + &mut self, + name: impl Into, + email: impl Into, + ) -> crate::Result { + let mut request = self.build(); + let id = request + .set_identity() + .create() + .name(name) + .email(email) + .create_id() + .unwrap(); + request + .send_single::() + .await? + .created(&id) + } + + pub async fn identity_destroy(&mut self, id: &str) -> crate::Result<()> { + let mut request = self.build(); + request.set_identity().destroy([id]); + request + .send_single::() + .await? + .destroyed(id) + } + + pub async fn identity_get( + &mut self, + id: &str, + properties: Option>, + ) -> crate::Result> { + let mut request = self.build(); + let get_request = request.get_identity().ids([id]); + if let Some(properties) = properties { + get_request.properties(properties.into_iter()); + } + request + .send_single::() + .await + .map(|mut r| r.unwrap_list().pop()) + } + + pub async fn identity_changes( + &mut self, + since_state: impl Into, + max_changes: usize, + ) -> crate::Result>> { + let mut request = self.build(); + request + .changes_identity(since_state) + .max_changes(max_changes); + request.send_single().await + } +} impl Request<'_> { - pub fn get_identity(&mut self) -> &mut GetRequest { + pub fn get_identity(&mut self) -> &mut GetRequest> { self.add_method_call( Method::GetIdentity, Arguments::identity_get(self.params(Method::GetIdentity)), @@ -23,7 +83,7 @@ impl Request<'_> { self.send_single().await } - pub fn set_identity(&mut self) -> &mut SetRequest, ()> { + pub fn set_identity(&mut self) -> &mut SetRequest> { self.add_method_call( Method::SetIdentity, Arguments::identity_set(self.params(Method::SetIdentity)), @@ -34,4 +94,16 @@ impl Request<'_> { pub async fn send_set_identity(self) -> crate::Result { self.send_single().await } + + pub fn changes_identity(&mut self, since_state: impl Into) -> &mut ChangesRequest { + self.add_method_call( + Method::ChangesIdentity, + Arguments::changes(self.params(Method::ChangesIdentity), since_state.into()), + ) + .changes_mut() + } + + pub async fn send_changes_identity(self) -> crate::Result>> { + self.send_single().await + } } diff --git a/src/identity/mod.rs b/src/identity/mod.rs index 4b65d89..f89c7eb 100644 --- a/src/identity/mod.rs +++ b/src/identity/mod.rs @@ -4,8 +4,9 @@ pub mod set; use std::fmt::Display; +use crate::core::changes::ChangesObject; use crate::core::set::list_not_set; -use crate::core::Type; +use crate::core::Object; use crate::Set; use crate::{email::EmailAddress, Get}; use serde::{Deserialize, Serialize}; @@ -86,14 +87,26 @@ impl Display for Property { } } -impl Type for Identity { +impl Object for Identity { + type Property = Property; + fn requires_account_id() -> bool { true } } -impl Type for Property { +impl Object for Identity { + type Property = Property; + fn requires_account_id() -> bool { true } } + +impl ChangesObject for Identity { + type ChangesResponse = (); +} + +impl ChangesObject for Identity { + type ChangesResponse = (); +} diff --git a/src/identity/set.rs b/src/identity/set.rs index 1b9d38b..ba752b6 100644 --- a/src/identity/set.rs +++ b/src/identity/set.rs @@ -1,15 +1,15 @@ -use crate::{core::set::Create, email::EmailAddress, Set}; +use crate::{core::set::SetObject, email::EmailAddress, Get, Set}; use super::Identity; impl Identity { - pub fn name(&mut self, name: String) -> &mut Self { - self.name = Some(name); + pub fn name(&mut self, name: impl Into) -> &mut Self { + self.name = Some(name.into()); self } - pub fn email(&mut self, email: String) -> &mut Self { - self.email = Some(email); + pub fn email(&mut self, email: impl Into) -> &mut Self { + self.email = Some(email.into()); self } @@ -31,18 +31,20 @@ impl Identity { self } - pub fn text_signature(&mut self, text_signature: String) -> &mut Self { - self.text_signature = Some(text_signature); + pub fn text_signature(&mut self, text_signature: impl Into) -> &mut Self { + self.text_signature = Some(text_signature.into()); self } - pub fn html_signature(&mut self, html_signature: String) -> &mut Self { - self.html_signature = Some(html_signature); + pub fn html_signature(&mut self, html_signature: impl Into) -> &mut Self { + self.html_signature = Some(html_signature.into()); self } } -impl Create for Identity { +impl SetObject for Identity { + type SetArguments = (); + fn new(_create_id: Option) -> Self { Identity { _create_id, @@ -62,3 +64,15 @@ impl Create for Identity { self._create_id.map(|id| format!("c{}", id)) } } + +impl SetObject for Identity { + type SetArguments = (); + + fn new(_create_id: Option) -> Self { + unimplemented!() + } + + fn create_id(&self) -> Option { + None + } +} diff --git a/src/mailbox/get.rs b/src/mailbox/get.rs index 84f2611..28e19e1 100644 --- a/src/mailbox/get.rs +++ b/src/mailbox/get.rs @@ -1,4 +1,4 @@ -use crate::Get; +use crate::{core::get::GetObject, Get, Set}; use super::{Mailbox, Role}; @@ -83,3 +83,11 @@ impl Mailbox { self.my_rights.as_ref().unwrap().may_submit } } + +impl GetObject for Mailbox { + type GetArguments = (); +} + +impl GetObject for Mailbox { + type GetArguments = (); +} diff --git a/src/mailbox/helpers.rs b/src/mailbox/helpers.rs index 3325f66..6de05cf 100644 --- a/src/mailbox/helpers.rs +++ b/src/mailbox/helpers.rs @@ -7,9 +7,9 @@ use crate::{ query_changes::{QueryChangesRequest, QueryChangesResponse}, request::{Arguments, Request}, response::{MailboxGetResponse, MailboxSetResponse}, - set::{Create, SetRequest}, + set::{SetObject, SetRequest}, }, - Method, Set, + Get, Method, Set, }; use super::{Mailbox, Property, Role}; @@ -137,7 +137,7 @@ impl Client { &mut self, since_state: impl Into, max_changes: usize, - ) -> crate::Result> { + ) -> crate::Result>> { let mut request = self.build(); request .changes_mailbox(since_state) @@ -147,7 +147,7 @@ impl Client { } impl Request<'_> { - pub fn get_mailbox(&mut self) -> &mut GetRequest { + pub fn get_mailbox(&mut self) -> &mut GetRequest> { self.add_method_call( Method::GetMailbox, Arguments::mailbox_get(self.params(Method::GetMailbox)), @@ -167,16 +167,11 @@ impl Request<'_> { .changes_mut() } - pub async fn send_changes_mailbox( - self, - ) -> crate::Result> { + pub async fn send_changes_mailbox(self) -> crate::Result>> { self.send_single().await } - pub fn query_mailbox( - &mut self, - ) -> &mut QueryRequest - { + pub fn query_mailbox(&mut self) -> &mut QueryRequest> { self.add_method_call( Method::QueryMailbox, Arguments::mailbox_query(self.params(Method::QueryMailbox)), @@ -191,11 +186,7 @@ impl Request<'_> { pub fn query_mailbox_changes( &mut self, since_query_state: impl Into, - ) -> &mut QueryChangesRequest< - super::query::Filter, - super::query::Comparator, - super::QueryArguments, - > { + ) -> &mut QueryChangesRequest> { self.add_method_call( Method::QueryChangesMailbox, Arguments::mailbox_query_changes( @@ -210,7 +201,7 @@ impl Request<'_> { self.send_single().await } - pub fn set_mailbox(&mut self) -> &mut SetRequest, super::SetArguments> { + pub fn set_mailbox(&mut self) -> &mut SetRequest> { self.add_method_call( Method::SetMailbox, Arguments::mailbox_set(self.params(Method::SetMailbox)), diff --git a/src/mailbox/mod.rs b/src/mailbox/mod.rs index 4f15d25..13ea1c6 100644 --- a/src/mailbox/mod.rs +++ b/src/mailbox/mod.rs @@ -5,7 +5,9 @@ pub mod set; use std::fmt::Display; -use crate::core::{set::string_not_set, Type}; +use crate::core::changes::ChangesObject; +use crate::core::set::string_not_set; +use crate::core::Object; use crate::mailbox::set::role_not_set; use crate::{Get, Set}; use serde::{Deserialize, Serialize}; @@ -184,14 +186,26 @@ impl ChangesResponse { } } -impl Type for Mailbox { +impl Object for Mailbox { + type Property = Property; + fn requires_account_id() -> bool { true } } -impl Type for Property { +impl Object for Mailbox { + type Property = Property; + fn requires_account_id() -> bool { true } } + +impl ChangesObject for Mailbox { + type ChangesResponse = ChangesResponse; +} + +impl ChangesObject for Mailbox { + type ChangesResponse = ChangesResponse; +} diff --git a/src/mailbox/query.rs b/src/mailbox/query.rs index fdecd56..2b3d2a3 100644 --- a/src/mailbox/query.rs +++ b/src/mailbox/query.rs @@ -1,8 +1,11 @@ use serde::Serialize; -use crate::core::query::{self}; +use crate::{ + core::query::{self, QueryObject}, + Set, +}; -use super::{QueryArguments, Role}; +use super::{Mailbox, QueryArguments, Role}; #[derive(Serialize, Clone, Debug)] #[serde(untagged)] @@ -97,3 +100,11 @@ impl QueryArguments { self } } + +impl QueryObject for Mailbox { + type QueryArguments = QueryArguments; + + type Filter = Filter; + + type Sort = Comparator; +} diff --git a/src/mailbox/set.rs b/src/mailbox/set.rs index 21e274c..a16059f 100644 --- a/src/mailbox/set.rs +++ b/src/mailbox/set.rs @@ -1,4 +1,4 @@ -use crate::{core::set::Create, Set}; +use crate::{core::set::SetObject, Get, Set}; use super::{Mailbox, Role, SetArguments}; @@ -37,7 +37,9 @@ pub fn role_not_set(role: &Option) -> bool { matches!(role, Some(Role::None)) } -impl Create for Mailbox { +impl SetObject for Mailbox { + type SetArguments = SetArguments; + fn new(_create_id: Option) -> Self { Mailbox { _create_id, @@ -61,6 +63,18 @@ impl Create for Mailbox { } } +impl SetObject for Mailbox { + type SetArguments = SetArguments; + + fn new(_create_id: Option) -> Self { + unimplemented!() + } + + fn create_id(&self) -> Option { + None + } +} + impl SetArguments { pub fn on_destroy_remove_emails(&mut self, value: bool) -> &mut Self { self.on_destroy_remove_emails = value.into(); diff --git a/src/push_subscription/get.rs b/src/push_subscription/get.rs index e31d759..3413850 100644 --- a/src/push_subscription/get.rs +++ b/src/push_subscription/get.rs @@ -1,4 +1,4 @@ -use crate::{Get, TypeState}; +use crate::{core::get::GetObject, Get, TypeState, Set}; use super::{Keys, PushSubscription}; @@ -45,3 +45,11 @@ impl Keys { base64::decode_config(&self.auth, base64::URL_SAFE).ok() } } + +impl GetObject for PushSubscription { + type GetArguments = (); +} + +impl GetObject for PushSubscription { + type GetArguments = (); +} diff --git a/src/push_subscription/helpers.rs b/src/push_subscription/helpers.rs index 241bfdb..53756f2 100644 --- a/src/push_subscription/helpers.rs +++ b/src/push_subscription/helpers.rs @@ -4,7 +4,7 @@ use crate::{ get::GetRequest, request::{Arguments, Request}, response::{PushSubscriptionGetResponse, PushSubscriptionSetResponse}, - set::{Create, SetRequest}, + set::{SetObject, SetRequest}, }, Method, Set, TypeState, }; @@ -76,7 +76,7 @@ impl Client { } impl Request<'_> { - pub fn get_push_subscription(&mut self) -> &mut GetRequest { + pub fn get_push_subscription(&mut self) -> &mut GetRequest> { self.add_method_call( Method::GetPushSubscription, Arguments::push_get(self.params(Method::GetPushSubscription)), @@ -88,7 +88,7 @@ impl Request<'_> { self.send_single().await } - pub fn set_push_subscription(&mut self) -> &mut SetRequest, ()> { + pub fn set_push_subscription(&mut self) -> &mut SetRequest> { self.add_method_call( Method::SetPushSubscription, Arguments::push_set(self.params(Method::SetPushSubscription)), diff --git a/src/push_subscription/mod.rs b/src/push_subscription/mod.rs index a8ead03..1408ea2 100644 --- a/src/push_subscription/mod.rs +++ b/src/push_subscription/mod.rs @@ -7,8 +7,9 @@ use std::fmt::Display; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; +use crate::core::changes::ChangesObject; use crate::core::set::list_not_set; -use crate::core::Type; +use crate::core::Object; use crate::{Get, Set, TypeState}; #[derive(Debug, Clone, Serialize, Deserialize)] @@ -86,14 +87,26 @@ pub struct Keys { auth: String, } -impl Type for PushSubscription { +impl Object for PushSubscription { + type Property = Property; + fn requires_account_id() -> bool { false } } -impl Type for Property { +impl Object for PushSubscription { + type Property = Property; + fn requires_account_id() -> bool { false } } + +impl ChangesObject for PushSubscription { + type ChangesResponse = (); +} + +impl ChangesObject for PushSubscription { + type ChangesResponse = (); +} diff --git a/src/push_subscription/set.rs b/src/push_subscription/set.rs index 09c0401..7014750 100644 --- a/src/push_subscription/set.rs +++ b/src/push_subscription/set.rs @@ -1,6 +1,7 @@ use crate::{ - core::set::{from_timestamp, Create}, - Set, TypeState, + core::set::{from_timestamp, SetObject}, + email_submission::SetArguments, + Get, Set, TypeState, }; use super::{Keys, PushSubscription}; @@ -37,7 +38,9 @@ impl PushSubscription { } } -impl Create for PushSubscription { +impl SetObject for PushSubscription { + type SetArguments = SetArguments; + fn new(_create_id: Option) -> Self { PushSubscription { _create_id, @@ -57,6 +60,18 @@ impl Create for PushSubscription { } } +impl SetObject for PushSubscription { + type SetArguments = SetArguments; + + fn new(_create_id: Option) -> Self { + unimplemented!() + } + + fn create_id(&self) -> Option { + None + } +} + impl Keys { pub fn new(p256dh: &[u8], auth: &[u8]) -> Self { Keys { diff --git a/src/thread/helpers.rs b/src/thread/helpers.rs index 047149f..964ae73 100644 --- a/src/thread/helpers.rs +++ b/src/thread/helpers.rs @@ -2,7 +2,7 @@ use crate::{ client::Client, core::{ changes::{ChangesRequest, ChangesResponse}, - get::GetRequest, + get::{GetObject, GetRequest}, request::{Arguments, Request}, response::ThreadGetResponse, }, @@ -23,7 +23,7 @@ impl Client { } impl Request<'_> { - pub fn get_thread(&mut self) -> &mut GetRequest { + pub fn get_thread(&mut self) -> &mut GetRequest { self.add_method_call( Method::GetThread, Arguments::thread_get(self.params(Method::GetThread)), @@ -43,7 +43,11 @@ impl Request<'_> { .changes_mut() } - pub async fn send_changes_thread(self) -> crate::Result> { + pub async fn send_changes_thread(self) -> crate::Result> { self.send_single().await } } + +impl GetObject for Thread { + type GetArguments = (); +} diff --git a/src/thread/mod.rs b/src/thread/mod.rs index d4620c5..4427ce1 100644 --- a/src/thread/mod.rs +++ b/src/thread/mod.rs @@ -5,7 +5,7 @@ use std::fmt::Display; use serde::{Deserialize, Serialize}; -use crate::core::Type; +use crate::core::{Object, changes::ChangesObject}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Thread { @@ -22,7 +22,9 @@ pub enum Property { EmailIds, } -impl Type for Property { +impl Object for Thread { + type Property = Property; + fn requires_account_id() -> bool { true } @@ -36,3 +38,8 @@ impl Display for Property { } } } + +impl ChangesObject for Thread { + type ChangesResponse = (); +} + diff --git a/src/vacation_response/get.rs b/src/vacation_response/get.rs index c49e4c6..21b4f69 100644 --- a/src/vacation_response/get.rs +++ b/src/vacation_response/get.rs @@ -1,4 +1,4 @@ -use crate::Get; +use crate::{core::get::GetObject, Get, Set}; use super::VacationResponse; @@ -31,3 +31,11 @@ impl VacationResponse { self.html_body.as_deref() } } + +impl GetObject for VacationResponse { + type GetArguments = (); +} + +impl GetObject for VacationResponse { + type GetArguments = (); +} diff --git a/src/vacation_response/helpers.rs b/src/vacation_response/helpers.rs index 16cdc63..c29e237 100644 --- a/src/vacation_response/helpers.rs +++ b/src/vacation_response/helpers.rs @@ -11,7 +11,7 @@ use crate::{ use super::VacationResponse; impl Request<'_> { - pub fn get_vacation_response(&mut self) -> &mut GetRequest { + pub fn get_vacation_response(&mut self) -> &mut GetRequest> { self.add_capability(URI::VacationResponse); self.add_method_call( Method::GetVacationResponse, @@ -24,7 +24,7 @@ impl Request<'_> { self.send_single().await } - pub fn set_vacation_response(&mut self) -> &mut SetRequest, ()> { + pub fn set_vacation_response(&mut self) -> &mut SetRequest> { self.add_capability(URI::VacationResponse); self.add_method_call( Method::SetVacationResponse, diff --git a/src/vacation_response/mod.rs b/src/vacation_response/mod.rs index 3fe2210..51b1e23 100644 --- a/src/vacation_response/mod.rs +++ b/src/vacation_response/mod.rs @@ -4,9 +4,10 @@ pub mod set; use std::fmt::Display; +use crate::core::changes::ChangesObject; use crate::core::set::date_not_set; use crate::core::set::string_not_set; -use crate::core::Type; +use crate::core::Object; use crate::Get; use crate::Set; use chrono::{DateTime, Utc}; @@ -81,14 +82,26 @@ impl Display for Property { } } -impl Type for VacationResponse { +impl Object for VacationResponse { + type Property = Property; + fn requires_account_id() -> bool { true } } -impl Type for Property { +impl Object for VacationResponse { + type Property = Property; + fn requires_account_id() -> bool { true } } + +impl ChangesObject for VacationResponse { + type ChangesResponse = (); +} + +impl ChangesObject for VacationResponse { + type ChangesResponse = (); +} diff --git a/src/vacation_response/set.rs b/src/vacation_response/set.rs index 1d47f5d..9774cc0 100644 --- a/src/vacation_response/set.rs +++ b/src/vacation_response/set.rs @@ -1,6 +1,7 @@ use crate::{ - core::set::{from_timestamp, Create}, - Set, + core::set::{from_timestamp, SetObject}, + email_submission::SetArguments, + Get, Set, }; use super::VacationResponse; @@ -37,7 +38,9 @@ impl VacationResponse { } } -impl Create for VacationResponse { +impl SetObject for VacationResponse { + type SetArguments = SetArguments; + fn new(_create_id: Option) -> Self { VacationResponse { _create_id, @@ -56,3 +59,15 @@ impl Create for VacationResponse { self._create_id.map(|id| format!("c{}", id)) } } + +impl SetObject for VacationResponse { + type SetArguments = SetArguments; + + fn new(_create_id: Option) -> Self { + unimplemented!() + } + + fn create_id(&self) -> Option { + None + } +}