jetstream_codegen 16.0.0

Jetstream is a RPC framework for Rust, based on the 9P protocol and QUIC.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// r[impl jetstream.codegen.service.swift]

use std::fmt::Write;

use convert_case::{Case, Casing};

use crate::service_parser::{MethodDef, ServiceDef};
use crate::swift_backend::{rust_type_to_swift, SwiftConfig};

/// r[jetstream.rpc.swift.message-ids]
/// Generate Swift RPC code from a ServiceDef.
///
/// This generates:
/// - Request/response structs conforming to WireFormat
/// - Tmessage/Rmessage enums with Framer conformance
/// - Client class with async methods
/// - Handler protocol
pub fn generate_swift_rpc(
    service: &ServiceDef,
    config: &SwiftConfig,
) -> String {
    let mut out = String::new();

    writeln!(out, "import Foundation").unwrap();
    writeln!(out, "import {}", config.wireformat_module).unwrap();
    writeln!(out, "import JetStreamRpc").unwrap();
    writeln!(out).unwrap();

    let name = &service.name;

    // Message ID constants
    writeln!(out, "private let MESSAGE_ID_START: UInt8 = 102").unwrap();
    writeln!(out, "private let RERROR: UInt8 = 5").unwrap();
    writeln!(out).unwrap();

    for method in &service.methods {
        let upper = method.name.to_case(Case::UpperSnake);
        writeln!(out, "private let T{upper}: UInt8 = {}", method.request_id)
            .unwrap();
        writeln!(out, "private let R{upper}: UInt8 = {}", method.response_id)
            .unwrap();
    }
    writeln!(out).unwrap();

    // Request/response structs
    for method in &service.methods {
        generate_swift_message_types(&mut out, method);
    }

    // r[jetstream.rpc.swift.framer]
    // Tmessage enum
    generate_swift_tmessage(&mut out, &service.methods);
    writeln!(out).unwrap();

    // Rmessage enum
    generate_swift_rmessage(&mut out, &service.methods);
    writeln!(out).unwrap();

    // Protocol name and version constants
    let name_lower = name.to_lowercase();
    let version = if service.version.is_empty() {
        "0.0.0"
    } else {
        &service.version
    };
    let digest = &service.digest;
    writeln!(
        out,
        "public let PROTOCOL_NAME = \"rs.jetstream.proto/{name_lower}\"",
    )
    .unwrap();
    writeln!(
        out,
        "public let PROTOCOL_VERSION = \"rs.jetstream.proto/{name_lower}/{version}+{digest}\"",
    )
    .unwrap();
    writeln!(out).unwrap();

    // Client class
    generate_swift_client(&mut out, name, &service.methods);
    writeln!(out).unwrap();

    // Handler protocol
    generate_swift_handler(&mut out, name, &service.methods);

    out
}

