@workerstack/clients 1.0.0-rc.27 → 1.0.0-rc.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -7
- package/dist/emit/cli/commands.js +25 -5
- package/dist/emit/cli/flags.js +7 -3
- package/dist/emit/cli/skills.js +5 -1
- package/dist/emit/csharp/realtime.d.ts +1 -8
- package/dist/emit/csharp/realtime.js +25 -25
- package/dist/emit/csharp/sdk-answer.d.ts +10 -2
- package/dist/emit/csharp/sdk-method.js +9 -2
- package/dist/emit/csharp/sse.d.ts +0 -11
- package/dist/emit/csharp/sse.js +91 -10
- package/dist/emit/csharp/transport.js +3 -3
- package/dist/emit/csharp/types-messages.d.ts +47 -0
- package/dist/emit/csharp/types-messages.js +172 -0
- package/dist/emit/csharp/types-names.js +3 -2
- package/dist/emit/csharp/ws.d.ts +0 -15
- package/dist/emit/csharp/ws.js +26 -5
- package/dist/emit/go/sdk-answer.d.ts +24 -1
- package/dist/emit/go/sdk-method.d.ts +3 -0
- package/dist/emit/go/sdk-method.js +16 -10
- package/dist/emit/go/sse.d.ts +2 -10
- package/dist/emit/go/sse.js +87 -12
- package/dist/emit/go/types-union.d.ts +3 -2
- package/dist/emit/go/types-union.js +2 -0
- package/dist/emit/go/types.d.ts +6 -0
- package/dist/emit/go/types.js +12 -1
- package/dist/emit/go/ws.d.ts +4 -11
- package/dist/emit/go/ws.js +214 -58
- package/dist/emit/java/json.d.ts +3 -0
- package/dist/emit/java/layout-ast.d.ts +4 -1
- package/dist/emit/java/layout-types.js +1 -1
- package/dist/emit/java/models-names.d.ts +2 -0
- package/dist/emit/java/models-names.js +1 -0
- package/dist/emit/java/models-union.d.ts +12 -1
- package/dist/emit/java/models-union.js +11 -5
- package/dist/emit/java/models.d.ts +5 -0
- package/dist/emit/java/models.js +22 -7
- package/dist/emit/java/realtime.d.ts +29 -9
- package/dist/emit/java/realtime.js +45 -10
- package/dist/emit/java/sdk-mirror.js +6 -5
- package/dist/emit/java/sdk-op.js +1 -1
- package/dist/emit/java/sse.d.ts +11 -10
- package/dist/emit/java/sse.js +104 -5
- package/dist/emit/java/ws.d.ts +46 -7
- package/dist/emit/java/ws.js +89 -4
- package/dist/emit/mcp/output-schema.d.ts +4 -2
- package/dist/emit/mcp/output-schema.js +56 -12
- package/dist/emit/python/realtime.d.ts +9 -6
- package/dist/emit/python/realtime.js +21 -19
- package/dist/emit/python/sdk-answer.d.ts +5 -1
- package/dist/emit/python/sdk-method.d.ts +1 -1
- package/dist/emit/python/sdk-method.js +33 -6
- package/dist/emit/python/sdk.js +22 -2
- package/dist/emit/python/sse.d.ts +0 -5
- package/dist/emit/python/sse.js +35 -5
- package/dist/emit/python/types.d.ts +7 -0
- package/dist/emit/python/types.js +20 -3
- package/dist/emit/python/ws.d.ts +20 -6
- package/dist/emit/python/ws.js +67 -1
- package/dist/emit/rust/realtime.d.ts +7 -5
- package/dist/emit/rust/realtime.js +14 -17
- package/dist/emit/rust/sdk-method.js +1 -1
- package/dist/emit/rust/sse.d.ts +11 -7
- package/dist/emit/rust/sse.js +52 -6
- package/dist/emit/rust/types-union.d.ts +9 -1
- package/dist/emit/rust/types-union.js +4 -0
- package/dist/emit/rust/types.d.ts +9 -0
- package/dist/emit/rust/types.js +13 -7
- package/dist/emit/rust/ws.d.ts +22 -4
- package/dist/emit/rust/ws.js +57 -7
- package/dist/emit/ts/route-tree.d.ts +1 -1
- package/dist/emit/ts/route-tree.js +0 -11
- package/dist/emit/ts/sdk-types-preamble.js +4 -0
- package/dist/emit/ts/sdk-types.js +49 -10
- package/dist/emit/ts/sdk.js +2 -2
- package/dist/emit/ts/service-map.d.ts +8 -2
- package/dist/emit/ts/service-map.js +30 -2
- package/dist/emit/ts/type-gen.d.ts +4 -3
- package/dist/emit/ts/type-gen.js +83 -40
- package/dist/extract/chain.d.ts +10 -6
- package/dist/extract/chain.js +5 -19
- package/dist/extract/extract.d.ts +2 -2
- package/dist/extract/extract.js +37 -23
- package/dist/extract/walk.d.ts +15 -4
- package/dist/extract/walk.js +159 -15
- package/dist/ir/build.js +4 -0
- package/dist/ir/model.d.ts +16 -0
- package/dist/ir/sdk-model.d.ts +25 -1
- package/dist/ir/sdk-model.js +35 -5
- package/dist/ir/slots.d.ts +1 -1
- package/dist/ir/slots.js +11 -3
- package/dist/ir/streams.d.ts +16 -0
- package/dist/ir/streams.js +40 -0
- package/dist/runtime/cli/socket.go +107 -13
- package/dist/runtime/cli/stream.go +18 -11
- package/dist/runtime/csharp/Adapters.cs +12 -11
- package/dist/runtime/csharp/Events.cs +22 -17
- package/dist/runtime/csharp/Realtime.cs +50 -23
- package/dist/runtime/csharp/RealtimeLoop.cs +1 -1
- package/dist/runtime/csharp/TransportSse.cs +3 -3
- package/dist/runtime/csharp/Ws.cs +1 -1
- package/dist/runtime/csharp/WsDial.cs +19 -1
- package/dist/runtime/csharp/WsTyped.cs +109 -0
- package/dist/runtime/go/realtime.go +125 -41
- package/dist/runtime/go/realtime_loop.go +21 -18
- package/dist/runtime/go/result.go +2 -1
- package/dist/runtime/go/sse.go +11 -0
- package/dist/runtime/go/stream.go +29 -13
- package/dist/runtime/go/transport.go +15 -21
- package/dist/runtime/go/transport_longpoll.go +3 -4
- package/dist/runtime/go/transport_sse.go +6 -6
- package/dist/runtime/go/ws.go +157 -61
- package/dist/runtime/java/EventStream.java +47 -29
- package/dist/runtime/java/Exchange.java +5 -5
- package/dist/runtime/java/Flight.java +3 -3
- package/dist/runtime/java/Realtime.java +45 -11
- package/dist/runtime/java/ResumableConnection.java +77 -32
- package/dist/runtime/java/Socket.java +8 -4
- package/dist/runtime/java/SseAdapter.java +1 -1
- package/dist/runtime/java/TypedWebSocket.java +81 -24
- package/dist/runtime/java/WsAdapter.java +4 -2
- package/dist/runtime/java/WsDial.java +12 -5
- package/dist/runtime/python/events.py +25 -11
- package/dist/runtime/python/operation.py +6 -1
- package/dist/runtime/python/realtime.py +47 -33
- package/dist/runtime/python/send_async.py +1 -0
- package/dist/runtime/python/transport_sse.py +2 -2
- package/dist/runtime/python/transport_ws.py +1 -1
- package/dist/runtime/python/ws.py +100 -39
- package/dist/runtime/rust/events.rs +64 -41
- package/dist/runtime/rust/realtime.rs +80 -34
- package/dist/runtime/rust/realtime_loop.rs +37 -12
- package/dist/runtime/rust/sse.rs +3 -2
- package/dist/runtime/rust/transport_sse.rs +1 -1
- package/dist/runtime/rust/transport_ws.rs +3 -2
- package/dist/runtime/rust/ws.rs +122 -50
- package/dist/runtime/ts/realtime-connection.ts +22 -0
- package/dist/runtime/ts/realtime-transports.ts +13 -4
- package/dist/runtime/ts/realtime.ts +27 -3
- package/dist/runtime/ts/sdk-realtime.ts +1 -1
- package/dist/runtime/ts/sdk-sse.ts +31 -14
- package/dist/runtime/ts/sdk-ws.ts +2 -1
- package/dist/runtime/ts/sdk.ts +9 -2
- package/dist/runtime/ts/sse.ts +8 -0
- package/dist/runtime/ts/ws.ts +88 -50
- package/package.json +3 -61
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mediaEssence } from "../../ir/sdk-model.js";
|
|
1
|
+
import { dispatchKey, isJsonEventData, mediaEssence, sseEvents, } from "../../ir/sdk-model.js";
|
|
2
2
|
import { pyString } from "../shared/escape.js";
|
|
3
3
|
import { compareCodeUnits, NameScope, pyIdentifier } from "../shared/lang.js";
|
|
4
4
|
import { realtimeStream } from "./realtime.js";
|
|
@@ -67,6 +67,28 @@ function acceptOf(op, kind) {
|
|
|
67
67
|
return "text/event-stream";
|
|
68
68
|
return kind === "text" || kind === "binary" ? mediaEssence(op.success.contentType) : null;
|
|
69
69
|
}
|
|
70
|
+
/** The names of the events whose data is JSON, for an SSE op that names its events (D7); null for any other. */
|
|
71
|
+
function jsonEvents(op, env) {
|
|
72
|
+
const events = op.stream === "sse" ? sseEvents(op.ir) : undefined;
|
|
73
|
+
if (events === undefined)
|
|
74
|
+
return null;
|
|
75
|
+
return tuple(events.filter(({ data }) => isJsonEventData(data, env.at.names.resolve)).map(({ name }) => pyString(name)));
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* How a WebSocket or realtime op's runtime frames its messages: JSON-encode every send when it declares what the
|
|
79
|
+
* server receives, parse every frame when it declares what the server sends, and the key that parsed union
|
|
80
|
+
* dispatches on (D9).
|
|
81
|
+
*/
|
|
82
|
+
function framing(op, env) {
|
|
83
|
+
const socket = op.stream === "ws" || op.stream === "realtime";
|
|
84
|
+
const { receive, send } = (socket ? op.ir.messages : undefined) ?? {};
|
|
85
|
+
const key = send === undefined ? undefined : dispatchKey(send, env.at.names.resolve);
|
|
86
|
+
return [
|
|
87
|
+
["encode", receive === undefined ? null : "True"],
|
|
88
|
+
["decode", send === undefined ? null : "True"],
|
|
89
|
+
["dispatch", key === undefined ? null : pyString(key)],
|
|
90
|
+
];
|
|
91
|
+
}
|
|
70
92
|
/** The `Operation(…)` keywords that differ from its defaults, in its field order. */
|
|
71
93
|
function namedArgs(parts, env) {
|
|
72
94
|
const { answer, headers, op } = parts;
|
|
@@ -83,6 +105,9 @@ function namedArgs(parts, env) {
|
|
|
83
105
|
["invalidates", op.invalidates.length > 0 ? tuple(op.invalidates.map(pyString)) : null],
|
|
84
106
|
["stream", op.stream === null ? null : pyString(op.stream)],
|
|
85
107
|
["headers", headers === null ? null : tuple(headers.read)],
|
|
108
|
+
["events", jsonEvents(op, env)],
|
|
109
|
+
...framing(op, env),
|
|
110
|
+
["kind", answer.kind ?? null],
|
|
86
111
|
];
|
|
87
112
|
return named.flatMap(([key, value]) => (value === null ? [] : [`${key}=${value}`]));
|
|
88
113
|
}
|
|
@@ -144,13 +169,15 @@ function methodLines(parts, async, env, shadowed) {
|
|
|
144
169
|
` return ${async ? "await " : ""}self._t.${answer.run}(${args.join(", ")})`,
|
|
145
170
|
];
|
|
146
171
|
}
|
|
147
|
-
/** The runtime and `collections.abc` names `op`'s methods spell. */
|
|
148
|
-
function noteImports(op, env) {
|
|
172
|
+
/** The runtime and `collections.abc` names `op`'s methods spell: none for a socket of its own class. */
|
|
173
|
+
function noteImports(op, answer, env) {
|
|
149
174
|
const names = { realtime: "ResumableConnection", sse: "EventStream", ws: "TypedWebSocket" };
|
|
150
|
-
if (op.stream !== null)
|
|
175
|
+
if (op.stream !== null && answer.kind === undefined)
|
|
151
176
|
env.imports.add(names[op.stream]);
|
|
152
|
-
if (op.stream === "ws"
|
|
177
|
+
if (op.stream === "ws")
|
|
153
178
|
env.imports.add("Sequence");
|
|
179
|
+
if (op.stream === "realtime")
|
|
180
|
+
env.imports.add("TransportOpts");
|
|
154
181
|
}
|
|
155
182
|
/** The method of `op` named `name`: its constant, its sync method (none for a stream), its async one, its plan. */
|
|
156
183
|
export function pyMethod(op, name, env) {
|
|
@@ -160,7 +187,7 @@ export function pyMethod(op, name, env) {
|
|
|
160
187
|
const answer = op.stream === null ? answerOf(op, env) : STREAMS[op.stream].answer(op, env);
|
|
161
188
|
const constant = env.constants.claim(`_${pyIdentifier(op.id).toUpperCase()}`);
|
|
162
189
|
const parts = { answer, constant, headers: responseHeaders(op, env), keywords, name, op, path };
|
|
163
|
-
noteImports(op, env);
|
|
190
|
+
noteImports(op, answer, env);
|
|
164
191
|
env.imports.add("Operation").add("RequestOptions");
|
|
165
192
|
const by = (where) => Object.fromEntries(keywords.filter((k) => k.where === where).map(({ ident, param }) => [param.name, ident]));
|
|
166
193
|
const plan = {
|
package/dist/emit/python/sdk.js
CHANGED
|
@@ -32,9 +32,25 @@ const RUNTIME = {
|
|
|
32
32
|
RequestOptions: "config",
|
|
33
33
|
ResumableConnection: "realtime",
|
|
34
34
|
Safe: "result",
|
|
35
|
+
SseEvent: "events",
|
|
35
36
|
SyncTransport: "send_sync",
|
|
37
|
+
TransportOpts: "transport",
|
|
36
38
|
TypedWebSocket: "ws",
|
|
37
39
|
};
|
|
40
|
+
/* where types.py imports each runtime name from, under `._http.`; any other it spells is `collections.abc`'s */
|
|
41
|
+
const TYPES_RUNTIME = {
|
|
42
|
+
FilePart: "body",
|
|
43
|
+
HttpError: "errors",
|
|
44
|
+
ResumableConnection: "realtime",
|
|
45
|
+
SseEventOf: "events",
|
|
46
|
+
TypedWebSocket: "ws",
|
|
47
|
+
WsClose: "ws",
|
|
48
|
+
};
|
|
49
|
+
/** types.py's imports of the runtime names it spells, a line per module in code-unit order. */
|
|
50
|
+
function typesRuntime(names) {
|
|
51
|
+
const modules = [...new Set([...names].flatMap((name) => TYPES_RUNTIME[name] ?? []))].sort(compareCodeUnits);
|
|
52
|
+
return modules.flatMap((module) => importLine(`._http.${module}`, [...names].filter((name) => TYPES_RUNTIME[name] === module)));
|
|
53
|
+
}
|
|
38
54
|
/** A docstring of `lines` at column 0: one line, or the first on the quotes and the closing quotes alone. */
|
|
39
55
|
const docstring = (lines) => lines.length === 1 ? [`"""${lines[0]}"""`] : [`"""${lines[0]}`, ...lines.slice(1), '"""'];
|
|
40
56
|
/** The blank lines before a module's `i`th declaration: one after the imports unless it is a class, else two. */
|
|
@@ -132,8 +148,12 @@ function emitPython(document, options, metadata = {}) {
|
|
|
132
148
|
[`${root}/py.typed`]: "",
|
|
133
149
|
[`${root}/types.py`]: pyFile([typesDoc[0]], [
|
|
134
150
|
["from __future__ import annotations"],
|
|
135
|
-
[
|
|
136
|
-
|
|
151
|
+
[
|
|
152
|
+
...importLine("collections.abc", [...env.typesImports].filter((name) => !(name in TYPES_RUNTIME))),
|
|
153
|
+
...(errorKey === null ? [] : ["from enum import StrEnum"]),
|
|
154
|
+
...importLine("typing", at.typing),
|
|
155
|
+
],
|
|
156
|
+
typesRuntime(env.typesImports),
|
|
137
157
|
], decls.map(([, text]) => text)),
|
|
138
158
|
...Object.fromEntries(Object.entries(runtime).map(([name, source]) => [`${root}/_http/${name}`, source])),
|
|
139
159
|
};
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* An SSE operation's answer (contract 34 §2.0–2.1): `EventStream[E]`, `E` the declared event schema's type (`str`
|
|
3
|
-
* for untyped events), opened by awaiting the method (G15), and its `last_event_id` field, sent as Last-Event-ID
|
|
4
|
-
* only when set and non-empty; a declared Last-Event-ID header param is folded into that field (Fd3).
|
|
5
|
-
*/
|
|
6
1
|
import { type PyStream } from "./sdk-answer.ts";
|
|
7
2
|
/** The header the SSE and realtime dials resume an event stream with. */
|
|
8
3
|
export declare const LAST_EVENT_ID = "Last-Event-ID";
|
package/dist/emit/python/sse.js
CHANGED
|
@@ -1,18 +1,48 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* An SSE operation's answer (contract 34 §2.0–2.1): `EventStream[E]`,
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* An SSE operation's answer (contract 34 §2.0–2.1): `EventStream[E]`, opened by awaiting the method (G15), and its
|
|
3
|
+
* `last_event_id` field, sent as Last-Event-ID only when set and non-empty; a declared Last-Event-ID header param is
|
|
4
|
+
* folded into that field (Fd3). An operation that names its events (D7) yields `<Op>Event`, a union of
|
|
5
|
+
* `SseEventOf[Literal[name], Data]` hoisted beside the types; any other yields `SseEvent[T]`, `T` its declared
|
|
6
|
+
* event schema's type (`str` for untyped events).
|
|
5
7
|
*/
|
|
8
|
+
import { sseEvents } from "../../ir/sdk-model.js";
|
|
9
|
+
import { pyClassName } from "../shared/lang.js";
|
|
6
10
|
import { eventType } from "./sdk-answer.js";
|
|
11
|
+
import { pyTypeOf } from "./types.js";
|
|
12
|
+
import { aliasDecl } from "./types-union.js";
|
|
7
13
|
/** The header the SSE and realtime dials resume an event stream with. */
|
|
8
14
|
export const LAST_EVENT_ID = "Last-Event-ID";
|
|
9
15
|
/** Whether a declared param is the Last-Event-ID header (any casing), the resume channel an SSE dial sends. */
|
|
10
16
|
export const isLastEventIdHeader = (name, where) => where === "header" && name.toLowerCase() === LAST_EVENT_ID.toLowerCase();
|
|
17
|
+
/**
|
|
18
|
+
* The event type of an op that names its events: `<Op>Event`, each event's `data` typed at `<Op><Name>`, declared
|
|
19
|
+
* in types.py; null for an op that names none.
|
|
20
|
+
*/
|
|
21
|
+
function namedEvents(op, env) {
|
|
22
|
+
const events = sseEvents(op.ir);
|
|
23
|
+
if (events === undefined)
|
|
24
|
+
return null;
|
|
25
|
+
const typing = env.at.declTyping;
|
|
26
|
+
const at = { ...env.at, parentName: op.id, typing };
|
|
27
|
+
const members = events.map(({ data, name }) => {
|
|
28
|
+
const literal = pyTypeOf({ kind: "const", value: name }, { ...at, fieldName: "" });
|
|
29
|
+
return `SseEventOf[${literal}, ${pyTypeOf(data, { ...at, fieldName: name })}]`;
|
|
30
|
+
});
|
|
31
|
+
const alias = env.at.names.claim(`${pyClassName(op.id)}Event`);
|
|
32
|
+
typing.add("TypeAlias");
|
|
33
|
+
env.typesImports.add("SseEventOf");
|
|
34
|
+
env.at.decls.set(alias, aliasDecl(alias, members.join(" | ")));
|
|
35
|
+
return alias;
|
|
36
|
+
}
|
|
11
37
|
/** The SSE operation's answer, its one field and its docstring's lines. */
|
|
12
38
|
export const sseStream = {
|
|
13
39
|
answer: (op, env) => {
|
|
14
|
-
const
|
|
15
|
-
|
|
40
|
+
const named = namedEvents(op, env);
|
|
41
|
+
if (named !== null)
|
|
42
|
+
return { returns: `EventStream[${named}]`, run: "sse", type: named };
|
|
43
|
+
env.imports.add("SseEvent");
|
|
44
|
+
const event = `SseEvent[${eventType(op, env, () => "str")}]`;
|
|
45
|
+
return { returns: `EventStream[${event}]`, run: "sse", type: event };
|
|
16
46
|
},
|
|
17
47
|
carries: isLastEventIdHeader,
|
|
18
48
|
doc: [
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* quoted string. Spec text reaches the source only through the shared escape layer.
|
|
6
6
|
*/
|
|
7
7
|
import type { IR, IRSchema } from "../../ir/model.ts";
|
|
8
|
+
import type { Dispatch } from "../../ir/sdk-model.ts";
|
|
8
9
|
/** How component and hoisted names are claimed. */
|
|
9
10
|
export type PyTypeNames = {
|
|
10
11
|
/** the class name of the component `name` */
|
|
@@ -32,5 +33,11 @@ export type PyTypeContext = {
|
|
|
32
33
|
export declare function pyTypeContext(ir?: IR): PyTypeContext;
|
|
33
34
|
/** The Python type of `ir` in use position; what it hoists lands in `at.decls`. */
|
|
34
35
|
export declare function pyTypeOf(ir: IRSchema, at: PyTypeContext): string;
|
|
36
|
+
/**
|
|
37
|
+
* Each member of the keyed union `ir` (a ref to it, or it written at `at`) as its value and the type its declaration
|
|
38
|
+
* names it by, in order: the names the union's own declaration spells, a ref's class or a hoisted
|
|
39
|
+
* `<Parent><Field><Const>`. Spelled in types.py, so what they hoist imports into `at.typing`.
|
|
40
|
+
*/
|
|
41
|
+
export declare function pyKeyedMembers(ir: IRSchema, dispatch: Dispatch, at: PyTypeContext): (readonly [string, string])[];
|
|
35
42
|
/** The declaration of `name` (a class name already) for `ir`, `doc` its description. */
|
|
36
43
|
export declare function pyTopLevel(name: string, ir: IRSchema, at: PyTypeContext, doc?: string): string;
|
|
@@ -72,9 +72,13 @@ function partsOf(ir, at) {
|
|
|
72
72
|
return [...partsOf(ir.inner, deeper(at)), { type: "None" }];
|
|
73
73
|
if (ir.kind !== "union")
|
|
74
74
|
return [{ type: single(ir, at) }];
|
|
75
|
-
return ir.variants.flatMap((variant, i) => variant
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
return ir.variants.flatMap((variant, i) => variantParts(variant, i, ir, at));
|
|
76
|
+
}
|
|
77
|
+
/** The members variant `i` of `union` contributes: an inline object hoisted as `<Parent><Field><Const>`. */
|
|
78
|
+
function variantParts(variant, i, union, at) {
|
|
79
|
+
return variant.kind === "object" && variant.fields.length > 0
|
|
80
|
+
? [{ type: hoist(variant, deeper(at), variantSuffix(variant, i, union.discriminator)) }]
|
|
81
|
+
: partsOf(variant, deeper(at));
|
|
78
82
|
}
|
|
79
83
|
/** A type that is never a union by itself. */
|
|
80
84
|
function single(ir, at) {
|
|
@@ -105,6 +109,19 @@ function use(ir, at) {
|
|
|
105
109
|
export function pyTypeOf(ir, at) {
|
|
106
110
|
return use(ir, { ...at, declTyping: at.declTyping ?? at.typing, depth: 0 });
|
|
107
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Each member of the keyed union `ir` (a ref to it, or it written at `at`) as its value and the type its declaration
|
|
114
|
+
* names it by, in order: the names the union's own declaration spells, a ref's class or a hoisted
|
|
115
|
+
* `<Parent><Field><Const>`. Spelled in types.py, so what they hoist imports into `at.typing`.
|
|
116
|
+
*/
|
|
117
|
+
export function pyKeyedMembers(ir, dispatch, at) {
|
|
118
|
+
const base = { ...at, declTyping: at.typing, depth: 0 };
|
|
119
|
+
const where = ir.kind === "ref" ? { ...base, fieldName: "", parentName: at.names.ref(ir.name) } : base;
|
|
120
|
+
return dispatch.members.map(({ value, variant }, i) => [
|
|
121
|
+
value,
|
|
122
|
+
joined(variantParts(variant, i, dispatch.union, where)).text,
|
|
123
|
+
]);
|
|
124
|
+
}
|
|
108
125
|
/** The keys of the TypedDict `name` declares for `fields`, `NotRequired` noted when one is optional. */
|
|
109
126
|
function dictKeys(name, fields, at) {
|
|
110
127
|
const member = (schema, fieldName) => use(schema, { ...at, declTyping: at.declTyping ?? at.typing, depth: 0, fieldName, parentName: name });
|
package/dist/emit/python/ws.d.ts
CHANGED
|
@@ -1,14 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* declared `reconnect_token` query param is folded into that field (Fd3).
|
|
5
|
-
*/
|
|
6
|
-
import type { PyStream } from "./sdk-answer.ts";
|
|
1
|
+
import { type SdkOp } from "../../ir/sdk-model.ts";
|
|
2
|
+
import { type Answer, type PyStream } from "./sdk-answer.ts";
|
|
3
|
+
import type { MethodEnv } from "./sdk-method.ts";
|
|
7
4
|
/** The resume cursor's token, which the WebSocket and realtime methods take. */
|
|
8
5
|
export declare const RECONNECT_TOKEN = "reconnect_token: str | None = None";
|
|
9
6
|
/** The subprotocols a WebSocket or realtime dial offers. */
|
|
10
7
|
export declare const PROTOCOLS = "protocols: Sequence[str] | None = None";
|
|
11
8
|
/** Whether a declared param is the `reconnect_token` query param, the resume channel a socket dial sends. */
|
|
12
9
|
export declare const isReconnectToken: (name: string, where: string) => boolean;
|
|
10
|
+
/** A socket class's `on` events beyond its members', given its `Out`: each with the type its handler takes. */
|
|
11
|
+
export type Lifecycle = (out: string) => readonly (readonly [event: string, handler: string])[];
|
|
12
|
+
/**
|
|
13
|
+
* How a socket stream answers: its generic class `base[Out, In]` and the transport call it makes; what it reads and
|
|
14
|
+
* sends when its op declares no `send` or `receive`; the subclass suffix a keyed `send` union names; the events
|
|
15
|
+
* every `on` takes.
|
|
16
|
+
*/
|
|
17
|
+
export type SocketShape = {
|
|
18
|
+
readonly base: string;
|
|
19
|
+
readonly run: string;
|
|
20
|
+
readonly out: () => string;
|
|
21
|
+
readonly in: string;
|
|
22
|
+
readonly suffix: string;
|
|
23
|
+
readonly lifecycle: Lifecycle;
|
|
24
|
+
};
|
|
25
|
+
/** A socket op's answer: its `<Op><suffix>` when its `send` union is keyed (D9), else `base[Out, In]`. */
|
|
26
|
+
export declare function socketAnswer(op: SdkOp, env: MethodEnv, shape: SocketShape): Answer;
|
|
13
27
|
/** The WebSocket operation's answer, its two fields and its docstring's lines. */
|
|
14
28
|
export declare const wsStream: PyStream;
|
package/dist/emit/python/ws.js
CHANGED
|
@@ -1,12 +1,78 @@
|
|
|
1
|
+
import { dispatchOf } from "../../ir/sdk-model.js";
|
|
2
|
+
import { pyString } from "../shared/escape.js";
|
|
3
|
+
import { pyClassName } from "../shared/lang.js";
|
|
4
|
+
import { typeAt } from "./sdk-answer.js";
|
|
5
|
+
import { pyKeyedMembers, pyTypeOf } from "./types.js";
|
|
1
6
|
/** The resume cursor's token, which the WebSocket and realtime methods take. */
|
|
2
7
|
export const RECONNECT_TOKEN = "reconnect_token: str | None = None";
|
|
3
8
|
/** The subprotocols a WebSocket or realtime dial offers. */
|
|
4
9
|
export const PROTOCOLS = "protocols: Sequence[str] | None = None";
|
|
5
10
|
/** Whether a declared param is the `reconnect_token` query param, the resume channel a socket dial sends. */
|
|
6
11
|
export const isReconnectToken = (name, where) => where === "query" && name === "reconnect_token";
|
|
12
|
+
/** One `on` overload: `event` hands its handler a `type`. */
|
|
13
|
+
const overload = ([event, type]) => [
|
|
14
|
+
" @overload",
|
|
15
|
+
` def on(self, event: Literal[${pyString(event)}], handler: Callable[[${type}], None]) -> None: ...`,
|
|
16
|
+
"",
|
|
17
|
+
];
|
|
18
|
+
/**
|
|
19
|
+
* `<Op><suffix>`, the subclass of `base[Out, In]` whose `on` takes each member of the keyed `send` union by its value
|
|
20
|
+
* (one a lifecycle event names excepted), then the lifecycle events, hoisted beside the types; null when no member is
|
|
21
|
+
* left to take.
|
|
22
|
+
*/
|
|
23
|
+
function keyedClass(op, env, shape, dispatch) {
|
|
24
|
+
const { receive, send } = op.ir.messages;
|
|
25
|
+
const typing = env.at.declTyping;
|
|
26
|
+
const at = { ...env.at, fieldName: "Send", parentName: op.id, typing };
|
|
27
|
+
/* spelled in types.py, whose typing names they then take: the same hoist keys, so the names client.py spells */
|
|
28
|
+
const out = pyTypeOf(send, at);
|
|
29
|
+
const inType = receive === undefined ? shape.in : pyTypeOf(receive, { ...at, fieldName: "Receive" });
|
|
30
|
+
const lifecycle = shape.lifecycle(out);
|
|
31
|
+
const taken = new Set(lifecycle.map(([event]) => event));
|
|
32
|
+
const members = pyKeyedMembers(send, dispatch, at).filter(([value]) => !taken.has(value));
|
|
33
|
+
if (members.length === 0)
|
|
34
|
+
return null;
|
|
35
|
+
const name = env.at.names.claim(`${pyClassName(op.id)}${shape.suffix}`);
|
|
36
|
+
for (const used of ["Any", "Literal", "overload"])
|
|
37
|
+
typing.add(used);
|
|
38
|
+
env.typesImports.add("Callable").add(shape.base);
|
|
39
|
+
for (const [, type] of lifecycle)
|
|
40
|
+
if (type !== out)
|
|
41
|
+
env.typesImports.add(type);
|
|
42
|
+
env.at.decls.set(name, [
|
|
43
|
+
`class ${name}(${shape.base}[${pyString(out)}, ${pyString(inType)}]):`,
|
|
44
|
+
...[...members, ...lifecycle].flatMap(overload),
|
|
45
|
+
" def on(self, event: str, handler: Callable[[Any], None]) -> None:",
|
|
46
|
+
" self.listen(event, handler)",
|
|
47
|
+
].join("\n"));
|
|
48
|
+
return name;
|
|
49
|
+
}
|
|
50
|
+
/** A socket op's answer: its `<Op><suffix>` when its `send` union is keyed (D9), else `base[Out, In]`. */
|
|
51
|
+
export function socketAnswer(op, env, shape) {
|
|
52
|
+
const { receive, send } = op.ir.messages ?? {};
|
|
53
|
+
const dispatch = send === undefined ? undefined : dispatchOf(send, env.at.names.resolve);
|
|
54
|
+
const kind = dispatch === undefined ? null : keyedClass(op, env, shape, dispatch);
|
|
55
|
+
if (kind !== null)
|
|
56
|
+
return { kind, returns: kind, run: shape.run, type: kind };
|
|
57
|
+
const out = send === undefined ? shape.out() : typeAt(op, "Send", send, env);
|
|
58
|
+
const type = `${shape.base}[${out}, ${receive === undefined ? shape.in : typeAt(op, "Receive", receive, env)}]`;
|
|
59
|
+
return { returns: type, run: shape.run, type };
|
|
60
|
+
}
|
|
7
61
|
/** The WebSocket operation's answer, its two fields and its docstring's lines. */
|
|
8
62
|
export const wsStream = {
|
|
9
|
-
answer: () => (
|
|
63
|
+
answer: (op, env) => socketAnswer(op, env, {
|
|
64
|
+
base: "TypedWebSocket",
|
|
65
|
+
in: "object",
|
|
66
|
+
/* the events every socket's `on` takes, with what each hands its handler */
|
|
67
|
+
lifecycle: (out) => [
|
|
68
|
+
["message", out],
|
|
69
|
+
["close", "WsClose"],
|
|
70
|
+
["error", "HttpError"],
|
|
71
|
+
],
|
|
72
|
+
out: () => "str | bytes",
|
|
73
|
+
run: "websocket",
|
|
74
|
+
suffix: "Socket",
|
|
75
|
+
}),
|
|
10
76
|
carries: isReconnectToken,
|
|
11
77
|
doc: [
|
|
12
78
|
'Opens the socket: a refused handshake raises its status\'s error; options["cancel"], close() or',
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* A realtime operation's answer (contract 20 §2.0, §2.3): `ResumableConnection<
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* A realtime operation's answer (contract 20 §2.0, §2.3): `ResumableConnection<In, Out>`, opened before the call
|
|
3
|
+
* resolves over WebSocket, else SSE, else long-poll. `In` is what the server sends (`send`), else the declared event
|
|
4
|
+
* schema's type, `Out` what it receives (`receive`), an inline union hoisted as `<Op>Send` or `<Op>Receive`; an op
|
|
5
|
+
* without them keeps the defaults, `serde_json::Value` read from JSON else text. A read side keyed on one field (D9)
|
|
6
|
+
* gets an `on_<value>` per member but `message`. Its Opts carry the resume cursor, the offered subprotocols and the
|
|
7
|
+
* reconnect policy as one `transport: TransportOpts`; a declared param on a resume channel is folded into it.
|
|
6
8
|
*/
|
|
7
9
|
import type { SdkOp } from "../../ir/sdk-model.ts";
|
|
8
10
|
import type { StreamAnswer } from "./sdk-opts.ts";
|
|
9
11
|
import type { RustTypeContext } from "./types.ts";
|
|
10
|
-
/** The realtime operation `op`'s answer
|
|
12
|
+
/** The realtime operation `op`'s answer, its message types hoisted into `at`. */
|
|
11
13
|
export declare function realtimeAnswer(op: SdkOp, at: RustTypeContext): StreamAnswer;
|
|
@@ -1,29 +1,26 @@
|
|
|
1
|
-
import { eventType, isLastEventIdHeader
|
|
2
|
-
import { isReconnectToken,
|
|
1
|
+
import { eventType, isLastEventIdHeader } from "./sse.js";
|
|
2
|
+
import { isReconnectToken, messageType, typeParams, valueHandlers } from "./ws.js";
|
|
3
3
|
/* the WebSocket and long-poll dials send the cursor as query params, the SSE dial its id as Last-Event-ID */
|
|
4
4
|
const REALTIME = {
|
|
5
5
|
carries: (param, where) => isReconnectToken(param, where) ||
|
|
6
6
|
(where === "query" && param === "last_event_id") ||
|
|
7
7
|
isLastEventIdHeader(param, where),
|
|
8
8
|
fields: [
|
|
9
|
-
|
|
10
|
-
RECONNECT_TOKEN,
|
|
11
|
-
PROTOCOLS,
|
|
12
|
-
{ doc: "Reconnects after a drop; 5 when None.", ident: "max_reconnect_attempts", type: "Option<u32>" },
|
|
13
|
-
{
|
|
14
|
-
doc: "The first backoff delay in milliseconds; 1000 when None.",
|
|
15
|
-
ident: "reconnect_delay_ms",
|
|
16
|
-
type: "Option<u64>",
|
|
17
|
-
},
|
|
9
|
+
{ doc: "The resume cursor, offered subprotocols and reconnect policy.", ident: "transport", type: "TransportOpts" },
|
|
18
10
|
],
|
|
19
11
|
sends: [],
|
|
20
12
|
};
|
|
21
|
-
/* the realtime Opts' own fields, each the TransportOpts field of its name */
|
|
22
|
-
const TRANSPORT_OPTS = "TransportOpts { last_event_id: opts.last_event_id.clone(), reconnect_token: opts.reconnect_token.clone(), protocols: opts.protocols.clone(), max_reconnect_attempts: opts.max_reconnect_attempts, reconnect_delay_ms: opts.reconnect_delay_ms }";
|
|
23
13
|
const DOC = "Resolves once the connection is open over WebSocket, else SSE, else long-poll; `recv` reconnects after a drop and resumes after the last message it delivered. `cancel_on` ends it quietly; dropping it closes it.";
|
|
24
|
-
/** The realtime operation `op`'s answer
|
|
14
|
+
/** The realtime operation `op`'s answer, its message types hoisted into `at`. */
|
|
25
15
|
export function realtimeAnswer(op, at) {
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
|
|
16
|
+
const { receive, send } = op.ir.messages ?? {};
|
|
17
|
+
const read = send && messageType(op, send, "Send", at);
|
|
18
|
+
const write = receive && messageType(op, receive, "Receive", at);
|
|
19
|
+
if (send !== undefined && read !== undefined)
|
|
20
|
+
valueHandlers("ResumableConnection", ["on_message"], { schema: send, type: read }, at);
|
|
21
|
+
/* declared messages are JSON: one that is not is an error; an event type or none reads text too */
|
|
22
|
+
const loop = read === undefined && write === undefined ? "connect" : "typed";
|
|
23
|
+
const params = typeParams(read ?? (op.events === undefined ? undefined : eventType(op, op.events, at)), write, "serde_json::Value");
|
|
24
|
+
const run = `crate::realtime_loop::${loop}(opts.transport.clone())`;
|
|
25
|
+
return { doc: DOC, run, stream: REALTIME, type: `ResumableConnection${params}` };
|
|
29
26
|
}
|
|
@@ -13,7 +13,7 @@ const METHOD_LOCALS = ["body", "client", "form", "opts", "spec"];
|
|
|
13
13
|
function streamAnswer(op, at) {
|
|
14
14
|
if (op.stream === "sse")
|
|
15
15
|
return sseAnswer(op, at);
|
|
16
|
-
return op.stream === "ws" ? wsAnswer() : realtimeAnswer(op, at);
|
|
16
|
+
return op.stream === "ws" ? wsAnswer(op, at) : realtimeAnswer(op, at);
|
|
17
17
|
}
|
|
18
18
|
/** How a method answers: a stream's way, else its answer's kind read. */
|
|
19
19
|
function answerOf(op, env) {
|
package/dist/emit/rust/sse.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* An SSE operation's answer (contract 20 §2.0–2.1)
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* An SSE operation's answer (contract 20 §2.0–2.1) and its Opts' `last_event_id`, sent as Last-Event-ID only
|
|
3
|
+
* when set and non-empty; a declared Last-Event-ID header param is folded into that field (Fd3). An operation that
|
|
4
|
+
* names its events (D7) answers `EventStream<<Op>Event>`: an enum with one variant per event name, holding
|
|
5
|
+
* `SseEvent<Data>` decoded by its name, and `Unknown(SseEvent<String>)` for a name it does not declare; it serializes
|
|
6
|
+
* as the `SseEvent` it holds (`{data, event, id, retry}`), so one reader takes any stream's events. Any other answers
|
|
7
|
+
* `EventStream<SseEvent<T>>`, `T` the declared event schema's type, or `EventStream` for untyped events.
|
|
5
8
|
*/
|
|
6
|
-
import type {
|
|
9
|
+
import type { IRSchema } from "../../ir/model.ts";
|
|
10
|
+
import { type SdkOp } from "../../ir/sdk-model.ts";
|
|
7
11
|
import type { OptsField, StreamAnswer } from "./sdk-opts.ts";
|
|
8
12
|
import type { RustTypeContext } from "./types.ts";
|
|
9
13
|
/** The resume cursor's id, which the SSE and realtime Opts carry. */
|
|
@@ -13,7 +17,7 @@ export declare const LAST_EVENT_ID: {
|
|
|
13
17
|
};
|
|
14
18
|
/** Whether a declared param is the Last-Event-ID header (any casing), the resume channel an SSE dial sends. */
|
|
15
19
|
export declare const isLastEventIdHeader: (param: string, where: OptsField["in"]) => boolean;
|
|
16
|
-
/**
|
|
17
|
-
export declare const eventType: (op: SdkOp,
|
|
18
|
-
/** The SSE operation `op`'s answer: its events as text
|
|
20
|
+
/** The type of the events `op` declares as `events`. */
|
|
21
|
+
export declare const eventType: (op: SdkOp, events: IRSchema, at: RustTypeContext) => string;
|
|
22
|
+
/** The SSE operation `op`'s answer: its events by name, else as text or JSON decoded into the event type. */
|
|
19
23
|
export declare function sseAnswer(op: SdkOp, at: RustTypeContext): StreamAnswer;
|
package/dist/emit/rust/sse.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
+
import { isJsonEventData, sseEvents } from "../../ir/sdk-model.js";
|
|
2
|
+
import { lineComment, rustString } from "../shared/escape.js";
|
|
3
|
+
import { NameScope, rustTypeIdent } from "../shared/lang.js";
|
|
1
4
|
import { typeAt } from "./sdk-body.js";
|
|
5
|
+
import { variantOf } from "./types-enum.js";
|
|
2
6
|
/** The resume cursor's id, which the SSE and realtime Opts carry. */
|
|
3
7
|
export const LAST_EVENT_ID = { ident: "last_event_id", type: "Option<String>" };
|
|
4
8
|
/** Whether a declared param is the Last-Event-ID header (any casing), the resume channel an SSE dial sends. */
|
|
5
9
|
export const isLastEventIdHeader = (param, where) => where === "header" && param.toLowerCase() === "last-event-id";
|
|
6
|
-
/**
|
|
7
|
-
export const eventType = (op,
|
|
10
|
+
/** The type of the events `op` declares as `events`. */
|
|
11
|
+
export const eventType = (op, events, at) => typeAt(events, op.id, "Event", at);
|
|
8
12
|
/* Last-Event-ID, sent only when set: an empty cursor names no position */
|
|
9
13
|
const SSE = {
|
|
10
14
|
carries: isLastEventIdHeader,
|
|
@@ -12,9 +16,51 @@ const SSE = {
|
|
|
12
16
|
sends: ['spec.header("Last-Event-ID", &crate::request::cursor(&opts.last_event_id));'],
|
|
13
17
|
};
|
|
14
18
|
const DOC = "Resolves once the stream is open: an error status is the answer's error (`Err(Error::Api)`, on `client.safe()` an `Answer::Err`). No timeout bounds the stream after that; `cancel_on` ends it quietly, and dropping it closes it.";
|
|
15
|
-
/** The
|
|
19
|
+
/** The match arm decoding `variant`: its data from JSON, or the event as sent. */
|
|
20
|
+
const armOf = ({ event, json, variant }) => ` ${rustString(event)} => ${json ? `crate::events::named_data(e).map(Self::${variant})` : `Ok(Self::${variant}(e))`},`;
|
|
21
|
+
/** `<Op>Event`, declared in `at` with its variants in the order declared and its decode; its name. */
|
|
22
|
+
function namedEvents(op, events, at) {
|
|
23
|
+
const name = at.names.claim(`${rustTypeIdent(op.id)}Event`);
|
|
24
|
+
const scope = new NameScope(["Unknown"]);
|
|
25
|
+
const variants = events.map(({ data, name: event }) => ({
|
|
26
|
+
data: typeAt(data, op.id, event, at),
|
|
27
|
+
event,
|
|
28
|
+
json: isJsonEventData(data, at.names.resolve),
|
|
29
|
+
variant: variantOf(event, scope),
|
|
30
|
+
}));
|
|
31
|
+
const list = variants.map((v) => `\`${v.variant}\``).join(", ");
|
|
32
|
+
at.decls.set(name, [
|
|
33
|
+
...lineComment(`One event of ${op.id}: ${list}, or \`Unknown\` for a name the operation does not declare, its data as sent. A match on it reads the event's data; it serializes as the \`SseEvent\` it holds.`, "///"),
|
|
34
|
+
"#[derive(Debug, Clone, PartialEq, serde::Serialize)]",
|
|
35
|
+
"#[serde(untagged)]",
|
|
36
|
+
`pub enum ${name} {`,
|
|
37
|
+
...variants.map((v) => ` ${v.variant}(crate::SseEvent<${v.data}>),`),
|
|
38
|
+
" Unknown(crate::SseEvent<String>),",
|
|
39
|
+
"}",
|
|
40
|
+
"",
|
|
41
|
+
`impl crate::events::Named for ${name} {`,
|
|
42
|
+
" fn named(mut e: crate::SseEvent) -> Result<Self, crate::Error> {",
|
|
43
|
+
" if e.event.is_empty() {",
|
|
44
|
+
' e.event = "message".to_owned();',
|
|
45
|
+
" }",
|
|
46
|
+
" match e.event.as_str() {",
|
|
47
|
+
...variants.map(armOf),
|
|
48
|
+
" _ => Ok(Self::Unknown(e)),",
|
|
49
|
+
" }",
|
|
50
|
+
" }",
|
|
51
|
+
"}",
|
|
52
|
+
].join("\n"));
|
|
53
|
+
return name;
|
|
54
|
+
}
|
|
55
|
+
/** The SSE operation `op`'s answer: its events by name, else as text or JSON decoded into the event type. */
|
|
16
56
|
export function sseAnswer(op, at) {
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
57
|
+
const named = sseEvents(op.ir);
|
|
58
|
+
if (named !== undefined) {
|
|
59
|
+
const name = namedEvents(op, named, at);
|
|
60
|
+
return { doc: DOC, run: `crate::events::named::<${name}>`, stream: SSE, type: `EventStream<${name}>` };
|
|
61
|
+
}
|
|
62
|
+
if (op.events === undefined)
|
|
63
|
+
return { doc: DOC, run: "crate::events::text", stream: SSE, type: "EventStream" };
|
|
64
|
+
const events = eventType(op, op.events, at);
|
|
65
|
+
return { doc: DOC, run: `crate::events::json::<${events}>`, stream: SSE, type: `EventStream<SseEvent<${events}>>` };
|
|
20
66
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* object, plus `Unknown` keeping any other payload whole, serialized as the payload and deserialized
|
|
6
6
|
* on the discriminator's value with the payload kept, the discriminator included.
|
|
7
7
|
*/
|
|
8
|
-
import type { IRSchema } from "../../ir/model.ts";
|
|
8
|
+
import type { IRDiscriminator, IRSchema } from "../../ir/model.ts";
|
|
9
9
|
import { type Member } from "./types-struct.ts";
|
|
10
10
|
import type { RustTypeNames } from "./types.ts";
|
|
11
11
|
type Of<K extends IRSchema["kind"]> = Extract<IRSchema, {
|
|
@@ -19,6 +19,14 @@ export type UnionScope = {
|
|
|
19
19
|
member: Member;
|
|
20
20
|
declare: Declare;
|
|
21
21
|
};
|
|
22
|
+
/** Each value a keyed union selects a variant by, beside that variant's name and payload type, in declared order. */
|
|
23
|
+
export declare function unionVariants(name: string, union: Of<"union"> & {
|
|
24
|
+
discriminator: IRDiscriminator;
|
|
25
|
+
}, scope: UnionScope): {
|
|
26
|
+
value: string;
|
|
27
|
+
variant: string;
|
|
28
|
+
type: string;
|
|
29
|
+
}[];
|
|
22
30
|
/** The declaration of a union with at least one variant. */
|
|
23
31
|
export declare function unionDecl(name: string, union: Of<"union">, scope: UnionScope): string;
|
|
24
32
|
export {};
|
|
@@ -47,6 +47,10 @@ function variantsOf(name, union, scope) {
|
|
|
47
47
|
return [{ type, values: values.filter(fresh), variant: variantOf(values[0] ?? type, variantNames) }];
|
|
48
48
|
});
|
|
49
49
|
}
|
|
50
|
+
/** Each value a keyed union selects a variant by, beside that variant's name and payload type, in declared order. */
|
|
51
|
+
export function unionVariants(name, union, scope) {
|
|
52
|
+
return variantsOf(name, union, scope).flatMap(({ type, values, variant }) => values.map((value) => ({ type, value, variant })));
|
|
53
|
+
}
|
|
50
54
|
/** `Serialize` as the payload of whichever variant holds it. */
|
|
51
55
|
const serializeImpl = (name, variants) => [
|
|
52
56
|
`impl serde::Serialize for ${name} {`,
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* only through the shared escape layer.
|
|
6
6
|
*/
|
|
7
7
|
import type { IR, IRSchema } from "../../ir/model.ts";
|
|
8
|
+
import type { Dispatch } from "../../ir/sdk-model.ts";
|
|
8
9
|
/** How component and hoisted names are claimed, and which components sit on a value cycle. */
|
|
9
10
|
export type RustTypeNames = {
|
|
10
11
|
/** the Rust type name of the component `name` */
|
|
@@ -28,7 +29,15 @@ export type RustTypeContext = {
|
|
|
28
29
|
};
|
|
29
30
|
/** A context over `ir`'s components, or plain names without one. */
|
|
30
31
|
export declare function rustTypeContext(ir?: IR): RustTypeContext;
|
|
32
|
+
/** `ir` declared once as `names.hoist(key, base)` into `at.decls`; its name. */
|
|
33
|
+
export declare function rustHoisted(ir: IRSchema, at: RustTypeContext, key: string, base: string): string;
|
|
31
34
|
/** The Rust type of `ir` in use position; what it hoists lands in `at.decls`. */
|
|
32
35
|
export declare function rustTypeOf(ir: IRSchema, at: RustTypeContext): string;
|
|
36
|
+
/** Each value the keyed union `union`, declared as `name`, selects a variant by, with that variant and its payload type. */
|
|
37
|
+
export declare const rustUnionVariants: (name: string, union: Dispatch["union"], at: RustTypeContext) => {
|
|
38
|
+
value: string;
|
|
39
|
+
variant: string;
|
|
40
|
+
type: string;
|
|
41
|
+
}[];
|
|
33
42
|
/** The top-level declaration of the type `name` (a Rust identifier already) for `ir`. */
|
|
34
43
|
export declare function rustTopLevel(name: string, ir: IRSchema, at: RustTypeContext): string;
|