jmap
Manos Pitsidianakis 2019-12-04 19:42:31 +02:00
parent 138c14f730
commit bfa5bab15d
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
11 changed files with 483 additions and 285 deletions

View File

@ -27,7 +27,7 @@ uuid = { version = "0.7.4", features = ["serde", "v4"] }
text_processing = { path = "../text_processing", version = "*", optional= true } text_processing = { path = "../text_processing", version = "*", optional= true }
libc = {version = "0.2.59", features = ["extra_traits",]} libc = {version = "0.2.59", features = ["extra_traits",]}
reqwest = { version ="0.10.0-alpha.2", optional=true, features = ["json", "blocking" ]} reqwest = { version ="0.10.0-alpha.2", optional=true, features = ["json", "blocking" ]}
serde_json = { version = "1.0", optional = true } serde_json = { version = "1.0", optional = true, features = ["raw_value",] }
[features] [features]
default = ["unicode_algorithms", "imap_backend", "maildir_backend", "mbox_backend", "jmap_backend", "vcard"] default = ["unicode_algorithms", "imap_backend", "maildir_backend", "mbox_backend", "jmap_backend", "vcard"]

View File

@ -45,6 +45,15 @@ macro_rules! _impl {
} }
} }
#[macro_export]
macro_rules! _impl_get_mut {
($method:ident, $field:ident : $t:ty) => {
pub fn $method(&mut self) -> &mut $t {
&mut self.$field
}
}
}
pub mod connection; pub mod connection;
use connection::*; use connection::*;

View File

@ -27,6 +27,7 @@ pub struct JmapConnection {
pub client: Arc<Mutex<Client>>, pub client: Arc<Mutex<Client>>,
pub online_status: Arc<Mutex<bool>>, pub online_status: Arc<Mutex<bool>>,
pub server_conf: JmapServerConf, pub server_conf: JmapServerConf,
pub account_id: Arc<Mutex<String>>,
} }
impl JmapConnection { impl JmapConnection {
@ -59,6 +60,7 @@ impl JmapConnection {
client: Arc::new(Mutex::new(client)), client: Arc::new(Mutex::new(client)),
online_status, online_status,
server_conf, server_conf,
account_id: Arc::new(Mutex::new(String::new())),
}) })
} }
} }

View File

@ -23,3 +23,6 @@ use super::*;
mod email; mod email;
pub use email::*; pub use email::*;
mod mailbox;
pub use mailbox::*;

View File

