Skip to content

Commit 5b435d1

Browse files
Frandolink2xt
authored andcommitted
feat(jsonrpc): generate OpenRPC definitions
When running `cargo test` in the deltachat-jsonrpc folder, a new file `openrpc/openrpc.json` will be created with an [OpenRPC](https://spec.open-rpc.org/) definition. It can be copy-pasted into the [OpenRPC playground](https://playground.open-rpc.org/) and used to generate clients in other languages.
1 parent b9b0d20 commit 5b435d1

17 files changed

+88
-38
lines changed

Cargo.lock

+47-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deltachat-ffi/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ anyhow = "1"
2525
thiserror = "1"
2626
rand = "0.8"
2727
once_cell = "1.17.0"
28-
yerpc = { version = "0.4.4", features = ["anyhow_expose"] }
28+
yerpc = { version = "0.5.1", features = ["anyhow_expose"] }
2929

3030
[features]
3131
default = ["vendored"]

deltachat-jsonrpc/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ required-features = ["webserver"]
1515
anyhow = "1"
1616
deltachat = { path = ".." }
1717
num-traits = "0.2"
18+
schemars = "0.8.11"
1819
serde = { version = "1.0", features = ["derive"] }
1920
tempfile = "3.3.0"
2021
log = "0.4"
2122
async-channel = { version = "1.8.0" }
2223
futures = { version = "0.3.28" }
2324
serde_json = "1.0.96"
24-
yerpc = { version = "0.4.4", features = ["anyhow_expose"] }
25+
yerpc = { version = "0.5.1", features = ["anyhow_expose", "openrpc"] }
2526
typescript-type-def = { version = "0.5.5", features = ["json_value"] }
2627
tokio = { version = "1.28.0" }
2728
sanitize-filename = "0.4"

deltachat-jsonrpc/src/api/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ impl CommandApi {
144144
}
145145
}
146146

