use alloc::{collections::BTreeMap, string::String, vec, vec::Vec};
use secrecy::SecretString;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::{
coroutine::*,
jmap_try,
rfc8620::{
JMAP_CORE_CAPABILITY, error::JmapMethodError, request::JmapBatch, send::*,
session::JmapSession,
},
rfc9610::{JMAP_CONTACTS_CAPABILITY, contact_card::JmapContactCard},
};
#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct JmapContactCardCopyArgs {
pub id: String,
pub address_book_ids: BTreeMap<String, bool>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum JmapContactCardCopyItemError {
AlreadyExists {
description: Option<String>,
},
NotFound {
description: Option<String>,
},
InvalidProperties {
description: Option<String>,
#[serde(default)]
properties: Vec<String>,
},
#[serde(other)]
Unknown,
}
#[derive(Debug, Error)]
pub enum JmapContactCardCopyError {
#[error("JMAP ContactCard/copy failed: missing response in method_responses")]
MissingResponse,
#[error("JMAP ContactCard/copy failed: {0}")]
Send(#[from] JmapSendError),
#[error("JMAP ContactCard/copy failed: serialize args: {0}")]
SerializeArgs(#[source] serde_json::Error),
#[error("JMAP ContactCard/copy failed: parse response: {0}")]
ParseResponse(#[source] serde_json::Error),
#[error("JMAP ContactCard/copy failed: {0}")]
Method(#[from] JmapMethodError),
}
#[derive(Clone, Debug)]
pub struct JmapContactCardCopyOutput {
pub new_state: String,
pub created: BTreeMap<String, JmapContactCard>,
pub not_created: BTreeMap<String, JmapContactCardCopyItemError>,
pub keep_alive: bool,
}
pub struct JmapContactCardCopy {
state: State,
}
impl JmapContactCardCopy {
pub fn new(
session: &JmapSession,
http_auth: &SecretString,
from_account_id: impl Into<String>,
cards: BTreeMap<String, JmapContactCardCopyArgs>,
) -> Result<Self, JmapContactCardCopyError> {
let account_id = session
.primary_accounts
.get(JMAP_CONTACTS_CAPABILITY)
.cloned()
.unwrap_or_default();
let api_url = &session.api_url;
let args = serde_json::to_value(ContactCardCopyArgs {
from_account_id: from_account_id.into(),
account_id,
create: cards,
})
.map_err(JmapContactCardCopyError::SerializeArgs)?;
let mut batch = JmapBatch::new();
batch.add("ContactCard/copy", args);
let request = batch.into_request(vec![
JMAP_CORE_CAPABILITY.into(),
JMAP_CONTACTS_CAPABILITY.into(),
]);
Ok(Self {
state: State::Send(JmapSend::new(http_auth, api_url, request)?),
})
}
}
impl JmapCoroutine for JmapContactCardCopy {
type Yield = JmapYield;
type Return = Result<JmapContactCardCopyOutput, JmapContactCardCopyError>;
fn resume(&mut self, arg: Option<&[u8]>) -> JmapCoroutineState<Self::Yield, Self::Return> {
match &mut self.state {
State::Send(send) => {
let JmapSendOutput {
response,
keep_alive,
} = jmap_try!(send, arg);
let Some((name, args, _)) = response.method_responses.into_iter().next() else {
return JmapCoroutineState::Complete(Err(
JmapContactCardCopyError::MissingResponse,
));
};
if name == "error" {
let err = serde_json::from_value::<JmapMethodError>(args)
.unwrap_or(JmapMethodError::Unknown);
return JmapCoroutineState::Complete(Err(err.into()));
}
match serde_json::from_value::<ContactCardCopyResponse>(args) {
Ok(r) => JmapCoroutineState::Complete(Ok(JmapContactCardCopyOutput {
new_state: r.new_state,
created: r.created,
not_created: r.not_created,
keep_alive,
})),
Err(err) => JmapCoroutineState::Complete(Err(
JmapContactCardCopyError::ParseResponse(err),
)),
}
}
}
}
}
enum State {
Send(JmapSend),
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ContactCardCopyArgs {
from_account_id: String,
account_id: String,
create: BTreeMap<String, JmapContactCardCopyArgs>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct ContactCardCopyResponse {
new_state: String,
#[serde(default)]
created: BTreeMap<String, JmapContactCard>,
#[serde(default)]
not_created: BTreeMap<String, JmapContactCardCopyItemError>,
}