@ -22,7 +22,11 @@
use super::*; use super::*;
use crate::backends::jmap::protocol::*; use crate::backends::jmap::protocol::*;
use crate::backends::jmap::rfc8620::bool_false; use crate::backends::jmap::rfc8620::bool_false;
use serde::de::{Deserialize, Deserializer};
use serde_json::Value;
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap; use std::collections::HashMap;
use std::hash::Hasher;
// 4.1.1. // 4.1.1.
// Metadata // Metadata
@ -122,17 +126,215 @@ use std::collections::HashMap;
// "internal date" in IMAP [RFC3501]./ // "internal date" in IMAP [RFC3501]./
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct EmailObject { pub struct EmailObject {
pub id: Id, #[serde(default)]
pub blob_id: Id, id: Id,
pub thread_id: Id, #[serde(default)]
pub mailbox_ids: HashMap<Id, bool>, mailbox_ids: HashMap<Id, bool>,
pub keywords: HashMap<String, bool>, #[serde(default)]
pub size: u64, size: u64,
pub received_at: String, #[serde(default)]
received_at: String,
#[serde(default)]
to: Vec<EmailAddress>,
#[serde(default)]
bcc: Vec<EmailAddress>,
#[serde(default)]
reply_to: Option<EmailAddress>,
#[serde(default)]
cc: Vec<EmailAddress>,
#[serde(default)]
from: Vec<EmailAddress>,
#[serde(default)]
in_reply_to_email_id: Id,
#[serde(default)]
keywords: Value,
#[serde(default)]
attached_emails: Option<Id>,
#[serde(default)]
attachments: Vec<Value>,
#[serde(default)]
blob_id: String,
#[serde(default)]
has_attachment: bool,
#[serde(default)]
#[serde(deserialize_with = "deserialize_header")]
headers: HashMap<String, String>,
#[serde(default)]
html_body: Vec<HtmlBody>,
#[serde(default)]
preview: Option<String>,
#[serde(default)]
sent_at: String,
#[serde(default)]
subject: String,
#[serde(default)]
text_body: Vec<TextBody>,
#[serde(default)]
thread_id: Id,
} }
impl Object for EmailObject {} #[derive(Deserialize, Serialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
struct Header {
name: String,
value: String,
}
fn deserialize_header<'de, D>(
deserializer: D,
) -> std::result::Result<HashMap<String, String>, D::Error>
where
D: Deserializer<'de>,
{
let v = <Vec<Header>>::deserialize(deserializer)?;
Ok(v.into_iter().map(|t| (t.name, t.value)).collect())
}
#[derive(Deserialize, Serialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
struct EmailAddress {
email: String,
name: Option<String>,
}
impl Into<crate::email::Address> for EmailAddress {
fn into(self) -> crate::email::Address {
let Self { email, mut name } = self;
crate::make_address!((name.take().unwrap_or_default()), email)
}
}
impl std::fmt::Display for EmailAddress {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if self.name.is_some() {
write!(f, "{} <{}>", self.name.as_ref().unwrap(), &self.email)
} else {
write!(f, "{}", &self.email)
}
}
}
impl std::convert::From<EmailObject> for crate::Envelope {
fn from(mut t: EmailObject) -> crate::Envelope {
let mut env = crate::Envelope::new(0);
env.set_date(std::mem::replace(&mut t.sent_at, String::new()).as_bytes());
if let Some(d) = crate::email::parser::date(env.date_as_str().as_bytes()) {
env.set_datetime(d);
}
if let Some(v) = t.headers.get("Message-ID").or(t.headers.get("Message-Id")) {
env.set_message_id(v.as_bytes());
}
if let Some(v) = t.headers.get("In-Reply-To") {
env.set_in_reply_to(v.as_bytes());
env.push_references(v.as_bytes());
}
if let Some(v) = t.headers.get("References") {
let parse_result = crate::email::parser::references(v.as_bytes());
if parse_result.is_done() {
for v in parse_result.to_full_result().unwrap() {
env.push_references(v);
}
}
env.set_references(v.as_bytes());
}
if let Some(v) = t.headers.get("Date") {
env.set_date(v.as_bytes());
if let Some(d) = crate::email::parser::date(v.as_bytes()) {
env.set_datetime(d);
}
}
env.set_has_attachments(t.has_attachment);
env.set_subject(std::mem::replace(&mut t.subject, String::new()).into_bytes());
env.set_from(
std::mem::replace(&mut t.from, Vec::new())
.into_iter()
.map(|addr| addr.into())
.collect::<Vec<crate::email::Address>>(),
);
env.set_to(
std::mem::replace(&mut t.to, Vec::new())
.into_iter()
.map(|addr| addr.into())
.collect::<Vec<crate::email::Address>>(),
);
env.set_cc(
std::mem::replace(&mut t.cc, Vec::new())
.into_iter()
.map(|addr| addr.into())
.collect::<Vec<crate::email::Address>>(),
);
env.set_bcc(
std::mem::replace(&mut t.bcc, Vec::new())
.into_iter()
.map(|addr| addr.into())
.collect::<Vec<crate::email::Address>>(),
);
if env.references.is_some() {
if let Some(pos) = env
.references
.as_ref()
.map(|r| &r.refs)
.unwrap()
.iter()
.position(|r| r == env.message_id())
{
env.references.as_mut().unwrap().refs.remove(pos);
}
}
let mut h = DefaultHasher::new();
h.write(t.id.as_bytes());
env.set_hash(h.finish());
env
}
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
struct HtmlBody {
blob_id: Id,
cid: Option<String>,
disposition: String,
headers: Value,
language: Option<Vec<String>>,
location: Option<String>,
name: Option<String>,
part_id: Option<String>,
size: u64,
#[serde(alias = "type")]
content_type: String,
#[serde(default)]
sub_parts: Vec<Value>,
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
struct TextBody {
blob_id: Id,
cid: Option<String>,
disposition: String,
headers: Value,
language: Option<Vec<String>>,
location: Option<String>,
name: Option<String>,
part_id: Option<String>,
size: u64,
#[serde(alias = "type")]
content_type: String,
#[serde(default)]
sub_parts: Vec<Value>,
}
impl Object for EmailObject {
const NAME: &'static str = "Email";
}
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -166,9 +368,9 @@ impl Method<EmailObject> for EmailQueryCall {
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct EmailGetCall { pub struct EmailGet {
#[serde(flatten)] #[serde(flatten)]
pub get_call: GetCall<EmailObject>, pub get_call: Get<EmailObject>,
#[serde(skip_serializing_if = "Vec::is_empty")] #[serde(skip_serializing_if = "Vec::is_empty")]
pub body_properties: Vec<String>, pub body_properties: Vec<String>,
#[serde(default = "bool_false")] #[serde(default = "bool_false")]
@ -181,13 +383,13 @@ pub struct EmailGetCall {
pub max_body_value_bytes: u64, pub max_body_value_bytes: u64,
} }
impl Method<EmailObject> for EmailGetCall { impl Method<EmailObject> for EmailGet {
const NAME: &'static str = "Email/get"; const NAME: &'static str = "Email/get";
} }
impl EmailGetCall { impl EmailGet {
pub fn new(get_call: GetCall<EmailObject>) -> Self { pub fn new(get_call: Get<EmailObject>) -> Self {
EmailGetCall { EmailGet {
get_call, get_call,
body_properties: Vec::new(), body_properties: Vec::new(),
fetch_text_body_values: false, fetch_text_body_values: false,
@ -197,7 +399,7 @@ impl EmailGetCall {
} }
} }
_impl!(get_call: GetCall<EmailObject>); _impl!(get_call: Get<EmailObject>);
_impl!(body_properties: Vec<String>); _impl!(body_properties: Vec<String>);
_impl!(fetch_text_body_values: bool); _impl!(fetch_text_body_values: bool);
_impl!(fetch_html_body_values: bool); _impl!(fetch_html_body_values: bool);
@ -249,7 +451,7 @@ pub struct EmailFilterCondition {
#[serde(skip_serializing_if = "String::is_empty")] #[serde(skip_serializing_if = "String::is_empty")]
pub body: String, pub body: String,
#[serde(skip_serializing_if = "Vec::is_empty")] #[serde(skip_serializing_if = "Vec::is_empty")]
pub header: Vec<String>, pub header: Vec<Value>,
} }
impl EmailFilterCondition { impl EmailFilterCondition {
@ -272,68 +474,24 @@ impl EmailFilterCondition {
_impl!(bcc: String); _impl!(bcc: String);
_impl!(subject: String); _impl!(subject: String);
_impl!(body: String); _impl!(body: String);
_impl!(header: Vec<String>); _impl!(header: Vec<Value>);
} }
impl FilterTrait<EmailObject> for EmailFilterCondition {} impl FilterTrait<EmailObject> for EmailFilterCondition {}
// The following convenience properties are also specified for the Email #[derive(Deserialize, Serialize, Debug)]
// object: #[serde(rename_all = "camelCase")]
// pub enum MessageProperty {
// o messageId: "String[]|null" (immutable) ThreadId,
// MailboxId,
// The value is identical to the value of "header:Message- IsUnread,
// ID:asMessageIds". For messages conforming to RFC 5322, this will IsFlagged,
// be an array with a single entry. IsAnswered,
// IsDraft,
// o inReplyTo: "String[]|null" (immutable) HasAttachment,
// From,
// The value is identical to the value of "header:In-Reply- To,
// To:asMessageIds". Subject,
// Date,
// o references: "String[]|null" (immutable) Preview,
// }
// The value is identical to the value of
// "header:References:asMessageIds".
//
// o sender: "EmailAddress[]|null" (immutable)
//
// The value is identical to the value of
// "header:Sender:asAddresses".
//
// o from: "EmailAddress[]|null" (immutable)
//
// The value is identical to the value of "header:From:asAddresses".
//
// o to: "EmailAddress[]|null" (immutable)
//
// The value is identical to the value of "header:To:asAddresses".
//
// o cc: "EmailAddress[]|null" (immutable)
//
// The value is identical to the value of "header:Cc:asAddresses".
//
// o bcc: "EmailAddress[]|null" (immutable)
//
// The value is identical to the value of "header:Bcc:asAddresses".
//
// o replyTo: "EmailAddress[]|null" (immutable)
//
// The value is identical to the value of "header:Reply-
// To:asAddresses".
//
// o subject: "String|null" (immutable)
//
// The value is identical to the value of "header:Subject:asText".
//
//
//
// Jenkins & Newman Standards Track [Page 34]
//
// RFC 8621 JMAP Mail August 2019
//
//
// o sentAt: "Date|null" (immutable; default on creation: current
// server time)
//
// The value is identical to the value of "header:Date:asDate".

View File

@ -0,0 +1,66 @@
/*
* meli - jmap module.
*
* Copyright 2019 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see <http://www.gnu.org/licenses/>.
*/
use super::*;
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MailboxObject {
pub id: String,
pub is_subscribed: bool,
pub my_rights: JmapRights,
pub name: String,
pub parent_id: Option<String>,
pub role: Option<String>,
pub sort_order: u64,
pub total_emails: u64,
pub total_threads: u64,
pub unread_emails: u64,
pub unread_threads: u64,
}
impl Object for MailboxObject {
const NAME: &'static str = "Mailbox";
}
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct JmapRights {
pub may_add_items: bool,
pub may_create_child: bool,
pub may_delete: bool,
pub may_read_items: bool,
pub may_remove_items: bool,
pub may_rename: bool,
pub may_set_keywords: bool,
pub may_set_seen: bool,
pub may_submit: bool,
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MailboxGet {
#[serde(flatten)]
pub get_call: Get<MailboxObject>,
}
impl Method<MailboxObject> for MailboxGet {
const NAME: &'static str = "Mailbox/query";
}

View File

@ -23,6 +23,7 @@ use super::folder::JmapFolder;
use super::*; use super::*;
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
use serde_json::{json, Value}; use serde_json::{json, Value};
use std::convert::TryFrom;
pub type Id = String; pub type Id = String;
pub type UtcDate = String; pub type UtcDate = String;
@ -38,26 +39,14 @@ macro_rules! get_request_no {
}}; }};
} }
pub trait Response<OBJ: Object> {
const NAME: &'static str;
}
pub trait Method<OBJ: Object>: Serialize { pub trait Method<OBJ: Object>: Serialize {
const NAME: &'static str; const NAME: &'static str;
} }
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub enum MessageProperty {
ThreadId,
MailboxId,
IsUnread,
IsFlagged,
IsAnswered,
IsDraft,
HasAttachment,
From,
To,
Subject,
Date,
Preview,
}
macro_rules! get_path_hash { macro_rules! get_path_hash {
($path:expr) => {{ ($path:expr) => {{
use std::collections::hash_map::DefaultHasher; use std::collections::hash_map::DefaultHasher;
@ -78,7 +67,7 @@ pub struct Request {
* Trait object because its serialize() will be generic. */ * Trait object because its serialize() will be generic. */
method_calls: Vec<Value>, method_calls: Vec<Value>,
#[serde(skip_serializing)] #[serde(skip)]
request_no: Arc<Mutex<usize>>, request_no: Arc<Mutex<usize>>,
} }
@ -129,19 +118,41 @@ pub fn get_mailboxes(conn: &JmapConnection) -> Result<FnvHashMap<FolderHash, Jma
})) }))
.send(); .send();
let mut v: JsonResponse = let res_text = res?.text()?;
serde_json::from_str(&std::dbg!(res.unwrap().text().unwrap())).unwrap(); let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap();
*conn.online_status.lock().unwrap() = true; *conn.online_status.lock().unwrap() = true;
std::dbg!(&v); let m = GetResponse::<MailboxObject>::try_from(v.method_responses.remove(0))?;
assert_eq!("Mailbox/get", v.method_responses[0].0); let GetResponse::<MailboxObject> {
Ok( list, account_id, ..
if let Response::MailboxGet { list, .. } = v.method_responses.remove(0).1 { } = m;
list.into_iter().map(|r| { *conn.account_id.lock().unwrap() = account_id;
if let MailboxResponse { Ok(list
.into_iter()
.map(|r| {
let MailboxObject {
id,
is_subscribed,
my_rights,
name,
parent_id,
role,
sort_order,
total_emails,
total_threads,
unread_emails,
unread_threads,
} = r;
let hash = get_path_hash!(&name);
(
hash,
JmapFolder {
name: name.clone(),
hash,
path: name,
v: Vec::new(),
id, id,
is_subscribed, is_subscribed,
my_rights, my_rights,
name,
parent_id, parent_id,
role, role,
sort_order, sort_order,
@ -149,132 +160,19 @@ pub fn get_mailboxes(conn: &JmapConnection) -> Result<FnvHashMap<FolderHash, Jma
total_threads, total_threads,
unread_emails, unread_emails,
unread_threads, unread_threads,
} = r },
{ )
let hash = get_path_hash!(&name); })
( .collect())
hash,
JmapFolder {
name: name.clone(),
hash,
path: name,
v: Vec::new(),
id,
is_subscribed,
my_rights,
parent_id,
role,
sort_order,
total_emails,
total_threads,
unread_emails,
unread_threads,
},
)
} else {
panic!()
}
})
} else {
panic!()
}
.collect(),
)
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct JsonResponse { pub struct JsonResponse<'a> {
method_responses: Vec<MethodResponse>, #[serde(borrow)]
method_responses: Vec<MethodResponse<'a>>,
} }
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MethodResponse(String, Response, String);
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
pub enum Response {
#[serde(rename_all = "camelCase")]
MailboxGet {
account_id: String,
list: Vec<MailboxResponse>,
not_found: Vec<String>,
state: String,
},
#[serde(rename_all = "camelCase")]
EmailQuery {
account_id: String,
can_calculate_changes: bool,
collapse_threads: bool,
filter: Value,
ids: Vec<String>,
position: u64,
query_state: String,
sort: Option<String>,
total: usize,
},
Empty {},
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MailboxResponse {
id: String,
is_subscribed: bool,
my_rights: JmapRights,
name: String,
parent_id: Option<String>,
role: Option<String>,
sort_order: u64,
total_emails: u64,
total_threads: u64,
unread_emails: u64,
unread_threads: u64,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct JmapRights {
may_add_items: bool,
may_create_child: bool,
may_delete: bool,
may_read_items: bool,
may_remove_items: bool,
may_rename: bool,
may_set_keywords: bool,
may_set_seen: bool,
may_submit: bool,
}
// [
// [ "getMessageList", {
// filter: {
// inMailboxes: [ "mailbox1" ]
// },
// sort: [ "date desc", "id desc" ]
// collapseThreads: true,
// position: 0,
// limit: 10,
// fetchThreads: true,
// fetchMessages: true,
// fetchMessageProperties: [
// "threadId",
// "mailboxId",
// "isUnread",
// "isFlagged",
// "isAnswered",
// "isDraft",
// "hasAttachment",
// "from",
// "to",
// "subject",
// "date",
// "preview"
// ],
// fetchSearchSnippets: false
// }, "call1"]
// ]
pub fn get_message_list(conn: &JmapConnection, folder: &JmapFolder) -> Result<Vec<String>> { pub fn get_message_list(conn: &JmapConnection, folder: &JmapFolder) -> Result<Vec<String>> {
let seq = get_request_no!(conn.request_no); let seq = get_request_no!(conn.request_no);
let email_call: EmailQueryCall = EmailQueryCall { let email_call: EmailQueryCall = EmailQueryCall {
@ -372,7 +270,6 @@ pub fn get_message_list(conn: &JmapConnection, folder: &JmapFolder) -> Result<Ve
});" });"
);*/ );*/
std::dbg!(serde_json::to_string(&req));
let res = conn let res = conn
.client .client
.lock() .lock()
@ -381,20 +278,21 @@ pub fn get_message_list(conn: &JmapConnection, folder: &JmapFolder) -> Result<Ve
.json(&req) .json(&req)
.send(); .send();
let mut v: JsonResponse = serde_json::from_str(&std::dbg!(res.unwrap().text().unwrap()))?; let res_text = res?.text()?;
let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap();
let result: Response = v.method_responses.remove(0).1; *conn.online_status.lock().unwrap() = true;
if let Response::EmailQuery { ids, .. } = result { let m = QueryResponse::<EmailObject>::try_from(v.method_responses.remove(0))?;
Ok(ids) let QueryResponse::<EmailObject> { ids, .. } = m;
} else { Ok(ids)
Err(MeliError::new(format!("response was {:#?}", &result)))
}
} }
pub fn get_message(conn: &JmapConnection, ids: &[String]) -> Result<Vec<Envelope>> { pub fn get_message(conn: &JmapConnection, ids: &[String]) -> Result<Vec<Envelope>> {
let seq = get_request_no!(conn.request_no); let seq = get_request_no!(conn.request_no);
let email_call: EmailGetCall = let email_call: EmailGet = EmailGet::new(
EmailGetCall::new(GetCall::new().ids(Some(ids.iter().cloned().collect::<Vec<String>>()))); Get::new()
.ids(Some(ids.iter().cloned().collect::<Vec<String>>()))
.account_id(conn.account_id.lock().unwrap().clone()),
);
let mut req = Request::new(conn.request_no.clone()); let mut req = Request::new(conn.request_no.clone());
req.add_call(email_call); req.add_call(email_call);
@ -407,14 +305,13 @@ pub fn get_message(conn: &JmapConnection, ids: &[String]) -> Result<Vec<Envelope
.send(); .send();
let res_text = res?.text()?; let res_text = res?.text()?;
let v: JsonResponse = serde_json::from_str(&res_text)?; let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap();
let mut f = std::fs::File::create(std::dbg!(format!("/tmp/asdfsa{}", seq))).unwrap(); let e = GetResponse::<EmailObject>::try_from(v.method_responses.remove(0))?;
use std::io::Write; let GetResponse::<EmailObject> { list, .. } = e;
f.write_all( Ok(list
serde_json::to_string_pretty(&serde_json::from_str::<Value>(&res_text)?)?.as_bytes(), .into_iter()
) .map(std::convert::Into::into)
.unwrap(); .collect::<Vec<Envelope>>())
Ok(vec![])
} }
/* /*

View File

@ -22,16 +22,18 @@
use super::Id; use super::Id;
use core::marker::PhantomData; use core::marker::PhantomData;
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
use serde_json::Value; use serde_json::{value::RawValue, Value};
mod filters; mod filters;
pub use filters::*; pub use filters::*;
mod comparator; mod comparator;
pub use comparator::*; pub use comparator::*;
use super::protocol::Method; use super::protocol::{Method, Response};
use std::collections::HashMap; use std::collections::HashMap;
pub trait Object {} pub trait Object {
const NAME: &'static str;
}
// 5.1. /get // 5.1. /get
// //
@ -102,7 +104,7 @@ pub struct Account {
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct GetCall<OBJ: Object> pub struct Get<OBJ: Object>
where where
OBJ: std::fmt::Debug + Serialize, OBJ: std::fmt::Debug + Serialize,
{ {
@ -112,10 +114,11 @@ where
pub ids: Option<Vec<String>>, pub ids: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub properties: Option<Vec<String>>, pub properties: Option<Vec<String>>,
#[serde(skip)]
_ph: PhantomData<*const OBJ>, _ph: PhantomData<*const OBJ>,
} }
impl<OBJ: Object> GetCall<OBJ> impl<OBJ: Object> Get<OBJ>
where where
OBJ: std::fmt::Debug + Serialize, OBJ: std::fmt::Debug + Serialize,
{ {
@ -173,14 +176,41 @@ where
// the maximum number the server is willing to process in a single // the maximum number the server is willing to process in a single
// method call. // method call.
#[derive(Serialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct GetResponse<T> { pub struct MethodResponse<'a> {
#[serde(borrow)]
pub method_responses: Vec<&'a RawValue>,
#[serde(default)]
pub created_ids: HashMap<Id, Id>,
#[serde(default)]
pub session_state: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GetResponse<OBJ: Object> {
#[serde(skip_serializing_if = "String::is_empty")] #[serde(skip_serializing_if = "String::is_empty")]
account_id: String, pub account_id: String,
state: String, pub state: String,
list: Vec<T>, pub list: Vec<OBJ>,
not_found: Vec<String>, pub not_found: Vec<String>,
}
impl<OBJ: Object + DeserializeOwned> std::convert::TryFrom<&RawValue> for GetResponse<OBJ> {
type Error = crate::error::MeliError;
fn try_from(t: &RawValue) -> Result<GetResponse<OBJ>, crate::error::MeliError> {
let res: (String, GetResponse<OBJ>, String) = serde_json::from_str(t.get())?;
assert_eq!(&res.0, &format!("{}/get", OBJ::NAME));
Ok(res.1)
}
}
impl<OBJ: Object> GetResponse<OBJ> {
_impl_get_mut!(account_id_mut, account_id: String);
_impl_get_mut!(state_mut, state: String);
_impl_get_mut!(list_mut, list: Vec<OBJ>);
_impl_get_mut!(not_found_mut, not_found: Vec<String>);
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
@ -379,6 +409,7 @@ where
limit: Option<u64>, limit: Option<u64>,
#[serde(default = "bool_false")] #[serde(default = "bool_false")]
calculate_total: bool, calculate_total: bool,
#[serde(skip)]
_ph: PhantomData<*const OBJ>, _ph: PhantomData<*const OBJ>,
} }
@ -495,3 +526,33 @@ pub fn bool_true() -> bool {
// server cannot process it. If the filter was the result of a user's // server cannot process it. If the filter was the result of a user's
// search input, the client SHOULD suggest that the user simplify their // search input, the client SHOULD suggest that the user simplify their
// search. // search.
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct QueryResponse<OBJ: Object> {
#[serde(skip_serializing_if = "String::is_empty", default)]
pub account_id: String,
pub query_state: String,
pub can_calculate_changes: bool,
pub position: u64,
pub ids: Vec<Id>,
#[serde(default)]
pub total: u64,
#[serde(default)]
pub limit: u64,
#[serde(skip)]
_ph: PhantomData<*const OBJ>,
}
impl<OBJ: Object + DeserializeOwned> std::convert::TryFrom<&RawValue> for QueryResponse<OBJ> {
type Error = crate::error::MeliError;
fn try_from(t: &RawValue) -> Result<QueryResponse<OBJ>, crate::error::MeliError> {
let res: (String, QueryResponse<OBJ>, String) = serde_json::from_str(t.get())?;
assert_eq!(&res.0, &format!("{}/query", OBJ::NAME));
Ok(res.1)
}
}
impl<OBJ: Object> QueryResponse<OBJ> {
_impl_get_mut!(ids_mut, ids: Vec<Id>);
}

View File

@ -125,7 +125,7 @@ pub struct Envelope {
subject: Option<Vec<u8>>, subject: Option<Vec<u8>>,
message_id: MessageID, message_id: MessageID,
in_reply_to: Option<MessageID>, in_reply_to: Option<MessageID>,
references: Option<References>, pub references: Option<References>,
other_headers: FnvHashMap<String, String>, other_headers: FnvHashMap<String, String>,
timestamp: UnixTimestamp, timestamp: UnixTimestamp,
@ -542,6 +542,7 @@ impl Envelope {
None => Vec::new(), None => Vec::new(),
} }
} }
pub fn other_headers(&self) -> &FnvHashMap<String, String> { pub fn other_headers(&self) -> &FnvHashMap<String, String> {
&self.other_headers &self.other_headers
} }

View File

@ -256,3 +256,34 @@ impl fmt::Debug for References {
write!(f, "{:#?}", self.refs) write!(f, "{:#?}", self.refs)
} }
} }
#[macro_export]
macro_rules! make_address {
($d:expr, $a:expr) => {
Address::Mailbox(if $d.is_empty() {
MailboxAddress {
raw: format!("{}", $a).into_bytes(),
display_name: StrBuilder {
offset: 0,
length: 0,
},
address_spec: StrBuilder {
offset: 0,
length: $a.len(),
},
}
} else {
MailboxAddress {
raw: format!("{} <{}>", $d, $a).into_bytes(),
display_name: StrBuilder {
offset: 0,
length: $d.len(),
},
address_spec: StrBuilder {
offset: $d.len() + 2,
length: $a.len(),
},
}
})
};
}

View File

@ -1083,36 +1083,6 @@ mod tests {
); );
} }
macro_rules! make_address {
($d:literal, $a:literal) => {
Address::Mailbox(if $d.is_empty() {
MailboxAddress {
raw: format!("<{}>", $a).into_bytes(),
display_name: StrBuilder {
offset: 0,
length: 0,
},
address_spec: StrBuilder {
offset: 1,
length: $a.len(),
},
}
} else {
MailboxAddress {
raw: format!("{} <{}>", $d, $a).into_bytes(),
display_name: StrBuilder {
offset: 0,
length: $d.len(),
},
address_spec: StrBuilder {
offset: $d.len() + 2,
length: $a.len(),
},
}
})
};
}
#[test] #[test]
fn test_address_list() { fn test_address_list() {
let s = b"Obit Oppidum <user@domain>, let s = b"Obit Oppidum <user@domain>,