fn generate_swift_message_types(out: &mut String, method: &MethodDef) {
    let pascal = method.name.to_case(Case::Pascal);

    // Request struct
    writeln!(out, "public struct T{pascal}: WireFormat {{").unwrap();
    for p in &method.params {
        let field_name = p.name.to_case(Case::Camel);
        let swift_type = rust_type_to_swift(&p.ty);
        writeln!(out, "    public var {field_name}: {swift_type}").unwrap();
    }
    writeln!(out).unwrap();

    // byteSize
    writeln!(out, "    public func byteSize() -> UInt32 {{").unwrap();
    if method.params.is_empty() {
        writeln!(out, "        return 0").unwrap();
    } else {
        let parts: Vec<String> = method
            .params
            .iter()
            .map(|p| format!("{}.byteSize()", p.name.to_case(Case::Camel)))
            .collect();
        writeln!(out, "        return {}", parts.join(" + ")).unwrap();
    }
    writeln!(out, "    }}").unwrap();
    writeln!(out).unwrap();

    // encode
    writeln!(
        out,
        "    public func encode(writer: inout BinaryWriter) throws {{"
    )
    .unwrap();
    for p in &method.params {
        let field_name = p.name.to_case(Case::Camel);
        writeln!(out, "        try {field_name}.encode(writer: &writer)")
            .unwrap();
    }
    writeln!(out, "    }}").unwrap();
    writeln!(out).unwrap();

    // decode
    writeln!(
        out,
        "    public static func decode(reader: inout BinaryReader) throws -> T{pascal} {{"
    )
    .unwrap();
    for p in &method.params {
        let field_name = p.name.to_case(Case::Camel);
        let swift_type = rust_type_to_swift(&p.ty);
        writeln!(
            out,
            "        let {field_name} = try {swift_type}.decode(reader: &reader)"
        )
        .unwrap();
    }
    let field_list: Vec<String> = method
        .params
        .iter()
        .map(|p| {
            let field_name = p.name.to_case(Case::Camel);
            format!("{field_name}: {field_name}")
        })
        .collect();
    writeln!(out, "        return T{pascal}({})", field_list.join(", "))
        .unwrap();
    writeln!(out, "    }}").unwrap();
    writeln!(out, "}}").unwrap();
    writeln!(out).unwrap();

    // Response struct
    if let Some(ret_type) = &method.return_type {
        let swift_type = rust_type_to_swift(ret_type);
        writeln!(out, "public struct R{pascal}: WireFormat {{").unwrap();
        writeln!(out, "    public var value: {swift_type}").unwrap();
        writeln!(out).unwrap();
        writeln!(out, "    public func byteSize() -> UInt32 {{").unwrap();
        writeln!(out, "        return value.byteSize()").unwrap();
        writeln!(out, "    }}").unwrap();
        writeln!(out).unwrap();
        writeln!(
            out,
            "    public func encode(writer: inout BinaryWriter) throws {{"
        )
        .unwrap();
        writeln!(out, "        try value.encode(writer: &writer)").unwrap();
        writeln!(out, "    }}").unwrap();
        writeln!(out).unwrap();
        writeln!(
            out,
            "    public static func decode(reader: inout BinaryReader) throws -> R{pascal} {{"
        )
        .unwrap();
        writeln!(
            out,
            "        let value = try {swift_type}.decode(reader: &reader)"
        )
        .unwrap();
        writeln!(out, "        return R{pascal}(value: value)").unwrap();
        writeln!(out, "    }}").unwrap();
        writeln!(out, "}}").unwrap();
    } else {
        writeln!(out, "public struct R{pascal}: WireFormat {{").unwrap();
        writeln!(out, "    public func byteSize() -> UInt32 {{ return 0 }}")
            .unwrap();
        writeln!(
            out,
            "    public func encode(writer: inout BinaryWriter) throws {{ }}"
        )
        .unwrap();
        writeln!(
            out,
            "    public static func decode(reader: inout BinaryReader) throws -> R{pascal} {{ return R{pascal}() }}"
        )
        .unwrap();
        writeln!(out, "}}").unwrap();
    }
    writeln!(out).unwrap();
}

fn generate_swift_tmessage(out: &mut String, methods: &[MethodDef]) {
    writeln!(out, "public enum Tmessage: Framer {{").unwrap();

    // Cases
    for method in methods {
        let case_name = method.name.to_case(Case::Camel);
        let pascal = method.name.to_case(Case::Pascal);
        writeln!(out, "    case {case_name}(T{pascal})").unwrap();
    }
    writeln!(out).unwrap();

    // messageType
    writeln!(out, "    public func messageType() -> UInt8 {{").unwrap();
    writeln!(out, "        switch self {{").unwrap();
    for method in methods {
        let case_name = method.name.to_case(Case::Camel);
        let upper = method.name.to_case(Case::UpperSnake);
        writeln!(out, "        case .{case_name}: return T{upper}").unwrap();
    }
    writeln!(out, "        }}").unwrap();
    writeln!(out, "    }}").unwrap();
    writeln!(out).unwrap();

    // byteSize
    writeln!(out, "    public func byteSize() -> UInt32 {{").unwrap();
    writeln!(out, "        switch self {{").unwrap();
    for method in methods {
        let case_name = method.name.to_case(Case::Camel);
        writeln!(
            out,
            "        case .{case_name}(let msg): return msg.byteSize()"
        )
        .unwrap();
    }
    writeln!(out, "        }}").unwrap();
    writeln!(out, "    }}").unwrap();
    writeln!(out).unwrap();

    // encode
    writeln!(
        out,
        "    public func encode(writer: inout BinaryWriter) throws {{"
    )
    .unwrap();
    writeln!(out, "        switch self {{").unwrap();
    for method in methods {
        let case_name = method.name.to_case(Case::Camel);
        writeln!(out, "        case .{case_name}(let msg):").unwrap();
        writeln!(out, "            try msg.encode(writer: &writer)").unwrap();
    }
    writeln!(out, "        }}").unwrap();
    writeln!(out, "    }}").unwrap();
    writeln!(out).unwrap();

    // decode
    writeln!(
        out,
        "    public static func decode(reader: inout BinaryReader, type ty: UInt8) throws -> Tmessage {{"
    )
    .unwrap();
    writeln!(out, "        switch ty {{").unwrap();
    for method in methods {
        let upper = method.name.to_case(Case::UpperSnake);
        let case_name = method.name.to_case(Case::Camel);
        let pascal = method.name.to_case(Case::Pascal);
        writeln!(
            out,
            "        case T{upper}: return .{case_name}(try T{pascal}.decode(reader: &reader))"
        )
        .unwrap();
    }
    writeln!(
        out,
        "        default: throw WireFormatError.invalidMessageType(ty)"
    )
    .unwrap();
    writeln!(out, "        }}").unwrap();
    writeln!(out, "    }}").unwrap();

    writeln!(out, "}}").unwrap();
}

