use alloc::{string::String, vec, vec::Vec};
use secrecy::SecretString;
use serde::Serialize;
use thiserror::Error;
use crate::{
calendars::{JMAP_CALENDARS_CAPABILITY, calendar_event::JmapCalendarEvent},
coroutine::*,
jmap_try,
rfc8620::{JMAP_CORE_CAPABILITY, get::*, request::JmapBatch, send::*, session::JmapSession},
};
#[derive(Debug, Error)]
pub enum JmapCalendarEventGetError {
#[error("JMAP CalendarEvent/get failed: {0}")]
Send(#[from] JmapSendError),
#[error("JMAP CalendarEvent/get failed: serialize args: {0}")]
SerializeArgs(#[source] serde_json::Error),
#[error("JMAP CalendarEvent/get failed: {0}")]
Get(#[from] JmapGetError),
}
#[derive(Clone, Debug, Default)]
pub struct JmapCalendarEventGetOptions {
pub ids: Option<Vec<String>>,
pub properties: Option<Vec<String>>,
pub recurrence_overrides_before: Option<String>,
pub recurrence_overrides_after: Option<String>,
pub reduce_participants: bool,
pub time_zone: Option<String>,
}
#[derive(Clone, Debug)]
pub struct JmapCalendarEventGetOutput {
pub events: Vec<JmapCalendarEvent>,
pub not_found: Vec<String>,
pub new_state: String,
pub keep_alive: bool,
}
pub struct JmapCalendarEventGet {
state: State,
}
impl JmapCalendarEventGet {
pub fn new(
session: &JmapSession,
http_auth: &SecretString,
opts: JmapCalendarEventGetOptions,
) -> Result<Self, JmapCalendarEventGetError> {
let account_id = session
.primary_accounts
.get(JMAP_CALENDARS_CAPABILITY)
.cloned()
.unwrap_or_default();
let api_url = &session.api_url;
let args = serde_json::to_value(CalendarEventGetArgs {
account_id: &account_id,
ids: opts.ids.as_deref(),
properties: opts.properties.as_deref(),
recurrence_overrides_before: opts.recurrence_overrides_before.as_deref(),
recurrence_overrides_after: opts.recurrence_overrides_after.as_deref(),
reduce_participants: opts.reduce_participants,
time_zone: opts.time_zone.as_deref(),
})
.map_err(JmapCalendarEventGetError::SerializeArgs)?;
let mut batch = JmapBatch::new();
batch.add("CalendarEvent/get", args);
let request = batch.into_request(vec![
JMAP_CORE_CAPABILITY.into(),
JMAP_CALENDARS_CAPABILITY.into(),
]);
let send = JmapSend::new(http_auth, api_url, request)?;
Ok(Self {
state: State::Get(JmapGet::from_send(send)),
})
}
}
impl JmapCoroutine for JmapCalendarEventGet {
type Yield = JmapYield;
type Return = Result<JmapCalendarEventGetOutput, JmapCalendarEventGetError>;
fn resume(&mut self, arg: Option<&[u8]>) -> JmapCoroutineState<Self::Yield, Self::Return> {
match &mut self.state {
State::Get(get) => {
let JmapGetOutput {
list,
not_found,
state,
keep_alive,
} = jmap_try!(get, arg);
JmapCoroutineState::Complete(Ok(JmapCalendarEventGetOutput {
events: list,
not_found,
new_state: state,
keep_alive,
}))
}
}
}
}
enum State {
Get(JmapGet<JmapCalendarEvent>),
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct CalendarEventGetArgs<'a> {
account_id: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
ids: Option<&'a [String]>,
#[serde(skip_serializing_if = "Option::is_none")]
properties: Option<&'a [String]>,
#[serde(skip_serializing_if = "Option::is_none")]
recurrence_overrides_before: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
recurrence_overrides_after: Option<&'a str>,
#[serde(skip_serializing_if = "is_false")]
reduce_participants: bool,
#[serde(skip_serializing_if = "Option::is_none")]
time_zone: Option<&'a str>,
}
fn is_false(b: &bool) -> bool {
!b
}