vox-codegen 0.4.0

Language bindings codegen for vox
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
//! Swift decoding statement generation.
//!
//! Generates Swift code that decodes values from an `inout ByteBuffer`.
//! All decode functions take `inout ByteBuffer` and advance its reader index.

use facet_core::{ScalarType, Shape};
use heck::ToLowerCamelCase;
use vox_types::{
    EnumInfo, ShapeKind, StructInfo, VariantKind, classify_shape, classify_variant, is_bytes,
};

/// Generate a Swift decode statement for a given shape.
/// Returns code that decodes from `buffer` into a variable named `var_name`.
pub fn generate_decode_stmt(shape: &'static Shape, var_name: &str, indent: &str) -> String {
    generate_decode_stmt_impl(shape, var_name, indent, "buffer")
}

/// Generate a Swift decode statement for a given shape using a custom cursor variable name.
/// (cursor_var is now ignored — kept for call-site compatibility, buffer is always `buffer`)
pub fn generate_decode_stmt_with_cursor(
    shape: &'static Shape,
    var_name: &str,
    indent: &str,
    _cursor_var: &str,
) -> String {
    generate_decode_stmt_impl(shape, var_name, indent, "buffer")
}

/// Generate a Swift decode statement from a specific data variable.
/// (data_var is now ignored — kept for call-site compatibility, buffer is always `buffer`)
pub fn generate_decode_stmt_from(
    shape: &'static Shape,
    var_name: &str,
    indent: &str,
    _data_var: &str,
) -> String {
    generate_decode_stmt_impl(shape, var_name, indent, "buffer")
}

/// Generate a Swift decode statement from a specific data variable and cursor.
/// (data_var and cursor_var are now ignored — kept for call-site compatibility)
pub fn generate_decode_stmt_from_with_cursor(
    shape: &'static Shape,
    var_name: &str,
    indent: &str,
    _data_var: &str,
    _cursor_var: &str,
) -> String {
    generate_decode_stmt_impl(shape, var_name, indent, "buffer")
}

/// Core implementation: generate a decode statement that reads from the named buffer variable.
pub fn generate_decode_stmt_with_buf(
    shape: &'static Shape,
    var_name: &str,
    indent: &str,
    buf_name: &str,
) -> String {
    generate_decode_stmt_impl(shape, var_name, indent, buf_name)
}