147-
#[rpc(all_positional, ts_outdir = "typescript/generated")]
147+
#[rpc(
148+
all_positional,
149+
ts_outdir = "typescript/generated",
150+
openrpc_outdir = "openrpc"
151+
)]
148152
impl CommandApi {
149153
/// Test function.
150154
async fn sleep(&self, delay: f64) {

deltachat-jsonrpc/src/api/types/account.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use typescript_type_def::TypeDef;
66

77
use super::color_int_to_hex_string;
88

9-
#[derive(Serialize, TypeDef)]
9+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
1010
#[serde(tag = "type")]
1111
pub enum Account {
1212
#[serde(rename_all = "camelCase")]

deltachat-jsonrpc/src/api/types/chat.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use typescript_type_def::TypeDef;
1313
use super::color_int_to_hex_string;
1414
use super::contact::ContactObject;
1515

16-
#[derive(Serialize, TypeDef)]
16+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
1717
#[serde(rename_all = "camelCase")]
1818
pub struct FullChat {
1919
id: u32,
@@ -121,7 +121,7 @@ impl FullChat {
121121
/// - can_send
122122
///
123123
/// used when you only need the basic metadata of a chat like type, name, profile picture
124-
#[derive(Serialize, TypeDef)]
124+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
125125
#[serde(rename_all = "camelCase")]
126126
pub struct BasicChat {
127127
id: u32,
@@ -166,7 +166,7 @@ impl BasicChat {
166166
}
167167
}
168168

169-
#[derive(Clone, Serialize, Deserialize, TypeDef)]
169+
#[derive(Clone, Serialize, Deserialize, TypeDef, schemars::JsonSchema)]
170170
pub enum MuteDuration {
171171
NotMuted,
172172
Forever,
@@ -191,7 +191,7 @@ impl MuteDuration {
191191
}
192192
}
193193

194-
#[derive(Clone, Serialize, Deserialize, TypeDef)]
194+
#[derive(Clone, Serialize, Deserialize, TypeDef, schemars::JsonSchema)]
195195
#[serde(rename = "ChatVisibility")]
196196
pub enum JSONRPCChatVisibility {
197197
Normal,

deltachat-jsonrpc/src/api/types/chat_list.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ use deltachat::{
88
chatlist::Chatlist,
99
};
1010
use num_traits::cast::ToPrimitive;
11-
use serde::Serialize;
11+
use serde::{Deserialize, Serialize};
1212
use typescript_type_def::TypeDef;
1313

1414
use super::color_int_to_hex_string;
1515
use super::message::MessageViewtype;
1616

17-
#[derive(Serialize, TypeDef)]
17+
#[derive(Deserialize, Serialize, TypeDef, schemars::JsonSchema)]
18+
pub struct ChatListEntry(pub u32, pub u32);
19+
20+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
1821
#[serde(tag = "type")]
1922
pub enum ChatListItemFetchResult {
2023
#[serde(rename_all = "camelCase")]

deltachat-jsonrpc/src/api/types/contact.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use typescript_type_def::TypeDef;
66

77
use super::color_int_to_hex_string;
88

9-
#[derive(Serialize, TypeDef)]
9+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
1010
#[serde(rename = "Contact", rename_all = "camelCase")]
1111
pub struct ContactObject {
1212
address: String,

deltachat-jsonrpc/src/api/types/events.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use deltachat::{Event as CoreEvent, EventType as CoreEventType};
22
use serde::Serialize;
33
use typescript_type_def::TypeDef;
44

5-
#[derive(Serialize, TypeDef)]
5+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
66
#[serde(rename_all = "camelCase")]
77
pub struct Event {
88
/// Event payload.
@@ -21,7 +21,7 @@ impl From<CoreEvent> for Event {
2121
}
2222
}
2323

24-
#[derive(Serialize, TypeDef)]
24+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
2525
#[serde(tag = "type")]
2626
pub enum EventType {
2727
/// The library-user may write an informational string to the log.

deltachat-jsonrpc/src/api/types/http.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use deltachat::net::HttpResponse as CoreHttpResponse;
22
use serde::Serialize;
33
use typescript_type_def::TypeDef;
44

5-
#[derive(Serialize, TypeDef)]
5+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
66
pub struct HttpResponse {
77
/// base64-encoded response body.
88
blob: String,

deltachat-jsonrpc/src/api/types/location.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use deltachat::location::Location;
22
use serde::Serialize;
33
use typescript_type_def::TypeDef;
44

5-
#[derive(Serialize, TypeDef)]
5+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
66
#[serde(rename = "Location", rename_all = "camelCase")]
77
pub struct JsonrpcLocation {
88
pub location_id: u32,

deltachat-jsonrpc/src/api/types/message.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,22 @@ use deltachat::message::MsgId;
1010
use deltachat::message::Viewtype;
1111
use deltachat::reaction::get_msg_reactions;
1212
use num_traits::cast::ToPrimitive;
13-
use serde::Deserialize;
14-
use serde::Serialize;
13+
use serde::{Deserialize, Serialize};
1514
use typescript_type_def::TypeDef;
1615

1716
use super::color_int_to_hex_string;
1817
use super::contact::ContactObject;
1918
use super::reactions::JSONRPCReactions;
2019
use super::webxdc::WebxdcMessageInfo;
2120

22-
#[derive(Serialize, TypeDef)]
21+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
2322
#[serde(rename_all = "camelCase", tag = "variant")]
2423
pub enum MessageLoadResult {
2524
Message(MessageObject),
2625
LoadingError { error: String },
2726
}
2827

29-
#[derive(Serialize, TypeDef)]
28+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
3029
#[serde(rename = "Message", rename_all = "camelCase")]
3130
pub struct MessageObject {
3231
id: u32,
@@ -86,7 +85,7 @@ pub struct MessageObject {
8685
reactions: Option<JSONRPCReactions>,
8786
}
8887

89-
#[derive(Serialize, TypeDef)]
88+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
9089
#[serde(tag = "kind")]
9190
enum MessageQuote {
9291
JustText {
@@ -238,7 +237,7 @@ impl MessageObject {
238237
}
239238
}
240239

241-
#[derive(Serialize, Deserialize, TypeDef)]
240+
#[derive(Serialize, Deserialize, TypeDef, schemars::JsonSchema)]
242241
#[serde(rename = "Viewtype")]
243242
pub enum MessageViewtype {
244243
Unknown,
@@ -314,7 +313,7 @@ impl From<MessageViewtype> for Viewtype {
314313
}
315314
}
316315

317-
#[derive(Serialize, TypeDef)]
316+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
318317
pub enum DownloadState {
319318
Done,
320319
Available,
@@ -333,7 +332,7 @@ impl From<download::DownloadState> for DownloadState {
333332
}
334333
}
335334

336-
#[derive(Serialize, TypeDef)]
335+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
337336
pub enum SystemMessageType {
338337
Unknown,
339338
GroupNameChanged,
@@ -388,7 +387,7 @@ impl From<deltachat::mimeparser::SystemMessage> for SystemMessageType {
388387
}
389388
}
390389

391-
#[derive(Serialize, TypeDef)]
390+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
392391
#[serde(rename_all = "camelCase")]
393392
pub struct MessageNotificationInfo {
394393
id: u32,
@@ -446,7 +445,7 @@ impl MessageNotificationInfo {
446445
}
447446
}
448447

449-
#[derive(Serialize, TypeDef)]
448+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
450449
#[serde(rename_all = "camelCase")]
451450
pub struct MessageSearchResult {
452451
id: u32,
@@ -507,7 +506,7 @@ impl MessageSearchResult {
507506
}
508507
}
509508

510-
#[derive(Serialize, TypeDef)]
509+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
511510
#[serde(rename_all = "camelCase", rename = "MessageListItem", tag = "kind")]
512511
pub enum JSONRPCMessageListItem {
513512
Message {
@@ -533,7 +532,7 @@ impl From<ChatItem> for JSONRPCMessageListItem {
533532
}
534533
}
535534

536-
#[derive(Deserialize, TypeDef)]
535+
#[derive(Deserialize, Serialize, TypeDef, schemars::JsonSchema)]
537536
#[serde(rename_all = "camelCase")]
538537
pub struct MessageData {
539538
pub text: Option<String>,
@@ -545,7 +544,7 @@ pub struct MessageData {
545544
pub quoted_message_id: Option<u32>,
546545
}
547546

548-
#[derive(Serialize, TypeDef)]
547+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
549548
#[serde(rename_all = "camelCase")]
550549
pub struct MessageReadReceipt {
551550
pub contact_id: u32,

deltachat-jsonrpc/src/api/types/provider_info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use num_traits::cast::ToPrimitive;
33
use serde::Serialize;
44
use typescript_type_def::TypeDef;
55

6-
#[derive(Serialize, TypeDef)]
6+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
77
#[serde(rename_all = "camelCase")]
88
pub struct ProviderInfo {
99
pub before_login_hint: String,

deltachat-jsonrpc/src/api/types/qr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use deltachat::qr::Qr;
22
use serde::Serialize;
33
use typescript_type_def::TypeDef;
44

5-
#[derive(Serialize, TypeDef)]
5+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
66
#[serde(rename = "Qr", rename_all = "camelCase")]
77
#[serde(tag = "type")]
88
pub enum QrObject {

deltachat-jsonrpc/src/api/types/reactions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::Serialize;
66
use typescript_type_def::TypeDef;
77

88
/// A single reaction emoji.
9-
#[derive(Serialize, TypeDef)]
9+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
1010
#[serde(rename = "Reaction", rename_all = "camelCase")]
1111
pub struct JSONRPCReaction {
1212
/// Emoji.
@@ -20,7 +20,7 @@ pub struct JSONRPCReaction {
2020
}
2121

2222
/// Structure representing all reactions to a particular message.
23-
#[derive(Serialize, TypeDef)]
23+
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
2424
#[serde(rename = "Reactions", rename_all = "camelCase")]
2525
pub struct JSONRPCReactions {
2626
/// Map from a contact to it's reaction to message.

0 commit comments

Comments
 (0)