use alloc::{string::String, vec};
use secrecy::SecretString;
use thiserror::Error;
use crate::{
coroutine::*,
jmap_try,
rfc8620::{JMAP_CORE_CAPABILITY, changes::*, session::JmapSession},
rfc8621::JMAP_MAIL_CAPABILITY,
};
#[derive(Debug, Error)]
pub enum JmapMailboxChangesError {
#[error("JMAP Mailbox/changes failed: {0}")]
Changes(#[from] JmapChangesError),
}
#[derive(Clone, Debug, Default)]
pub struct JmapMailboxChangesOptions {
pub max_changes: Option<u64>,
}
pub struct JmapMailboxChanges {
state: State,
}
impl JmapMailboxChanges {
pub fn new(
session: &JmapSession,
http_auth: &SecretString,
since_state: impl Into<String>,
opts: JmapMailboxChangesOptions,
) -> Result<Self, JmapMailboxChangesError> {
let account_id = session
.primary_accounts
.get(JMAP_MAIL_CAPABILITY)
.cloned()
.unwrap_or_default();
let api_url = &session.api_url;
Ok(Self {
state: State::Changes(JmapChanges::new(
account_id,
http_auth,
api_url,
"Mailbox/changes",
vec![JMAP_CORE_CAPABILITY.into(), JMAP_MAIL_CAPABILITY.into()],
since_state,
JmapChangesOptions {
max_changes: opts.max_changes,
},
)?),
})
}
}
impl JmapCoroutine for JmapMailboxChanges {
type Yield = JmapYield;
type Return = Result<JmapChangesOutput, JmapMailboxChangesError>;
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),
}