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, request::JmapBatch, send::*, session::JmapSession, set::*},
rfc9610::{JMAP_CONTACTS_CAPABILITY, contact_card::JmapContactCard},
};
#[derive(Clone, Debug, Default, Serialize)]
#[serde(transparent)]
pub struct JmapContactCardPatch(pub BTreeMap<String, serde_json::Value>);
#[derive(Clone, Debug, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum JmapContactCardSetItemError {
BlobNotFound {
description: Option<String>,
},
Forbidden {
description: Option<String>,
},
NotFound {
description: Option<String>,
},
InvalidPatch {
description: Option<String>,
},
WillDestroy {
description: Option<String>,
},
InvalidProperties {
description: Option<String>,
#[serde(default)]
properties: Vec<String>,
},
#[serde(other)]
Unknown,
}
#[derive(Debug, Error)]
pub enum JmapContactCardSetError {
#[error("JMAP ContactCard/set failed: {0}")]
Send(#[from] JmapSendError),
#[error("JMAP ContactCard/set failed: serialize args: {0}")]
SerializeArgs(#[source] serde_json::Error),
#[error("JMAP ContactCard/set failed: {0}")]
Set(#[from] JmapSetError),
}
#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct JmapContactCardSetArgs {
#[serde(skip_serializing_if = "Option::is_none")]
pub create: Option<BTreeMap<String, JmapContactCard>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub update: Option<BTreeMap<String, JmapContactCardPatch>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub destroy: Option<Vec<String>>,
}
#[derive(Clone, Debug)]
pub struct JmapContactCardSetOutput {
pub new_state: String,
pub created: BTreeMap<String, JmapContactCard>,
pub updated: BTreeMap<String, Option<JmapContactCard>>,
pub destroyed: Vec<String>,
pub not_created: BTreeMap<String, JmapContactCardSetItemError>,
pub not_updated: BTreeMap<String, JmapContactCardSetItemError>,
pub not_destroyed: BTreeMap<String, JmapContactCardSetItemError>,
pub keep_alive: bool,
}
pub struct JmapContactCardSet {
state: State,
}
impl JmapContactCardSet {
pub fn new(
session: &JmapSession,
http_auth: &SecretString,
args: JmapContactCardSetArgs,
) -> Result<Self, JmapContactCardSetError> {
let account_id = session
.primary_accounts
.get(JMAP_CONTACTS_CAPABILITY)
.cloned()
.unwrap_or_default();
let api_url = &session.api_url;
let json_args = serde_json::to_value(ContactCardSetRequest { account_id, args })
.map_err(JmapContactCardSetError::SerializeArgs)?;
let mut batch = JmapBatch::new();
batch.add("ContactCard/set", json_args);
let request = batch.into_request(vec![
JMAP_CORE_CAPABILITY.into(),
JMAP_CONTACTS_CAPABILITY.into(),
]);
let send = JmapSend::new(http_auth, api_url, request)?;
Ok(Self {
state: State::Set(JmapSet::from_send(send)),
})
}
}
impl JmapCoroutine for JmapContactCardSet {
type Yield = JmapYield;
type Return = Result<JmapContactCardSetOutput, JmapContactCardSetError>;
fn resume(&mut self, arg: Option<&[u8]>) -> JmapCoroutineState<Self::Yield, Self::Return> {
match &mut self.state {
State::Set(set) => {
let JmapSetOutput {
new_state,
created,
updated,
destroyed,
not_created,
not_updated,
not_destroyed,
keep_alive,
} = jmap_try!(set, arg);
let parse = |map: BTreeMap<String, serde_json::Value>| {
map.into_iter()
.map(|(k, v)| {
let e = serde_json::from_value(v)
.unwrap_or(JmapContactCardSetItemError::Unknown);
(k, e)
})
.collect()
};
JmapCoroutineState::Complete(Ok(JmapContactCardSetOutput {
new_state,
created,
updated,
destroyed,
not_created: parse(not_created),
not_updated: parse(not_updated),
not_destroyed: parse(not_destroyed),
keep_alive,
}))
}
}
}
}
enum State {
Set(JmapSet<JmapContactCard>),
}
#[derive(Serialize)]
struct ContactCardSetRequest {
#[serde(rename = "accountId")]
account_id: String,
#[serde(flatten)]
args: JmapContactCardSetArgs,
}