adress dig's comments

- description in cargo.toml
- impl From<EventType> for EventTypeName
- rename `CommandApi::new_from_arc` -> `CommandApi::from_arc`
- pre-allocate if we know the entry count already
- remove unused enumerate
- remove unused serde attribute comment
- rename `FullChat::from_dc_chat_id` -> `FullChat::try_from_dc_chat_id`
This commit is contained in:
Simon Laux
2022-06-30 13:59:20 +02:00
parent aaf27e4434
commit 40fa2d4120
6 changed files with 46 additions and 42 deletions

View File

@@ -4460,7 +4460,7 @@ mod jsonrpc {
} }
let cmd_api = let cmd_api =
deltachat_jsonrpc::api::CommandApi::new_from_arc((*account_manager).inner.clone()); deltachat_jsonrpc::api::CommandApi::from_arc((*account_manager).inner.clone());
let (request_handle, receiver) = RpcClient::new(); let (request_handle, receiver) = RpcClient::new();
let handle = RpcSession::new(request_handle, cmd_api); let handle = RpcSession::new(request_handle, cmd_api);

View File

@@ -1,6 +1,7 @@
[package] [package]
name = "deltachat-jsonrpc" name = "deltachat-jsonrpc"
version = "1.86.0" version = "1.86.0"
description = "DeltaChat JSON-RPC API"
authors = ["Delta Chat Developers (ML) <delta@codespeak.net>"] authors = ["Delta Chat Developers (ML) <delta@codespeak.net>"]
edition = "2021" edition = "2021"
default-run = "deltachat-jsonrpc-server" default-run = "deltachat-jsonrpc-server"

View File

@@ -62,8 +62,9 @@ pub fn event_to_json_rpc_notification(event: Event) -> Value {
} => (json!(msg_id), json!(status_update_serial)), } => (json!(msg_id), json!(status_update_serial)),
}; };
let id: EventTypeName = event.typ.into();
json!({ json!({
"id": event_type_to_string(event.typ), "id": id,
"contextId": event.id, "contextId": event.id,
"field1": field1, "field1": field1,
"field2": field2 "field2": field2
@@ -103,7 +104,8 @@ pub enum EventTypeName {
WebxdcStatusUpdate, WebxdcStatusUpdate,
} }
fn event_type_to_string(event: EventType) -> EventTypeName { impl From<EventType> for EventTypeName {
fn from(event: EventType) -> Self {
use EventTypeName::*; use EventTypeName::*;
match event { match event {
EventType::Info(_) => Info, EventType::Info(_) => Info,
@@ -137,6 +139,7 @@ fn event_type_to_string(event: EventType) -> EventTypeName {
EventType::WebxdcStatusUpdate { .. } => WebxdcStatusUpdate, EventType::WebxdcStatusUpdate { .. } => WebxdcStatusUpdate,
} }
} }
}
#[cfg(test)] #[cfg(test)]
#[test] #[test]

View File

@@ -41,7 +41,7 @@ impl CommandApi {
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn new_from_arc(accounts: Arc<RwLock<Accounts>>) -> Self { pub fn from_arc(accounts: Arc<RwLock<Accounts>>) -> Self {
CommandApi { accounts } CommandApi { accounts }
} }
@@ -258,7 +258,7 @@ impl CommandApi {
query_contact_id.map(ContactId::new), query_contact_id.map(ContactId::new),
) )
.await?; .await?;
let mut l: Vec<ChatListEntry> = Vec::new(); let mut l: Vec<ChatListEntry> = Vec::with_capacity(list.len());
for i in 0..list.len() { for i in 0..list.len() {
l.push(ChatListEntry( l.push(ChatListEntry(
list.get_chat_id(i)?.to_u32(), list.get_chat_id(i)?.to_u32(),
@@ -275,8 +275,9 @@ impl CommandApi {
) -> Result<HashMap<u32, ChatListItemFetchResult>> { ) -> Result<HashMap<u32, ChatListItemFetchResult>> {
// todo custom json deserializer for ChatListEntry? // todo custom json deserializer for ChatListEntry?
let ctx = self.get_context(account_id).await?; let ctx = self.get_context(account_id).await?;
let mut result: HashMap<u32, ChatListItemFetchResult> = HashMap::new(); let mut result: HashMap<u32, ChatListItemFetchResult> =
for (_i, entry) in entries.iter().enumerate() { HashMap::with_capacity(entries.len());
for entry in entries.iter() {
result.insert( result.insert(
entry.0, entry.0,
match get_chat_list_item_by_id(&ctx, entry).await { match get_chat_list_item_by_id(&ctx, entry).await {
@@ -301,7 +302,7 @@ impl CommandApi {
chat_id: u32, chat_id: u32,
) -> Result<FullChat> { ) -> Result<FullChat> {
let ctx = self.get_context(account_id).await?; let ctx = self.get_context(account_id).await?;
FullChat::from_dc_chat_id(&ctx, chat_id).await FullChat::try_from_dc_chat_id(&ctx, chat_id).await
} }
async fn accept_chat(&self, account_id: u32, chat_id: u32) -> Result<()> { async fn accept_chat(&self, account_id: u32, chat_id: u32) -> Result<()> {

View File

@@ -9,7 +9,6 @@ use super::color_int_to_hex_string;
#[derive(Serialize, TypeDef)] #[derive(Serialize, TypeDef)]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum Account { pub enum Account {
//#[serde(rename_all = "camelCase")]
Configured { Configured {
id: u32, id: u32,
display_name: Option<String>, display_name: Option<String>,

View File

@@ -35,13 +35,13 @@ pub struct FullChat {
} }
impl FullChat { impl FullChat {
pub async fn from_dc_chat_id(context: &Context, chat_id: u32) -> Result<Self> { pub async fn try_from_dc_chat_id(context: &Context, chat_id: u32) -> Result<Self> {
let rust_chat_id = ChatId::new(chat_id); let rust_chat_id = ChatId::new(chat_id);
let chat = Chat::load_from_db(context, rust_chat_id).await?; let chat = Chat::load_from_db(context, rust_chat_id).await?;
let contact_ids = get_chat_contacts(context, rust_chat_id).await?; let contact_ids = get_chat_contacts(context, rust_chat_id).await?;
let mut contacts = Vec::new(); let mut contacts = Vec::with_capacity(contact_ids.len());
for contact_id in &contact_ids { for contact_id in &contact_ids {
contacts.push( contacts.push(