fn generate_swift_rmessage(out: &mut String, methods: &[MethodDef]) {
    // r[jetstream.rpc.swift.error-frame]
    writeln!(out, "public enum Rmessage: Framer {{").unwrap();

    for method in methods {
        let case_name = method.name.to_case(Case::Camel);
        let pascal = method.name.to_case(Case::Pascal);
        writeln!(out, "    case {case_name}(R{pascal})").unwrap();
    }
    writeln!(out, "    case error(JetStreamError)").unwrap();
    writeln!(out).unwrap();

    // messageType
    writeln!(out, "    public func messageType() -> UInt8 {{").unwrap();
    writeln!(out, "        switch self {{").unwrap();
    for method in methods {
        let case_name = method.name.to_case(Case::Camel);
        let upper = method.name.to_case(Case::UpperSnake);
        writeln!(out, "        case .{case_name}: return R{upper}").unwrap();
    }
    writeln!(out, "        case .error: return RERROR").unwrap();
    writeln!(out, "        }}").unwrap();
    writeln!(out, "    }}").unwrap();
    writeln!(out).unwrap();

    // byteSize
    writeln!(out, "    public func byteSize() -> UInt32 {{").unwrap();
    writeln!(out, "        switch self {{").unwrap();
    for method in methods {
        let case_name = method.name.to_case(Case::Camel);
        writeln!(
            out,
            "        case .{case_name}(let msg): return msg.byteSize()"
        )
        .unwrap();
    }
    writeln!(out, "        case .error(let err): return err.byteSize()")
        .unwrap();
    writeln!(out, "        }}").unwrap();
    writeln!(out, "    }}").unwrap();
    writeln!(out).unwrap();

    // encode
    writeln!(
        out,
        "    public func encode(writer: inout BinaryWriter) throws {{"
    )
    .unwrap();
    writeln!(out, "        switch self {{").unwrap();
    for method in methods {
        let case_name = method.name.to_case(Case::Camel);
        writeln!(out, "        case .{case_name}(let msg):").unwrap();
        writeln!(out, "            try msg.encode(writer: &writer)").unwrap();
    }
    writeln!(out, "        case .error(let err):").unwrap();
    writeln!(out, "            try err.encode(writer: &writer)").unwrap();
    writeln!(out, "        }}").unwrap();
    writeln!(out, "    }}").unwrap();
    writeln!(out).unwrap();

    // decode
    writeln!(
        out,
        "    public static func decode(reader: inout BinaryReader, type ty: UInt8) throws -> Rmessage {{"
    )
    .unwrap();
    writeln!(out, "        switch ty {{").unwrap();
    for method in methods {
        let upper = method.name.to_case(Case::UpperSnake);
        let case_name = method.name.to_case(Case::Camel);
        let pascal = method.name.to_case(Case::Pascal);
        writeln!(
            out,
            "        case R{upper}: return .{case_name}(try R{pascal}.decode(reader: &reader))"
        )
        .unwrap();
    }
    writeln!(
        out,
        "        case RERROR: return .error(try JetStreamError.decode(reader: &reader))"
    )
    .unwrap();
    writeln!(
        out,
        "        default: throw WireFormatError.invalidMessageType(ty)"
    )
    .unwrap();
    writeln!(out, "        }}").unwrap();
    writeln!(out, "    }}").unwrap();

    writeln!(out, "}}").unwrap();
}

