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 JmapAddressBookChangesError {
#[error("JMAP AddressBook/changes failed: {0}")]
Changes(#[from] JmapChangesError),
}
#[derive(Clone, Debug, Default)]
pub struct JmapAddressBookChangesOptions {
pub max_changes: Option<u64>,
}
pub struct JmapAddressBookChanges {
state: State,
}
impl JmapAddressBookChanges {
pub fn new(
session: &JmapSession,
http_auth: &SecretString,
since_state: impl Into<String>,
opts: JmapAddressBookChangesOptions,
) -> Result<Self, JmapAddressBookChangesError> {
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,
"AddressBook/changes",
vec![JMAP_CORE_CAPABILITY.into(), JMAP_CONTACTS_CAPABILITY.into()],
since_state,
JmapChangesOptions {
max_changes: opts.max_changes,
},
)?),
})
}
}
impl JmapCoroutine for JmapAddressBookChanges {
type Yield = JmapYield;
type Return = Result<JmapChangesOutput, JmapAddressBookChangesError>;
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),
}