use alloc::{string::String, vec};
use secrecy::SecretString;
use thiserror::Error;
use crate::{
calendars::JMAP_CALENDARS_CAPABILITY,
coroutine::*,
jmap_try,
rfc8620::{JMAP_CORE_CAPABILITY, changes::*, session::JmapSession},
};
#[derive(Debug, Error)]
pub enum JmapCalendarEventChangesError {
#[error("JMAP CalendarEvent/changes failed: {0}")]
Changes(#[from] JmapChangesError),
}
#[derive(Clone, Debug, Default)]
pub struct JmapCalendarEventChangesOptions {
pub max_changes: Option<u64>,
}
pub struct JmapCalendarEventChanges {
state: State,
}
impl JmapCalendarEventChanges {
pub fn new(
session: &JmapSession,
http_auth: &SecretString,
since_state: impl Into<String>,
opts: JmapCalendarEventChangesOptions,
) -> Result<Self, JmapCalendarEventChangesError> {
let account_id = session
.primary_accounts
.get(JMAP_CALENDARS_CAPABILITY)
.cloned()
.unwrap_or_default();
let api_url = &session.api_url;
Ok(Self {
state: State::Changes(JmapChanges::new(
account_id,
http_auth,
api_url,
"CalendarEvent/changes",
vec![
JMAP_CORE_CAPABILITY.into(),
JMAP_CALENDARS_CAPABILITY.into(),
],
since_state,
JmapChangesOptions {
max_changes: opts.max_changes,
},
)?),
})
}
}
impl JmapCoroutine for JmapCalendarEventChanges {
type Yield = JmapYield;
type Return = Result<JmapChangesOutput, JmapCalendarEventChangesError>;
fn resume(&mut self, arg: Option<&[u8]>) -> JmapCoroutineState<Self::Yield, Self::Return> {
match &mut self.state {
State::Changes(changes) => {
let out = jmap_try!(changes, arg);
JmapCoroutineState::Complete(Ok(out))
}
}
}
}
enum State {
Changes(JmapChanges),
}