fn generate_decode_stmt_impl(
    shape: &'static Shape,
    var_name: &str,
    indent: &str,
    buf_name: &str,
) -> String {
    // bytes → ByteBuffer slice, presented as Data for the user-facing type
    if is_bytes(shape) {
        return format!(
            "{indent}var _{var_name}_buf = try decodeBytes(from: &{buf_name})\n{indent}let {var_name} = Data(_{var_name}_buf.readBytes(length: _{var_name}_buf.readableBytes) ?? [])\n"
        );
    }

    match classify_shape(shape) {
        ShapeKind::Scalar(scalar) => {
            let decode_fn = swift_decode_fn(scalar);
            format!("{indent}let {var_name} = try {decode_fn}(from: &{buf_name})\n")
        }
        ShapeKind::List { element }
        | ShapeKind::Slice { element }
        | ShapeKind::Array { element, .. } => {
            let inner = generate_decode_closure(element);
            format!("{indent}let {var_name} = try decodeVec(from: &{buf_name}, decoder: {inner})\n")
        }
        ShapeKind::Option { inner } => {
            let inner = generate_decode_closure(inner);
            format!(
                "{indent}let {var_name} = try decodeOption(from: &{buf_name}, decoder: {inner})\n"
            )
        }
        ShapeKind::Tuple { elements } if elements.len() == 2 => {
            let a = generate_decode_closure(elements[0].shape);
            let b = generate_decode_closure(elements[1].shape);
            format!(
                "{indent}let {var_name} = try decodeTuple2(from: &{buf_name}, decoderA: {a}, decoderB: {b})\n"
            )
        }
        ShapeKind::TupleStruct { fields } if fields.len() == 2 => {
            let a = generate_decode_closure(fields[0].shape());
            let b = generate_decode_closure(fields[1].shape());
            format!(
                "{indent}let {var_name} = try decodeTuple2(from: &{buf_name}, decoderA: {a}, decoderB: {b})\n"
            )
        }
        ShapeKind::Struct(StructInfo {
            name: Some(name),
            fields,
            ..
        }) => {
            // Named struct — decode each field then construct
            let mut out = String::new();
            for f in fields.iter() {
                let field_name = f.name.to_lower_camel_case();
                out.push_str(&generate_decode_stmt_impl(
                    f.shape(),
                    &format!("_{var_name}_{field_name}"),
                    indent,
                    buf_name,
                ));
            }
            let field_inits: Vec<String> = fields
                .iter()
                .map(|f| {
                    let field_name = f.name.to_lower_camel_case();
                    format!("{field_name}: _{var_name}_{field_name}")
                })
                .collect();
            out.push_str(&format!(
                "{indent}let {var_name} = {name}({})\n",
                field_inits.join(", ")
            ));
            out
        }
        ShapeKind::Enum(EnumInfo {
            name: Some(name),
            variants,
            ..
        }) => {
            let mut out = String::new();
            out.push_str(&format!(
                "{indent}let _{var_name}_disc = try decodeVarint(from: &{buf_name})\n"
            ));
            out.push_str(&format!("{indent}let {var_name}: {name}\n"));
            out.push_str(&format!("{indent}switch _{var_name}_disc {{\n"));
            for (i, v) in variants.iter().enumerate() {
                out.push_str(&format!("{indent}case {i}:\n"));
                let inner_indent = format!("{indent}    ");
                match classify_variant(v) {
                    VariantKind::Unit => {
                        out.push_str(&format!(
                            "{inner_indent}{var_name} = .{}\n",
                            v.name.to_lower_camel_case()
                        ));
                    }
                    VariantKind::Newtype { inner } => {
                        out.push_str(&generate_decode_stmt_impl(
                            inner,
                            &format!("_{var_name}_val"),
                            &inner_indent,
                            buf_name,
                        ));
                        out.push_str(&format!(
                            "{inner_indent}{var_name} = .{}(_{var_name}_val)\n",
                            v.name.to_lower_camel_case()
                        ));
                    }
                    VariantKind::Tuple { fields } => {
                        for (j, f) in fields.iter().enumerate() {
                            out.push_str(&generate_decode_stmt_impl(
                                f.shape(),
                                &format!("_{var_name}_f{j}"),
                                &inner_indent,
                                buf_name,
                            ));
                        }
                        let args: Vec<String> = (0..fields.len())
                            .map(|j| format!("_{var_name}_f{j}"))
                            .collect();
                        out.push_str(&format!(
                            "{inner_indent}{var_name} = .{}({})\n",
                            v.name.to_lower_camel_case(),
                            args.join(", ")
                        ));
                    }
                    VariantKind::Struct { fields } => {
                        for f in fields.iter() {
                            let field_name = f.name.to_lower_camel_case();
                            out.push_str(&generate_decode_stmt_impl(
                                f.shape(),
                                &format!("_{var_name}_{field_name}"),
                                &inner_indent,
                                buf_name,
                            ));
                        }
                        let args: Vec<String> = fields
                            .iter()
                            .map(|f| {
                                let field_name = f.name.to_lower_camel_case();
                                format!("{field_name}: _{var_name}_{field_name}")
                            })
                            .collect();
                        out.push_str(&format!(
                            "{inner_indent}{var_name} = .{}({})\n",
                            v.name.to_lower_camel_case(),
                            args.join(", ")
                        ));
                    }
                }
            }
            out.push_str(&format!("{indent}default:\n"));
            out.push_str(&format!(
                "{indent}    throw VoxError.decodeError(\"unknown enum variant\")\n"
            ));
            out.push_str(&format!("{indent}}}\n"));
            out
        }
        ShapeKind::Pointer { pointee } => {
            generate_decode_stmt_impl(pointee, var_name, indent, buf_name)
        }
        ShapeKind::Result { ok, err } => {
            let ok_type = super::types::swift_type_base(ok);
            let err_type = super::types::swift_type_base(err);
            let mut out = String::new();
            out.push_str(&format!(
                "{indent}let _{var_name}_disc = try decodeVarint(from: &{buf_name})\n"
            ));
            out.push_str(&format!(
                "{indent}let {var_name}: Result<{ok_type}, {err_type}>\n"
            ));
            out.push_str(&format!("{indent}switch _{var_name}_disc {{\n"));
            out.push_str(&format!("{indent}case 0:\n"));
            let inner_indent = format!("{indent}    ");
            out.push_str(&generate_decode_stmt_impl(
                ok,
                &format!("_{var_name}_ok"),
                &inner_indent,
                buf_name,
            ));
            out.push_str(&format!(
                "{inner_indent}{var_name} = .success(_{var_name}_ok)\n"
            ));
            out.push_str(&format!("{indent}case 1:\n"));
            out.push_str(&generate_decode_stmt_impl(
                err,
                &format!("_{var_name}_err"),
                &inner_indent,
                buf_name,
            ));
            out.push_str(&format!(
                "{inner_indent}{var_name} = .failure(_{var_name}_err)\n"
            ));
            out.push_str(&format!("{indent}default:\n"));
            out.push_str(&format!(
                "{indent}    throw VoxError.decodeError(\"invalid Result discriminant\")\n"
            ));
            out.push_str(&format!("{indent}}}\n"));
            out
        }
        _ => {
            format!("{indent}let {var_name}: Any = () // unsupported type\n")
        }
    }
}