// r[impl jetstream.rpc.swift.client]
fn generate_swift_client(
    out: &mut String,
    service_name: &str,
    methods: &[MethodDef],
) {
    let client_name = format!("{service_name}Client");

    writeln!(out, "public class {client_name} {{").unwrap();
    writeln!(out, "    private let mux: Mux").unwrap();
    writeln!(out).unwrap();
    writeln!(out, "    public init(mux: Mux) {{").unwrap();
    writeln!(out, "        self.mux = mux").unwrap();
    writeln!(out, "    }}").unwrap();
    writeln!(out).unwrap();

    // r[impl jetstream.version.framer.client-handshake]
    // Static method to negotiate version using raw frame I/O
    writeln!(
        out,
        "    // r[impl jetstream.version.framer.client-handshake]"
    )
    .unwrap();
    writeln!(out, "    /// Perform Tversion/Rversion handshake.").unwrap();
    writeln!(
        out,
        "    /// Call before creating a Mux on the same stream."
    )
    .unwrap();
    writeln!(out, "    public static func negotiate(").unwrap();
    writeln!(out, "        writer: inout BinaryWriter,").unwrap();
    writeln!(out, "        reader: inout BinaryReader,").unwrap();
    writeln!(out, "        msize: UInt32 = 65536").unwrap();
    writeln!(out, "    ) throws -> NegotiatedVersion {{").unwrap();
    writeln!(out, "        let tversion = Tversion(msize: msize, version: PROTOCOL_VERSION)").unwrap();
    writeln!(
        out,
        "        let totalSize: UInt32 = 4 + 1 + 2 + tversion.byteSize()"
    )
    .unwrap();
    writeln!(out, "        writer.writeU32(totalSize)").unwrap();
    writeln!(out, "        writer.writeU8(TVERSION)").unwrap();
    writeln!(out, "        writer.writeU16(0xFFFF) // NOTAG").unwrap();
    writeln!(out, "        try tversion.encode(writer: &writer)").unwrap();
    writeln!(out, "        let size = try reader.readU32()").unwrap();
    writeln!(out, "        guard size >= 7 else {{ throw VersionNegotiationError.streamClosed }}").unwrap();
    writeln!(out, "        let ty = try reader.readU8()").unwrap();
    writeln!(out, "        let _ = try reader.readU16() // tag").unwrap();
    writeln!(out, "        guard ty == RVERSION else {{ throw VersionNegotiationError.unexpectedMessageType(ty) }}").unwrap();
    writeln!(
        out,
        "        let rversion = try Rversion.decode(reader: &reader)"
    )
    .unwrap();
    writeln!(out, "        guard rversion.version != \"unknown\" else {{ throw VersionNegotiationError.rejected }}").unwrap();
    writeln!(out, "        return NegotiatedVersion(msize: rversion.msize, version: rversion.version)").unwrap();
    writeln!(out, "    }}").unwrap();

    for method in methods {
        let method_name = method.name.to_case(Case::Camel);
        let pascal = method.name.to_case(Case::Pascal);

        let params: Vec<String> = method
            .params
            .iter()
            .map(|p| {
                let name = p.name.to_case(Case::Camel);
                let swift_type = rust_type_to_swift(&p.ty);
                format!("{name}: {swift_type}")
            })
            .collect();

        let ret_type = method
            .return_type
            .as_ref()
            .map(rust_type_to_swift)
            .unwrap_or_else(|| "Void".into());

        writeln!(out).unwrap();
        writeln!(
            out,
            "    public func {method_name}({}) async throws -> {ret_type} {{",
            params.join(", ")
        )
        .unwrap();

        let args: Vec<String> = method
            .params
            .iter()
            .map(|p| {
                let name = p.name.to_case(Case::Camel);
                format!("{name}: {name}")
            })
            .collect();
        writeln!(
            out,
            "        let req = Tmessage.{method_name}(T{pascal}({}))",
            args.join(", ")
        )
        .unwrap();
        writeln!(out, "        let res = try await mux.rpc(req)").unwrap();
        writeln!(out, "        switch res {{").unwrap();
        writeln!(out, "        case .{method_name}(let msg):").unwrap();
        if method.return_type.is_some() {
            writeln!(out, "            return msg.value").unwrap();
        } else {
            writeln!(out, "            return").unwrap();
        }
        writeln!(out, "        case .error(let err):").unwrap();
        writeln!(out, "            throw err").unwrap();
        writeln!(out, "        default:").unwrap();
        writeln!(
            out,
            "            throw WireFormatError.invalidMessageType(0)"
        )
        .unwrap();
        writeln!(out, "        }}").unwrap();
        writeln!(out, "    }}").unwrap();
    }

    writeln!(out, "}}").unwrap();
}

