Skip to content

Commit 454d097

Browse files
committed
api!(deltachat-jsonrpc): use kind as a tag for all union types
1 parent 954067e commit 454d097

File tree

16 files changed

+43
-43
lines changed

16 files changed

+43
-43
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use typescript_type_def::TypeDef;
77
use super::color_int_to_hex_string;
88

99
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
10-
#[serde(tag = "type")]
10+
#[serde(tag = "kind")]
1111
pub enum Account {
1212
#[serde(rename_all = "camelCase")]
1313
Configured {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,24 +167,25 @@ impl BasicChat {
167167
}
168168

169169
#[derive(Clone, Serialize, Deserialize, TypeDef, schemars::JsonSchema)]
170+
#[serde(tag = "kind")]
170171
pub enum MuteDuration {
171172
NotMuted,
172173
Forever,
173-
Until(i64),
174+
Until { duration: i64 },
174175
}
175176

176177
impl MuteDuration {
177178
pub fn try_into_core_type(self) -> Result<chat::MuteDuration> {
178179
match self {
179180
MuteDuration::NotMuted => Ok(chat::MuteDuration::NotMuted),
180181
MuteDuration::Forever => Ok(chat::MuteDuration::Forever),
181-
MuteDuration::Until(n) => {
182-
if n <= 0 {
182+
MuteDuration::Until { duration } => {
183+
if duration <= 0 {
183184
bail!("failed to read mute duration")
184185
}
185186

186187
Ok(SystemTime::now()
187-
.checked_add(Duration::from_secs(n as u64))
188+
.checked_add(Duration::from_secs(duration as u64))
188189
.map_or(chat::MuteDuration::Forever, chat::MuteDuration::Until))
189190
}
190191
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use super::message::MessageViewtype;
1818
pub struct ChatListEntry(pub u32, pub u32);
1919

2020
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
21-
#[serde(tag = "type")]
21+
#[serde(tag = "kind")]
2222
pub enum ChatListItemFetchResult {
2323
#[serde(rename_all = "camelCase")]
2424
ChatListItem {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl From<CoreEvent> for Event {
2222
}
2323

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use super::reactions::JSONRPCReactions;
1919
use super::webxdc::WebxdcMessageInfo;
2020

2121
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
22-
#[serde(rename_all = "camelCase", tag = "variant")]
22+
#[serde(rename_all = "camelCase", tag = "kind")]
2323
pub enum MessageLoadResult {
2424
Message(MessageObject),
2525
LoadingError { error: String },

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use typescript_type_def::TypeDef;
44

55
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
66
#[serde(rename = "Qr", rename_all = "camelCase")]
7-
#[serde(tag = "type")]
7+
#[serde(tag = "kind")]
88
pub enum QrObject {
99
AskVerifyContact {
1010
contact_id: u32,

deltachat-jsonrpc/typescript/example/example.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async function run() {
3535
const accounts = await client.rpc.getAllAccounts();
3636
console.log("accounts loaded", accounts);
3737
for (const account of accounts) {
38-
if (account.type === "Configured") {
38+
if (account.kind === "Configured") {
3939
write(
4040
$head,
4141
`<a href="#" onclick="selectDeltaAccount(${account.id})">
@@ -57,7 +57,7 @@ async function run() {
5757
clear($main);
5858
const selectedAccount = SELECTED_ACCOUNT;
5959
const info = await client.rpc.getAccountInfo(selectedAccount);
60-
if (info.type !== "Configured") {
60+
if (info.kind !== "Configured") {
6161
return write($main, "Account is not configured");
6262
}
6363
write($main, `<h2>${info.addr!}</h2>`);
@@ -81,8 +81,7 @@ async function run() {
8181
messageIds
8282
);
8383
for (const [_messageId, message] of Object.entries(messages)) {
84-
if (message.variant === "message")
85-
write($main, `<p>${message.text}</p>`);
84+
if (message.kind === "message") write($main, `<p>${message.text}</p>`);
8685
else write($main, `<p>loading error: ${message.error}</p>`);
8786
}
8887
}
@@ -93,9 +92,9 @@ async function run() {
9392
$side,
9493
`
9594
<p class="message">
96-
[<strong>${event.type}</strong> on account ${accountId}]<br>
95+
[<strong>${event.kind}</strong> on account ${accountId}]<br>
9796
<em>f1:</em> ${JSON.stringify(
98-
Object.assign({}, event, { type: undefined })
97+
Object.assign({}, event, { kind: undefined })
9998
)}
10099
</p>`
101100
);

deltachat-jsonrpc/typescript/src/client.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ import { WebsocketTransport, BaseTransport, Request } from "yerpc";
66
import { TinyEmitter } from "@deltachat/tiny-emitter";
77

88
type Events = { ALL: (accountId: number, event: EventType) => void } & {
9-
[Property in EventType["type"]]: (
9+
[Property in EventType["kind"]]: (
1010
accountId: number,
11-
event: Extract<EventType, { type: Property }>
11+
event: Extract<EventType, { kind: Property }>
1212
) => void;
1313
};
1414

1515
type ContextEvents = { ALL: (event: EventType) => void } & {
16-
[Property in EventType["type"]]: (
17-
event: Extract<EventType, { type: Property }>
16+
[Property in EventType["kind"]]: (
17+
event: Extract<EventType, { kind: Property }>
1818
) => void;
1919
};
2020

2121
export type DcEvent = EventType;
22-
export type DcEventType<T extends EventType["type"]> = Extract<
22+
export type DcEventType<T extends EventType["kind"]> = Extract<
2323
EventType,
24-
{ type: T }
24+
{ kind: T }
2525
>;
2626

2727
export class BaseDeltaChat<
@@ -46,12 +46,12 @@ export class BaseDeltaChat<
4646
while (true) {
4747
const event = await this.rpc.getNextEvent();
4848
//@ts-ignore
49-
this.emit(event.event.type, event.contextId, event.event);
49+
this.emit(event.event.kind, event.contextId, event.event);
5050
this.emit("ALL", event.contextId, event.event);
5151

5252
if (this.contextEmitters[event.contextId]) {
5353
this.contextEmitters[event.contextId].emit(
54-
event.event.type,
54+
event.event.kind,
5555
//@ts-ignore
5656
event.event as any
5757
);

deltachat-jsonrpc/typescript/test/online.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ describe("online tests", function () {
2929
serverHandle = await startServer();
3030
dc = new DeltaChat(serverHandle.stdin, serverHandle.stdout, true);
3131

32-
dc.on("ALL", (contextId, { type }) => {
33-
if (type !== "Info") console.log(contextId, type);
32+
dc.on("ALL", (contextId, { kind }) => {
33+
if (kind !== "Info") console.log(contextId, kind);
3434
});
3535

3636
account1 = await createTempUser(process.env.DCC_NEW_TMP_EMAIL);
@@ -177,12 +177,12 @@ describe("online tests", function () {
177177
});
178178
});
179179

180-
async function waitForEvent<T extends DcEvent["type"]>(
180+
async function waitForEvent<T extends DcEvent["kind"]>(
181181
dc: DeltaChat,
182182
eventType: T,
183183
accountId: number,
184184
timeout: number = EVENT_TIMEOUT
185-
): Promise<Extract<DcEvent, { type: T }>> {
185+
): Promise<Extract<DcEvent, { kind: T }>> {
186186
return new Promise((resolve, reject) => {
187187
const rejectTimeout = setTimeout(
188188
() => reject(new Error("Timeout reached before event came in")),

deltachat-rpc-client/examples/echobot_advanced.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
@hooks.on(events.RawEvent)
1616
async def log_event(event):
17-
if event.type == EventType.INFO:
17+
if event.kind == EventType.INFO:
1818
logging.info(event.msg)
19-
elif event.type == EventType.WARNING:
19+
elif event.kind == EventType.WARNING:
2020
logging.warning(event.msg)
2121

2222

deltachat-rpc-client/src/deltachat_rpc_client/chat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ async def mute(self, duration: Optional[int] = None) -> None:
5454
"""
5555
if duration is not None:
5656
assert duration > 0, "Invalid duration"
57-
dur: Union[str, dict] = {"Until": duration}
57+
dur: dict = {kind: "Until", "duration": duration}
5858
else:
59-
dur = "Forever"
59+
dur = {kind: "Forever"}
6060
await self._rpc.set_chat_mute_duration(self.account.id, self.id, dur)
6161

6262
async def unmute(self) -> None:
6363
"""Unmute this chat."""
64-
await self._rpc.set_chat_mute_duration(self.account.id, self.id, "NotMuted")
64+
await self._rpc.set_chat_mute_duration(self.account.id, self.id, {kind: "NotMuted"})
6565

6666
async def pin(self) -> None:
6767
"""Pin this chat."""

deltachat-rpc-client/src/deltachat_rpc_client/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ async def run_until(self, func: Callable[[AttrDict], Union[bool, Coroutine]]) ->
106106
await self._process_messages() # Process old messages.
107107
while True:
108108
event = await self.account.wait_for_event()
109-
event["type"] = EventType(event.type)
109+
event["kind"] = EventType(event.kind)
110110
event["account"] = self.account
111111
await self._on_event(event)
112-
if event.type == EventType.INCOMING_MSG:
112+
if event.kind == EventType.INCOMING_MSG:
113113
await self._process_messages()
114114

115115
stop = func(event)

deltachat-rpc-client/src/deltachat_rpc_client/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def __eq__(self, other) -> bool:
8383
return False
8484

8585
async def filter(self, event: "AttrDict") -> bool:
86-
if self.types and event.type not in self.types:
86+
if self.types and event.kind not in self.types:
8787
return False
8888
return await self._call_func(event)
8989

deltachat-rpc-client/src/deltachat_rpc_client/pytestplugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async def get_online_account(self) -> Account:
5757
while True:
5858
event = await account.wait_for_event()
5959
print(event)
60-
if event.type == EventType.IMAP_INBOX_IDLE:
60+
if event.kind == EventType.IMAP_INBOX_IDLE:
6161
break
6262
return account
6363

@@ -98,7 +98,7 @@ async def process_message(
9898
group=group,
9999
)
100100

101-
return await to_client.run_until(lambda e: e.type == EventType.INCOMING_MSG)
101+
return await to_client.run_until(lambda e: e.kind == EventType.INCOMING_MSG)
102102

103103

104104
@pytest_asyncio.fixture

deltachat-rpc-client/tests/test_something.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async def test_acfactory(acfactory) -> None:
4545
account = await acfactory.new_configured_account()
4646
while True:
4747
event = await account.wait_for_event()
48-
if event.type == EventType.CONFIGURE_PROGRESS:
48+
if event.kind == EventType.CONFIGURE_PROGRESS:
4949
assert event.progress != 0 # Progress 0 indicates error.
5050
if event.progress == 1000: # Success
5151
break
@@ -76,7 +76,7 @@ async def test_account(acfactory) -> None:
7676

7777
while True:
7878
event = await bob.wait_for_event()
79-
if event.type == EventType.INCOMING_MSG:
79+
if event.kind == EventType.INCOMING_MSG:
8080
chat_id = event.chat_id
8181
msg_id = event.msg_id
8282
break
@@ -146,7 +146,7 @@ async def test_chat(acfactory) -> None:
146146

147147
while True:
148148
event = await bob.wait_for_event()
149-
if event.type == EventType.INCOMING_MSG:
149+
if event.kind == EventType.INCOMING_MSG:
150150
chat_id = event.chat_id
151151
msg_id = event.msg_id
152152
break
@@ -232,7 +232,7 @@ async def test_message(acfactory) -> None:
232232

233233
while True:
234234
event = await bob.wait_for_event()
235-
if event.type == EventType.INCOMING_MSG:
235+
if event.kind == EventType.INCOMING_MSG:
236236
chat_id = event.chat_id
237237
msg_id = event.msg_id
238238
break
@@ -272,7 +272,7 @@ async def test_is_bot(acfactory) -> None:
272272

273273
while True:
274274
event = await bob.wait_for_event()
275-
if event.type == EventType.INCOMING_MSG:
275+
if event.kind == EventType.INCOMING_MSG:
276276
msg_id = event.msg_id
277277
message = bob.get_message_by_id(msg_id)
278278
snapshot = await message.get_snapshot()

deltachat-rpc-client/tests/test_webxdc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async def test_webxdc(acfactory) -> None:
1313

1414
while True:
1515
event = await bob.wait_for_event()
16-
if event.type == EventType.INCOMING_MSG:
16+
if event.kind == EventType.INCOMING_MSG:
1717
bob_chat_alice = bob.get_chat_by_id(event.chat_id)
1818
message = bob.get_message_by_id(event.msg_id)
1919
break

0 commit comments

Comments
 (0)