/// Generate a Swift decode closure `(inout ByteBuffer) throws -> T` for use with
/// `decodeVec`, `decodeOption`, etc.
pub fn generate_decode_closure(shape: &'static Shape) -> String {
    if is_bytes(shape) {
        // decodeBytes returns ByteBuffer; convert to Data for user-facing type
        return "{ buf in var _b = try decodeBytes(from: &buf); return Data(_b.readBytes(length: _b.readableBytes) ?? []) }".into();
    }

    match classify_shape(shape) {
        ShapeKind::Scalar(scalar) => {
            let decode_fn = swift_decode_fn(scalar);
            format!("{{ buf in try {decode_fn}(from: &buf) }}")
        }
        ShapeKind::List { element } | ShapeKind::Slice { element } => {
            let inner = generate_decode_closure(element);
            format!("{{ buf in try decodeVec(from: &buf, decoder: {inner}) }}")
        }
        ShapeKind::Option { inner } => {
            let inner = generate_decode_closure(inner);
            format!("{{ buf in try decodeOption(from: &buf, decoder: {inner}) }}")
        }
        ShapeKind::Tuple { elements } if elements.len() == 2 => {
            let a = generate_decode_closure(elements[0].shape);
            let b = generate_decode_closure(elements[1].shape);
            format!("{{ buf in try decodeTuple2(from: &buf, decoderA: {a}, decoderB: {b}) }}")
        }
        ShapeKind::TupleStruct { fields } if fields.len() == 2 => {
            let a = generate_decode_closure(fields[0].shape());
            let b = generate_decode_closure(fields[1].shape());
            format!("{{ buf in try decodeTuple2(from: &buf, decoderA: {a}, decoderB: {b}) }}")
        }
        ShapeKind::Struct(StructInfo {
            name: Some(name),
            fields,
            ..
        }) => {
            // Named struct — inline decode all fields then construct
            let mut code = "{ buf in\n".to_string();
            for f in fields.iter() {
                let field_name = f.name.to_lower_camel_case();
                let inner = generate_decode_closure(f.shape());
                code.push_str(&format!("    let _{field_name} = try ({inner})(&buf)\n"));
            }
            let field_inits: Vec<String> = fields
                .iter()
                .map(|f| {
                    let field_name = f.name.to_lower_camel_case();
                    format!("{field_name}: _{field_name}")
                })
                .collect();
            code.push_str(&format!(
                "    return {name}({})\n}}",
                field_inits.join(", ")
            ));
            code
        }
        ShapeKind::Enum(EnumInfo {
            name: Some(name),
            variants,
            ..
        }) => {
            let mut code = format!(
                "{{ buf in\n    let disc = try decodeVarint(from: &buf)\n    let result: {name}\n    switch disc {{\n"
            );
            for (i, v) in variants.iter().enumerate() {
                code.push_str(&format!("    case {i}:\n"));
                match classify_variant(v) {
                    VariantKind::Unit => {
                        code.push_str(&format!(
                            "        result = .{}\n",
                            v.name.to_lower_camel_case()
                        ));
                    }
                    VariantKind::Newtype { inner } => {
                        let inner_closure = generate_decode_closure(inner);
                        code.push_str(&format!(
                            "        let val = try ({inner_closure})(&buf)\n        result = .{}(val)\n",
                            v.name.to_lower_camel_case()
                        ));
                    }
                    VariantKind::Tuple { fields } => {
                        for (j, f) in fields.iter().enumerate() {
                            let inner = generate_decode_closure(f.shape());
                            code.push_str(&format!("        let f{j} = try ({inner})(&buf)\n"));
                        }
                        let args: Vec<String> =
                            (0..fields.len()).map(|j| format!("f{j}")).collect();
                        code.push_str(&format!(
                            "        result = .{}({})\n",
                            v.name.to_lower_camel_case(),
                            args.join(", ")
                        ));
                    }
                    VariantKind::Struct { fields } => {
                        for f in fields.iter() {
                            let field_name = f.name.to_lower_camel_case();
                            let inner = generate_decode_closure(f.shape());
                            code.push_str(&format!(
                                "        let _{field_name} = try ({inner})(&buf)\n"
                            ));
                        }
                        let args: Vec<String> = fields
                            .iter()
                            .map(|f| {
                                let field_name = f.name.to_lower_camel_case();
                                format!("{field_name}: _{field_name}")
                            })
                            .collect();
                        code.push_str(&format!(
                            "        result = .{}({})\n",
                            v.name.to_lower_camel_case(),
                            args.join(", ")
                        ));
                    }
                }
            }
            code.push_str(
                "    default:\n        throw VoxError.decodeError(\"unknown enum variant\")\n    }\n    return result\n}",
            );
            code
        }
        ShapeKind::Pointer { pointee } => generate_decode_closure(pointee),
        _ => "{ _ in throw VoxError.decodeError(\"unsupported type\") }".into(),
    }
}

