use alloc::{string::String, vec, vec::Vec};
use secrecy::SecretString;
use thiserror::Error;
use crate::{
coroutine::*,
jmap_try,
rfc8620::{JMAP_CORE_CAPABILITY, get::*, session::JmapSession},
rfc9610::{JMAP_CONTACTS_CAPABILITY, contact_card::JmapContactCard},
};
#[derive(Debug, Error)]
pub enum JmapContactCardGetError {
#[error("JMAP ContactCard/get failed: {0}")]
Get(#[from] JmapGetError),
}
#[derive(Clone, Debug, Default)]
pub struct JmapContactCardGetOptions {
pub ids: Option<Vec<String>>,
pub properties: Option<Vec<String>>,
}
#[derive(Clone, Debug)]
pub struct JmapContactCardGetOutput {
pub cards: Vec<JmapContactCard>,
pub not_found: Vec<String>,
pub new_state: String,
pub keep_alive: bool,
}
pub struct JmapContactCardGet {
state: State,
}
impl JmapContactCardGet {
pub fn new(
session: &JmapSession,
http_auth: &SecretString,
opts: JmapContactCardGetOptions,
) -> Result<Self, JmapContactCardGetError> {
let account_id = session
.primary_accounts
.get(JMAP_CONTACTS_CAPABILITY)
.cloned()
.unwrap_or_default();
let api_url = &session.api_url;
Ok(Self {
state: State::Get(JmapGet::new(
account_id,
http_auth,
api_url,
"ContactCard/get",
vec![JMAP_CORE_CAPABILITY.into(), JMAP_CONTACTS_CAPABILITY.into()],
JmapGetOptions {
ids: opts.ids,
properties: opts.properties,
},
)?),
})
}
}
impl JmapCoroutine for JmapContactCardGet {
type Yield = JmapYield;
type Return = Result<JmapContactCardGetOutput, JmapContactCardGetError>;
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(JmapContactCardGetOutput {
cards: list,
not_found,
new_state: state,
keep_alive,
}))
}
}
}
}
enum State {
Get(JmapGet<JmapContactCard>),
}