// r[impl jetstream.rpc.swift.handler]
fn generate_swift_handler(
    out: &mut String,
    service_name: &str,
    methods: &[MethodDef],
) {
    let handler_name = format!("{service_name}Handler");

    writeln!(out, "public protocol {handler_name}: Sendable {{").unwrap();
    for method in methods {
        let method_name = method.name.to_case(Case::Camel);

        let mut all_params = vec!["ctx: Context".to_string()];
        all_params.extend(method.params.iter().map(|p| {
            let name = p.name.to_case(Case::Camel);
            let swift_type = rust_type_to_swift(&p.ty);
            format!("{name}: {swift_type}")
        }));

        let ret_type = method
            .return_type
            .as_ref()
            .map(rust_type_to_swift)
            .unwrap_or_else(|| "Void".into());

        writeln!(
            out,
            "    func {method_name}({}) async throws -> {ret_type}",
            all_params.join(", ")
        )
        .unwrap();
    }
    writeln!(out, "}}").unwrap();
    writeln!(out).unwrap();

    // r[impl jetstream.rpc.swift.handler.dispatch]
    generate_swift_dispatch(out, service_name, methods);
}

fn generate_swift_dispatch(
    out: &mut String,
    service_name: &str,
    methods: &[MethodDef],
) {
    let handler_name = format!("{service_name}Handler");

    writeln!(
        out,
        "public func dispatch{service_name}<H: {handler_name}>(handler: H, ctx: Context, frame: Frame<Tmessage>) async -> Frame<Rmessage> {{"
    )
    .unwrap();
    writeln!(out, "    let tag = frame.tag").unwrap();
    writeln!(out, "    do {{").unwrap();
    writeln!(out, "        switch frame.msg {{").unwrap();

    for method in methods {
        let case_name = method.name.to_case(Case::Camel);
        let pascal = method.name.to_case(Case::Pascal);

        writeln!(out, "        case .{case_name}(let req):").unwrap();

        // Build the call arguments: ctx first, then each field from the request struct
        let mut call_args = vec!["ctx: ctx".to_string()];
        call_args.extend(method.params.iter().map(|p| {
            let name = p.name.to_case(Case::Camel);
            format!("{name}: req.{name}")
        }));

        if method.return_type.is_some() {
            writeln!(
                out,
                "            let result = try await handler.{case_name}({})",
                call_args.join(", ")
            )
            .unwrap();
            writeln!(
                out,
                "            return Frame(tag: tag, msg: .{case_name}(R{pascal}(value: result)))"
            )
            .unwrap();
        } else {
            writeln!(
                out,
                "            try await handler.{case_name}({})",
                call_args.join(", ")
            )
            .unwrap();
            writeln!(
                out,
                "            return Frame(tag: tag, msg: .{case_name}(R{pascal}()))"
            )
            .unwrap();
        }
    }

    writeln!(out, "        }}").unwrap();
    writeln!(out, "    }} catch let error as JetStreamError {{").unwrap();
    writeln!(out, "        return Frame(tag: tag, msg: .error(error))")
        .unwrap();
    writeln!(out, "    }} catch {{").unwrap();
    writeln!(
        out,
        "        let jsErr = JetStreamError(inner: ErrorInner(message: \"\\(error)\"))"
    )
    .unwrap();
    writeln!(out, "        return Frame(tag: tag, msg: .error(jsErr))")
        .unwrap();
    writeln!(out, "    }}").unwrap();
    writeln!(out, "}}").unwrap();
}