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