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},
rfc8621::{
JMAP_MAIL_CAPABILITY, email_submission::JMAP_SUBMISSION_CAPABILITY, identity::JmapIdentity,
},
};
#[derive(Debug, Error)]
pub enum JmapIdentityGetError {
#[error("JMAP Identity/get failed: {0}")]
Get(#[from] JmapGetError),
}
#[derive(Clone, Debug, Default)]
pub struct JmapIdentityGetOptions {
pub ids: Option<Vec<String>>,
}
#[derive(Clone, Debug)]
pub struct JmapIdentityGetOutput {
pub identities: Vec<JmapIdentity>,
pub not_found: Vec<String>,
pub new_state: String,
pub keep_alive: bool,
}
pub struct JmapIdentityGet {
state: State,
}
impl JmapIdentityGet {
pub fn new(
session: &JmapSession,
http_auth: &SecretString,
opts: JmapIdentityGetOptions,
) -> Result<Self, JmapIdentityGetError> {
let account_id = session
.primary_accounts
.get(JMAP_MAIL_CAPABILITY)
.cloned()
.unwrap_or_default();
let api_url = &session.api_url;
Ok(Self {
state: State::Get(JmapGet::new(
account_id,
http_auth,
api_url,
"Identity/get",
vec![
JMAP_CORE_CAPABILITY.into(),
JMAP_MAIL_CAPABILITY.into(),
JMAP_SUBMISSION_CAPABILITY.into(),
],
JmapGetOptions {
ids: opts.ids,
properties: None,
},
)?),
})
}
}
impl JmapCoroutine for JmapIdentityGet {
type Yield = JmapYield;
type Return = Result<JmapIdentityGetOutput, JmapIdentityGetError>;
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(JmapIdentityGetOutput {
identities: list,
not_found,
new_state: state,
keep_alive,
}))
}
}
}
}
enum State {
Get(JmapGet<JmapIdentity>),
}