use alloc::{collections::BTreeMap, format, string::String, vec::Vec};
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct JmapResultReference<'a> {
pub result_of: &'a str,
pub name: &'static str,
pub path: &'static str,
}
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct JmapRequest {
pub using: Vec<String>,
pub method_calls: Vec<(String, Value, String)>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created_ids: Option<BTreeMap<String, String>>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct JmapResponse {
pub method_responses: Vec<(String, Value, String)>,
#[serde(default)]
pub created_ids: Option<BTreeMap<String, String>>,
pub session_state: String,
}
#[derive(Debug, Default)]
pub struct JmapBatch {
calls: Vec<(String, Value, String)>,
counter: usize,
}
impl JmapBatch {
pub fn new() -> Self {
Self::default()
}
pub fn add(&mut self, method: impl Into<String>, args: Value) -> String {
let call_id = format!("c{}", self.counter);
self.counter += 1;
self.calls.push((method.into(), args, call_id.clone()));
call_id
}
pub fn into_request(self, using: Vec<String>) -> JmapRequest {
JmapRequest {
using,
method_calls: self.calls,
created_ids: None,
}
}
}