io_jmap/rfc8621/vacation_response/
get.rs1use core::fmt;
56
57use alloc::{
58 string::{String, ToString},
59 vec,
60 vec::Vec,
61};
62
63use log::trace;
64use secrecy::SecretString;
65use serde::Serialize;
66use thiserror::Error;
67
68use crate::{
69 coroutine::*,
70 jmap_try,
71 rfc8620::{CORE_CAPABILITY, JmapBatch, JmapSession, get::*, send::*},
72 rfc8621::{
73 MAIL_CAPABILITY,
74 vacation_response::{JmapVacationResponse, VACATION_RESPONSE_CAPABILITY},
75 },
76};
77
78#[derive(Debug, Error)]
80pub enum JmapVacationResponseGetError {
81 #[error("JMAP VacationResponse/get failed: {0}")]
82 Send(#[from] JmapSendError),
83 #[error("JMAP VacationResponse/get failed: serialize args: {0}")]
84 SerializeArgs(#[source] serde_json::Error),
85 #[error("JMAP VacationResponse/get failed: {0}")]
86 Get(#[from] JmapGetError),
87}
88
89#[derive(Clone, Debug)]
91pub struct JmapVacationResponseGetOutput {
92 pub vacation_response: Option<JmapVacationResponse>,
93 pub new_state: String,
94 pub keep_alive: bool,
95}
96
97pub struct JmapVacationResponseGet {
99 state: State,
100}
101
102impl JmapVacationResponseGet {
103 pub fn new(
104 session: &JmapSession,
105 http_auth: &SecretString,
106 ) -> Result<Self, JmapVacationResponseGetError> {
107 let account_id = session
108 .primary_accounts
109 .get(VACATION_RESPONSE_CAPABILITY)
110 .or_else(|| session.primary_accounts.get(MAIL_CAPABILITY))
111 .cloned()
112 .unwrap_or_default();
113 let api_url = &session.api_url;
114
115 let args = serde_json::to_value(VacationResponseGetArgs {
116 account_id,
117 ids: vec!["singleton".to_string()],
118 })
119 .map_err(JmapVacationResponseGetError::SerializeArgs)?;
120
121 let mut using = vec![CORE_CAPABILITY.into(), MAIL_CAPABILITY.into()];
122 if session
123 .capabilities
124 .contains_key(VACATION_RESPONSE_CAPABILITY)
125 {
126 using.push(VACATION_RESPONSE_CAPABILITY.into());
127 }
128
129 let mut batch = JmapBatch::new();
130 batch.add("VacationResponse/get", args);
131 let request = batch.into_request(using);
132
133 let send = JmapSend::new(http_auth, api_url, request)?;
134 Ok(Self {
135 state: State::Get(JmapGet::from_send(send)),
136 })
137 }
138}
139
140impl JmapCoroutine for JmapVacationResponseGet {
141 type Yield = JmapYield;
142 type Return = Result<JmapVacationResponseGetOutput, JmapVacationResponseGetError>;
143
144 fn resume(&mut self, arg: Option<&[u8]>) -> JmapCoroutineState<Self::Yield, Self::Return> {
145 trace!("VacationResponse/get: {}", self.state);
146 match &mut self.state {
147 State::Get(get) => {
148 let JmapGetOutput {
149 list,
150 state,
151 keep_alive,
152 ..
153 } = jmap_try!(get, arg);
154 JmapCoroutineState::Complete(Ok(JmapVacationResponseGetOutput {
155 vacation_response: list.into_iter().next(),
156 new_state: state,
157 keep_alive,
158 }))
159 }
160 }
161 }
162}
163
164enum State {
165 Get(JmapGet<JmapVacationResponse>),
166}
167
168impl fmt::Display for State {
169 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170 match self {
171 Self::Get(_) => f.write_str("get"),
172 }
173 }
174}
175
176#[derive(Serialize)]
177#[serde(rename_all = "camelCase")]
178struct VacationResponseGetArgs {
179 account_id: String,
180 ids: Vec<String>,
181}