jmap_client/core/
response.rs

1/*
2 * Copyright Stalwart Labs LLC See the COPYING
3 * file at the top-level directory of this distribution.
4 *
5 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
8 * option. This file may not be copied, modified, or distributed
9 * except according to those terms.
10 */
11
12use ahash::AHashMap;
13use serde::{de::Visitor, Deserialize};
14use std::fmt;
15
16use crate::{
17    blob::copy::CopyBlobResponse,
18    email::{
19        import::EmailImportResponse, parse::EmailParseResponse,
20        search_snippet::SearchSnippetGetResponse, Email,
21    },
22    email_submission::EmailSubmission,
23    identity::Identity,
24    mailbox::Mailbox,
25    principal::Principal,
26    push_subscription::PushSubscription,
27    sieve::{validate::SieveScriptValidateResponse, SieveScript},
28    thread::Thread,
29    vacation_response::VacationResponse,
30    Get, Method,
31};
32
33use super::{
34    changes::ChangesResponse, copy::CopyResponse, error::MethodError, get::GetResponse,
35    query::QueryResponse, query_changes::QueryChangesResponse, set::SetResponse,
36};
37
38#[derive(Debug, Deserialize)]
39pub struct Response<T> {
40    #[serde(rename = "methodResponses")]
41    method_responses: Vec<T>,
42
43    #[serde(rename = "createdIds")]
44    created_ids: Option<AHashMap<String, String>>,
45
46    #[serde(rename = "sessionState")]
47    session_state: String,
48
49    request_id: Option<String>,
50}
51
52impl<T> Response<T> {
53    pub fn new(
54        method_responses: Vec<T>,
55        created_ids: Option<AHashMap<String, String>>,
56        session_state: String,
57        request_id: Option<String>,
58    ) -> Self {
59        Response {
60            method_responses,
61            created_ids,
62            session_state,
63            request_id,
64        }
65    }
66
67    pub fn method_responses(&self) -> &[T] {
68        self.method_responses.as_ref()
69    }
70
71    pub fn unwrap_method_responses(self) -> Vec<T> {
72        self.method_responses
73    }
74
75    pub fn method_response_by_pos(&mut self, index: usize) -> T {
76        self.method_responses.remove(index)
77    }
78
79    pub fn pop_method_response(&mut self) -> Option<T> {
80        self.method_responses.pop()
81    }
82
83    pub fn created_ids(&self) -> Option<impl Iterator<Item = (&String, &String)>> {
84        self.created_ids.as_ref().map(|map| map.iter())
85    }
86
87    pub fn session_state(&self) -> &str {
88        &self.session_state
89    }
90
91    pub fn request_id(&self) -> Option<&str> {
92        self.request_id.as_deref()
93    }
94}
95
96impl Response<TaggedMethodResponse> {
97    pub fn method_response_by_id(&self, id: &str) -> Option<&TaggedMethodResponse> {
98        self.method_responses
99            .iter()
100            .find(|response| response.call_id() == id)
101    }
102}
103
104#[derive(Debug, Deserialize)]
105#[serde(untagged)]
106pub enum SingleMethodResponse<T> {
107    Error((Error, MethodError, String)),
108    Ok((String, T, String)),
109}
110
111#[derive(Debug, Deserialize)]
112pub enum Error {
113    #[serde(rename = "error")]
114    Error,
115}
116
117pub type PushSubscriptionSetResponse = SetResponse<PushSubscription<Get>>;
118pub type PushSubscriptionGetResponse = GetResponse<PushSubscription<Get>>;
119pub type MailboxChangesResponse = ChangesResponse<Mailbox<Get>>;
120pub type MailboxSetResponse = SetResponse<Mailbox<Get>>;
121pub type MailboxGetResponse = GetResponse<Mailbox<Get>>;
122pub type ThreadGetResponse = GetResponse<Thread>;
123pub type ThreadChangesResponse = ChangesResponse<Thread>;
124pub type EmailGetResponse = GetResponse<Email<Get>>;
125pub type EmailSetResponse = SetResponse<Email<Get>>;
126pub type EmailCopyResponse = CopyResponse<Email<Get>>;
127pub type EmailChangesResponse = ChangesResponse<Email<Get>>;
128pub type IdentitySetResponse = SetResponse<Identity<Get>>;
129pub type IdentityGetResponse = GetResponse<Identity<Get>>;
130pub type IdentityChangesResponse = ChangesResponse<Identity<Get>>;
131pub type EmailSubmissionSetResponse = SetResponse<EmailSubmission<Get>>;
132pub type EmailSubmissionGetResponse = GetResponse<EmailSubmission<Get>>;
133pub type EmailSubmissionChangesResponse = ChangesResponse<EmailSubmission<Get>>;
134pub type VacationResponseGetResponse = GetResponse<VacationResponse<Get>>;
135pub type VacationResponseSetResponse = SetResponse<VacationResponse<Get>>;
136pub type SieveScriptGetResponse = GetResponse<SieveScript<Get>>;
137pub type SieveScriptSetResponse = SetResponse<SieveScript<Get>>;
138pub type PrincipalChangesResponse = ChangesResponse<Principal<Get>>;
139pub type PrincipalSetResponse = SetResponse<Principal<Get>>;
140pub type PrincipalGetResponse = GetResponse<Principal<Get>>;
141
142#[derive(Debug)]
143pub struct TaggedMethodResponse {
144    id: String,
145    response: MethodResponse,
146}
147
148#[derive(Debug)]
149pub enum MethodResponse {
150    CopyBlob(CopyBlobResponse),
151    GetPushSubscription(PushSubscriptionGetResponse),
152    SetPushSubscription(PushSubscriptionSetResponse),
153    GetMailbox(MailboxGetResponse),
154    ChangesMailbox(MailboxChangesResponse),
155    QueryMailbox(QueryResponse),
156    QueryChangesMailbox(QueryChangesResponse),
157    SetMailbox(MailboxSetResponse),
158    GetThread(ThreadGetResponse),
159    ChangesThread(ThreadChangesResponse),
160    GetEmail(EmailGetResponse),
161    ChangesEmail(EmailChangesResponse),
162    QueryEmail(QueryResponse),
163    QueryChangesEmail(QueryChangesResponse),
164    SetEmail(EmailSetResponse),
165    CopyEmail(EmailCopyResponse),
166    ImportEmail(EmailImportResponse),
167    ParseEmail(EmailParseResponse),
168    GetSearchSnippet(SearchSnippetGetResponse),
169    GetIdentity(IdentityGetResponse),
170    ChangesIdentity(IdentityChangesResponse),
171    SetIdentity(IdentitySetResponse),
172    GetEmailSubmission(EmailSubmissionGetResponse),
173    ChangesEmailSubmission(EmailSubmissionChangesResponse),
174    QueryEmailSubmission(QueryResponse),
175    QueryChangesEmailSubmission(QueryChangesResponse),
176    SetEmailSubmission(EmailSubmissionSetResponse),
177    GetVacationResponse(VacationResponseGetResponse),
178    SetVacationResponse(VacationResponseSetResponse),
179    GetSieveScript(SieveScriptGetResponse),
180    QuerySieveScript(QueryResponse),
181    SetSieveScript(SieveScriptSetResponse),
182    ValidateSieveScript(SieveScriptValidateResponse),
183
184    GetPrincipal(PrincipalGetResponse),
185    ChangesPrincipal(PrincipalChangesResponse),
186    QueryPrincipal(QueryResponse),
187    QueryChangesPrincipal(QueryChangesResponse),
188    SetPrincipal(PrincipalSetResponse),
189
190    Echo(serde_json::Value),
191    Error(MethodError),
192}
193
194impl TaggedMethodResponse {
195    pub fn call_id(&self) -> &str {
196        self.id.as_str()
197    }
198
199    pub fn is_type(&self, type_: Method) -> bool {
200        matches!(
201            (&self.response, type_),
202            (MethodResponse::CopyBlob(_), Method::CopyBlob)
203                | (
204                    MethodResponse::GetPushSubscription(_),
205                    Method::GetPushSubscription
206                )
207                | (
208                    MethodResponse::SetPushSubscription(_),
209                    Method::SetPushSubscription
210                )
211                | (MethodResponse::GetMailbox(_), Method::GetMailbox)
212                | (MethodResponse::ChangesMailbox(_), Method::ChangesMailbox)
213                | (MethodResponse::QueryMailbox(_), Method::QueryMailbox)
214                | (
215                    MethodResponse::QueryChangesMailbox(_),
216                    Method::QueryChangesMailbox
217                )
218                | (MethodResponse::SetMailbox(_), Method::SetMailbox)
219                | (MethodResponse::GetThread(_), Method::GetThread)
220                | (MethodResponse::ChangesThread(_), Method::ChangesThread)
221                | (MethodResponse::GetEmail(_), Method::GetEmail)
222                | (MethodResponse::ChangesEmail(_), Method::ChangesEmail)
223                | (MethodResponse::QueryEmail(_), Method::QueryEmail)
224                | (
225                    MethodResponse::QueryChangesEmail(_),
226                    Method::QueryChangesEmail
227                )
228                | (MethodResponse::SetEmail(_), Method::SetEmail)
229                | (MethodResponse::CopyEmail(_), Method::CopyEmail)
230                | (MethodResponse::ImportEmail(_), Method::ImportEmail)
231                | (MethodResponse::ParseEmail(_), Method::ParseEmail)
232                | (
233                    MethodResponse::GetSearchSnippet(_),
234                    Method::GetSearchSnippet
235                )
236                | (MethodResponse::GetIdentity(_), Method::GetIdentity)
237                | (MethodResponse::ChangesIdentity(_), Method::ChangesIdentity)
238                | (MethodResponse::SetIdentity(_), Method::SetIdentity)
239                | (
240                    MethodResponse::GetEmailSubmission(_),
241                    Method::GetEmailSubmission
242                )
243                | (
244                    MethodResponse::ChangesEmailSubmission(_),
245                    Method::ChangesEmailSubmission
246                )
247                | (
248                    MethodResponse::QueryEmailSubmission(_),
249                    Method::QueryEmailSubmission
250                )
251                | (
252                    MethodResponse::QueryChangesEmailSubmission(_),
253                    Method::QueryChangesEmailSubmission
254                )
255                | (
256                    MethodResponse::SetEmailSubmission(_),
257                    Method::SetEmailSubmission
258                )
259                | (
260                    MethodResponse::GetVacationResponse(_),
261                    Method::GetVacationResponse
262                )
263                | (
264                    MethodResponse::SetVacationResponse(_),
265                    Method::SetVacationResponse
266                )
267                | (MethodResponse::GetSieveScript(_), Method::GetSieveScript)
268                | (
269                    MethodResponse::ValidateSieveScript(_),
270                    Method::ValidateSieveScript
271                )
272                | (
273                    MethodResponse::QuerySieveScript(_),
274                    Method::QuerySieveScript
275                )
276                | (MethodResponse::SetSieveScript(_), Method::SetSieveScript)
277                | (MethodResponse::GetPrincipal(_), Method::GetPrincipal)
278                | (
279                    MethodResponse::ChangesPrincipal(_),
280                    Method::ChangesPrincipal
281                )
282                | (MethodResponse::QueryPrincipal(_), Method::QueryPrincipal)
283                | (
284                    MethodResponse::QueryChangesPrincipal(_),
285                    Method::QueryChangesPrincipal
286                )
287                | (MethodResponse::SetPrincipal(_), Method::SetPrincipal)
288                | (MethodResponse::Echo(_), Method::Echo)
289                | (MethodResponse::Error(_), Method::Error)
290        )
291    }
292
293    pub fn unwrap_method_response(self) -> MethodResponse {
294        self.response
295    }
296
297    pub fn unwrap_copy_blob(self) -> crate::Result<CopyBlobResponse> {
298        match self.response {
299            MethodResponse::CopyBlob(response) => Ok(response),
300            MethodResponse::Error(err) => Err(err.into()),
301            _ => Err("Response type mismatch".into()),
302        }
303    }
304
305    pub fn unwrap_get_push_subscription(self) -> crate::Result<PushSubscriptionGetResponse> {
306        match self.response {
307            MethodResponse::GetPushSubscription(response) => Ok(response),
308            MethodResponse::Error(err) => Err(err.into()),
309            _ => Err("Response type mismatch".into()),
310        }
311    }
312
313    pub fn unwrap_set_push_subscription(self) -> crate::Result<PushSubscriptionSetResponse> {
314        match self.response {
315            MethodResponse::SetPushSubscription(response) => Ok(response),
316            MethodResponse::Error(err) => Err(err.into()),
317            _ => Err("Response type mismatch".into()),
318        }
319    }
320
321    pub fn unwrap_get_mailbox(self) -> crate::Result<MailboxGetResponse> {
322        match self.response {
323            MethodResponse::GetMailbox(response) => Ok(response),
324            MethodResponse::Error(err) => Err(err.into()),
325            _ => Err("Response type mismatch".into()),
326        }
327    }
328
329    pub fn unwrap_changes_mailbox(self) -> crate::Result<MailboxChangesResponse> {
330        match self.response {
331            MethodResponse::ChangesMailbox(response) => Ok(response),
332            MethodResponse::Error(err) => Err(err.into()),
333            _ => Err("Response type mismatch".into()),
334        }
335    }
336
337    pub fn unwrap_query_mailbox(self) -> crate::Result<QueryResponse> {
338        match self.response {
339            MethodResponse::QueryMailbox(response) => Ok(response),
340            MethodResponse::Error(err) => Err(err.into()),
341            _ => Err("Response type mismatch".into()),
342        }
343    }
344
345    pub fn unwrap_query_changes_mailbox(self) -> crate::Result<QueryChangesResponse> {
346        match self.response {
347            MethodResponse::QueryChangesMailbox(response) => Ok(response),
348            MethodResponse::Error(err) => Err(err.into()),
349            _ => Err("Response type mismatch".into()),
350        }
351    }
352
353    pub fn unwrap_set_mailbox(self) -> crate::Result<MailboxSetResponse> {
354        match self.response {
355            MethodResponse::SetMailbox(response) => Ok(response),
356            MethodResponse::Error(err) => Err(err.into()),
357            _ => Err("Response type mismatch".into()),
358        }
359    }
360
361    pub fn unwrap_get_thread(self) -> crate::Result<ThreadGetResponse> {
362        match self.response {
363            MethodResponse::GetThread(response) => Ok(response),
364            MethodResponse::Error(err) => Err(err.into()),
365            _ => Err("Response type mismatch".into()),
366        }
367    }
368
369    pub fn unwrap_changes_thread(self) -> crate::Result<ThreadChangesResponse> {
370        match self.response {
371            MethodResponse::ChangesThread(response) => Ok(response),
372            MethodResponse::Error(err) => Err(err.into()),
373            _ => Err("Response type mismatch".into()),
374        }
375    }
376
377    pub fn unwrap_get_email(self) -> crate::Result<EmailGetResponse> {
378        match self.response {
379            MethodResponse::GetEmail(response) => Ok(response),
380            MethodResponse::Error(err) => Err(err.into()),
381            _ => Err("Response type mismatch".into()),
382        }
383    }
384
385    pub fn unwrap_changes_email(self) -> crate::Result<EmailChangesResponse> {
386        match self.response {
387            MethodResponse::ChangesEmail(response) => Ok(response),
388            MethodResponse::Error(err) => Err(err.into()),
389            _ => Err("Response type mismatch".into()),
390        }
391    }
392
393    pub fn unwrap_query_email(self) -> crate::Result<QueryResponse> {
394        match self.response {
395            MethodResponse::QueryEmail(response) => Ok(response),
396            MethodResponse::Error(err) => Err(err.into()),
397            _ => Err("Response type mismatch".into()),
398        }
399    }
400
401    pub fn unwrap_query_changes_email(self) -> crate::Result<QueryChangesResponse> {
402        match self.response {
403            MethodResponse::QueryChangesEmail(response) => Ok(response),
404            MethodResponse::Error(err) => Err(err.into()),
405            _ => Err("Response type mismatch".into()),
406        }
407    }
408
409    pub fn unwrap_set_email(self) -> crate::Result<EmailSetResponse> {
410        match self.response {
411            MethodResponse::SetEmail(response) => Ok(response),
412            MethodResponse::Error(err) => Err(err.into()),
413            _ => Err("Response type mismatch".into()),
414        }
415    }
416
417    pub fn unwrap_copy_email(self) -> crate::Result<EmailCopyResponse> {
418        match self.response {
419            MethodResponse::CopyEmail(response) => Ok(response),
420            MethodResponse::Error(err) => Err(err.into()),
421            _ => Err("Response type mismatch".into()),
422        }
423    }
424
425    pub fn unwrap_import_email(self) -> crate::Result<EmailImportResponse> {
426        match self.response {
427            MethodResponse::ImportEmail(response) => Ok(response),
428            MethodResponse::Error(err) => Err(err.into()),
429            _ => Err("Response type mismatch".into()),
430        }
431    }
432
433    pub fn unwrap_parse_email(self) -> crate::Result<EmailParseResponse> {
434        match self.response {
435            MethodResponse::ParseEmail(response) => Ok(response),
436            MethodResponse::Error(err) => Err(err.into()),
437            _ => Err("Response type mismatch".into()),
438        }
439    }
440
441    pub fn unwrap_get_search_snippet(self) -> crate::Result<SearchSnippetGetResponse> {
442        match self.response {
443            MethodResponse::GetSearchSnippet(response) => Ok(response),
444            MethodResponse::Error(err) => Err(err.into()),
445            _ => Err("Response type mismatch".into()),
446        }
447    }
448
449    pub fn unwrap_get_identity(self) -> crate::Result<IdentityGetResponse> {
450        match self.response {
451            MethodResponse::GetIdentity(response) => Ok(response),
452            MethodResponse::Error(err) => Err(err.into()),
453            _ => Err("Response type mismatch".into()),
454        }
455    }
456
457    pub fn unwrap_changes_identity(self) -> crate::Result<IdentityChangesResponse> {
458        match self.response {
459            MethodResponse::ChangesIdentity(response) => Ok(response),
460            MethodResponse::Error(err) => Err(err.into()),
461            _ => Err("Response type mismatch".into()),
462        }
463    }
464
465    pub fn unwrap_set_identity(self) -> crate::Result<IdentitySetResponse> {
466        match self.response {
467            MethodResponse::SetIdentity(response) => Ok(response),
468            MethodResponse::Error(err) => Err(err.into()),
469            _ => Err("Response type mismatch".into()),
470        }
471    }
472
473    pub fn unwrap_get_email_submission(self) -> crate::Result<EmailSubmissionGetResponse> {
474        match self.response {
475            MethodResponse::GetEmailSubmission(response) => Ok(response),
476            MethodResponse::Error(err) => Err(err.into()),
477            _ => Err("Response type mismatch".into()),
478        }
479    }
480
481    pub fn unwrap_changes_email_submission(self) -> crate::Result<EmailSubmissionChangesResponse> {
482        match self.response {
483            MethodResponse::ChangesEmailSubmission(response) => Ok(response),
484            MethodResponse::Error(err) => Err(err.into()),
485            _ => Err("Response type mismatch".into()),
486        }
487    }
488
489    pub fn unwrap_set_email_submission(self) -> crate::Result<EmailSubmissionSetResponse> {
490        match self.response {
491            MethodResponse::SetEmailSubmission(response) => Ok(response),
492            MethodResponse::Error(err) => Err(err.into()),
493            _ => Err("Response type mismatch".into()),
494        }
495    }
496
497    pub fn unwrap_query_email_submission(self) -> crate::Result<QueryResponse> {
498        match self.response {
499            MethodResponse::QueryEmailSubmission(response) => Ok(response),
500            MethodResponse::Error(err) => Err(err.into()),
501            _ => Err("Response type mismatch".into()),
502        }
503    }
504
505    pub fn unwrap_query_changes_email_submission(self) -> crate::Result<QueryChangesResponse> {
506        match self.response {
507            MethodResponse::QueryChangesEmailSubmission(response) => Ok(response),
508            MethodResponse::Error(err) => Err(err.into()),
509            _ => Err("Response type mismatch".into()),
510        }
511    }
512
513    pub fn unwrap_get_vacation_response(self) -> crate::Result<VacationResponseGetResponse> {
514        match self.response {
515            MethodResponse::GetVacationResponse(response) => Ok(response),
516            MethodResponse::Error(err) => Err(err.into()),
517            _ => Err("Response type mismatch".into()),
518        }
519    }
520
521    pub fn unwrap_set_vacation_response(self) -> crate::Result<VacationResponseSetResponse> {
522        match self.response {
523            MethodResponse::SetVacationResponse(response) => Ok(response),
524            MethodResponse::Error(err) => Err(err.into()),
525            _ => Err("Response type mismatch".into()),
526        }
527    }
528
529    pub fn unwrap_get_sieve_script(self) -> crate::Result<SieveScriptGetResponse> {
530        match self.response {
531            MethodResponse::GetSieveScript(response) => Ok(response),
532            MethodResponse::Error(err) => Err(err.into()),
533            _ => Err("Response type mismatch".into()),
534        }
535    }
536
537    pub fn unwrap_validate_sieve_script(self) -> crate::Result<SieveScriptValidateResponse> {
538        match self.response {
539            MethodResponse::ValidateSieveScript(response) => Ok(response),
540            MethodResponse::Error(err) => Err(err.into()),
541            _ => Err("Response type mismatch".into()),
542        }
543    }
544
545    pub fn unwrap_set_sieve_script(self) -> crate::Result<SieveScriptSetResponse> {
546        match self.response {
547            MethodResponse::SetSieveScript(response) => Ok(response),
548            MethodResponse::Error(err) => Err(err.into()),
549            _ => Err("Response type mismatch".into()),
550        }
551    }
552
553    pub fn unwrap_query_sieve_script(self) -> crate::Result<QueryResponse> {
554        match self.response {
555            MethodResponse::QuerySieveScript(response) => Ok(response),
556            MethodResponse::Error(err) => Err(err.into()),
557            _ => Err("Response type mismatch".into()),
558        }
559    }
560
561    pub fn unwrap_get_principal(self) -> crate::Result<PrincipalGetResponse> {
562        match self.response {
563            MethodResponse::GetPrincipal(response) => Ok(response),
564            MethodResponse::Error(err) => Err(err.into()),
565            _ => Err("Response type mismatch".into()),
566        }
567    }
568
569    pub fn unwrap_changes_principal(self) -> crate::Result<PrincipalChangesResponse> {
570        match self.response {
571            MethodResponse::ChangesPrincipal(response) => Ok(response),
572            MethodResponse::Error(err) => Err(err.into()),
573            _ => Err("Response type mismatch".into()),
574        }
575    }
576
577    pub fn unwrap_query_principal(self) -> crate::Result<QueryResponse> {
578        match self.response {
579            MethodResponse::QueryPrincipal(response) => Ok(response),
580            MethodResponse::Error(err) => Err(err.into()),
581            _ => Err("Response type mismatch".into()),
582        }
583    }
584
585    pub fn unwrap_query_changes_principal(self) -> crate::Result<QueryChangesResponse> {
586        match self.response {
587            MethodResponse::QueryChangesPrincipal(response) => Ok(response),
588            MethodResponse::Error(err) => Err(err.into()),
589            _ => Err("Response type mismatch".into()),
590        }
591    }
592
593    pub fn unwrap_set_principal(self) -> crate::Result<PrincipalSetResponse> {
594        match self.response {
595            MethodResponse::SetPrincipal(response) => Ok(response),
596            MethodResponse::Error(err) => Err(err.into()),
597            _ => Err("Response type mismatch".into()),
598        }
599    }
600
601    pub fn unwrap_echo(self) -> crate::Result<serde_json::Value> {
602        match self.response {
603            MethodResponse::Echo(response) => Ok(response),
604            MethodResponse::Error(err) => Err(err.into()),
605            _ => Err("Response type mismatch".into()),
606        }
607    }
608
609    pub fn is_error(&self) -> bool {
610        matches!(self.response, MethodResponse::Error(_))
611    }
612}
613
614impl<'de> Deserialize<'de> for TaggedMethodResponse {
615    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
616    where
617        D: serde::Deserializer<'de>,
618    {
619        deserializer.deserialize_seq(TaggedMethodResponseVisitor)
620    }
621}
622
623struct TaggedMethodResponseVisitor;
624
625impl<'de> Visitor<'de> for TaggedMethodResponseVisitor {
626    type Value = TaggedMethodResponse;
627
628    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
629        formatter.write_str("a valid JMAP method response")
630    }
631
632    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
633    where
634        A: serde::de::SeqAccess<'de>,
635    {
636        let response = match seq
637            .next_element::<Method>()?
638            .ok_or_else(|| serde::de::Error::custom("Expected a method name"))?
639        {
640            Method::Echo => MethodResponse::Echo(
641                seq.next_element()?
642                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
643            ),
644            Method::CopyBlob => MethodResponse::CopyBlob(
645                seq.next_element()?
646                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
647            ),
648            Method::GetPushSubscription => MethodResponse::GetPushSubscription(
649                seq.next_element()?
650                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
651            ),
652            Method::SetPushSubscription => MethodResponse::SetPushSubscription(
653                seq.next_element()?
654                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
655            ),
656            Method::GetMailbox => MethodResponse::GetMailbox(
657                seq.next_element()?
658                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
659            ),
660            Method::ChangesMailbox => MethodResponse::ChangesMailbox(
661                seq.next_element()?
662                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
663            ),
664            Method::QueryMailbox => MethodResponse::QueryMailbox(
665                seq.next_element()?
666                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
667            ),
668            Method::QueryChangesMailbox => MethodResponse::QueryChangesMailbox(
669                seq.next_element()?
670                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
671            ),
672            Method::SetMailbox => MethodResponse::SetMailbox(
673                seq.next_element()?
674                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
675            ),
676            Method::GetThread => MethodResponse::GetThread(
677                seq.next_element()?
678                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
679            ),
680            Method::ChangesThread => MethodResponse::ChangesThread(
681                seq.next_element()?
682                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
683            ),
684            Method::GetEmail => MethodResponse::GetEmail(
685                seq.next_element()?
686                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
687            ),
688            Method::ChangesEmail => MethodResponse::ChangesEmail(
689                seq.next_element()?
690                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
691            ),
692            Method::QueryEmail => MethodResponse::QueryEmail(
693                seq.next_element()?
694                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
695            ),
696            Method::QueryChangesEmail => MethodResponse::QueryChangesEmail(
697                seq.next_element()?
698                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
699            ),
700            Method::SetEmail => MethodResponse::SetEmail(
701                seq.next_element()?
702                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
703            ),
704            Method::CopyEmail => MethodResponse::CopyEmail(
705                seq.next_element()?
706                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
707            ),
708            Method::ImportEmail => MethodResponse::ImportEmail(
709                seq.next_element()?
710                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
711            ),
712            Method::ParseEmail => MethodResponse::ParseEmail(
713                seq.next_element()?
714                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
715            ),
716            Method::GetSearchSnippet => MethodResponse::GetSearchSnippet(
717                seq.next_element()?
718                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
719            ),
720            Method::GetIdentity => MethodResponse::GetIdentity(
721                seq.next_element()?
722                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
723            ),
724            Method::ChangesIdentity => MethodResponse::ChangesIdentity(
725                seq.next_element()?
726                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
727            ),
728            Method::SetIdentity => MethodResponse::SetIdentity(
729                seq.next_element()?
730                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
731            ),
732            Method::GetEmailSubmission => MethodResponse::GetEmailSubmission(
733                seq.next_element()?
734                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
735            ),
736            Method::ChangesEmailSubmission => MethodResponse::ChangesEmailSubmission(
737                seq.next_element()?
738                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
739            ),
740            Method::QueryEmailSubmission => MethodResponse::QueryEmailSubmission(
741                seq.next_element()?
742                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
743            ),
744            Method::QueryChangesEmailSubmission => MethodResponse::QueryChangesEmailSubmission(
745                seq.next_element()?
746                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
747            ),
748            Method::SetEmailSubmission => MethodResponse::SetEmailSubmission(
749                seq.next_element()?
750                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
751            ),
752            Method::GetVacationResponse => MethodResponse::GetVacationResponse(
753                seq.next_element()?
754                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
755            ),
756            Method::SetVacationResponse => MethodResponse::SetVacationResponse(
757                seq.next_element()?
758                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
759            ),
760            Method::GetSieveScript => MethodResponse::GetSieveScript(
761                seq.next_element()?
762                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
763            ),
764            Method::SetSieveScript => MethodResponse::SetSieveScript(
765                seq.next_element()?
766                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
767            ),
768            Method::QuerySieveScript => MethodResponse::QuerySieveScript(
769                seq.next_element()?
770                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
771            ),
772            Method::ValidateSieveScript => MethodResponse::ValidateSieveScript(
773                seq.next_element()?
774                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
775            ),
776            Method::GetPrincipal => MethodResponse::GetPrincipal(
777                seq.next_element()?
778                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
779            ),
780            Method::ChangesPrincipal => MethodResponse::ChangesPrincipal(
781                seq.next_element()?
782                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
783            ),
784            Method::QueryPrincipal => MethodResponse::QueryPrincipal(
785                seq.next_element()?
786                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
787            ),
788            Method::QueryChangesPrincipal => MethodResponse::QueryChangesPrincipal(
789                seq.next_element()?
790                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
791            ),
792            Method::SetPrincipal => MethodResponse::SetPrincipal(
793                seq.next_element()?
794                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
795            ),
796            Method::Error => MethodResponse::Error(
797                seq.next_element()?
798                    .ok_or_else(|| serde::de::Error::custom("Expected a method response"))?,
799            ),
800        };
801
802        let id = seq
803            .next_element::<String>()?
804            .ok_or_else(|| serde::de::Error::custom("Expected method call id"))?;
805
806        Ok(TaggedMethodResponse { response, id })
807    }
808}