/// Generate inline decode expression — just the expression part, no `let x =`.
/// Used internally where a closure calls another closure.
pub fn generate_inline_decode(shape: &'static Shape, _data_var: &str, _offset_var: &str) -> String {
    // data_var and offset_var are ignored — we always use `buf` (the closure parameter)
    let closure = generate_decode_closure(shape);
    format!("({closure})(&buf)")
}

/// Get the Swift decode function name for a scalar type.
pub fn swift_decode_fn(scalar: ScalarType) -> &'static str {
    match scalar {
        ScalarType::Bool => "decodeBool",
        ScalarType::U8 => "decodeU8",
        ScalarType::I8 => "decodeI8",
        ScalarType::U16 => "decodeU16",
        ScalarType::I16 => "decodeI16",
        ScalarType::U32 => "decodeU32",
        ScalarType::I32 => "decodeI32",
        ScalarType::U64 | ScalarType::USize => "decodeVarint",
        ScalarType::I64 | ScalarType::ISize => "decodeI64",
        ScalarType::F32 => "decodeF32",
        ScalarType::F64 => "decodeF64",
        ScalarType::Char | ScalarType::Str | ScalarType::CowStr | ScalarType::String => {
            "decodeString"
        }
        ScalarType::Unit => "{ _ in () }",
        _ => "decodeBytes",
    }
}