vox-codegen 0.8.2

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
//! TypeScript type generation and collection.
//!
//! This module handles:
//! - Collecting named types (structs and enums) from service definitions
//! - Generating TypeScript type definitions (interfaces, type unions)
//! - Converting Rust types to TypeScript type strings

use std::collections::HashSet;

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

fn transparent_named_alias(shape: &'static Shape) -> Option<(&'static str, &'static Shape)> {
    if !shape.is_transparent() {
        return None;
    }
    let name = extract_type_name(shape.type_identifier)?;
    let inner = shape.inner?;
    Some((name, inner))
}

fn extract_type_name(type_identifier: &'static str) -> Option<&'static str> {
    if type_identifier.is_empty()
        || type_identifier.starts_with('(')
        || type_identifier.starts_with('[')
    {
        return None;
    }
    Some(type_identifier)
}

/// Generate TypeScript field access expression.
/// Uses bracket notation for numeric field names (tuple fields), dot notation otherwise.
pub fn ts_field_access(expr: &str, field_name: &str) -> String {
    if field_name
        .chars()
        .next()
        .is_some_and(|c| c.is_ascii_digit())
    {
        format!("{expr}[{field_name}]")
    } else {
        format!("{expr}.{field_name}")
    }
}

/// Collect all named types (structs and enums with a name) from a service.
/// Returns a vector of (name, Shape) pairs in dependency order.
pub fn collect_named_types(service: &ServiceDescriptor) -> Vec<(String, &'static Shape)> {
    let mut seen = HashSet::new();
    let mut types = Vec::new();

    fn visit(
        shape: &'static Shape,
        seen: &mut HashSet<String>,
        types: &mut Vec<(String, &'static Shape)>,
    ) {
        if let Some((name, inner)) = transparent_named_alias(shape) {
            if !seen.contains(name) {
                seen.insert(name.to_string());
                visit(inner, seen, types);
                types.push((name.to_string(), shape));
            }
            return;
        }

        match classify_shape(shape) {
            ShapeKind::Struct(StructInfo {
                name: Some(name),
                fields,
                ..
            }) if seen.insert(name.to_string()) => {
                // Visit nested types first (dependencies before dependents)
                for field in fields {
                    visit(field.shape(), seen, types);
                }
                types.push((name.to_string(), shape));
            }
            ShapeKind::Enum(EnumInfo {
                name: Some(name),
                variants,
            }) if seen.insert(name.to_string()) => {
                // Visit nested types in variants
                for variant in variants {
                    match classify_variant(variant) {
                        VariantKind::Newtype { inner } => visit(inner, seen, types),
                        VariantKind::Struct { fields } | VariantKind::Tuple { fields } => {
                            for field in fields {
                                visit(field.shape(), seen, types);
                            }
                        }
                        VariantKind::Unit => {}
                    }
                }
                types.push((name.to_string(), shape));
            }
            ShapeKind::List { element } => visit(element, seen, types),
            ShapeKind::Option { inner } => visit(inner, seen, types),
            ShapeKind::Array { element, .. } => visit(element, seen, types),
            ShapeKind::Map { key, value } => {
                visit(key, seen, types);
                visit(value, seen, types);
            }
            ShapeKind::Set { element } => visit(element, seen, types),
            ShapeKind::Tuple { elements } => {
                for param in elements {
                    visit(param.shape, seen, types);
                }
            }
            ShapeKind::Tx { inner } | ShapeKind::Rx { inner } => visit(inner, seen, types),
            ShapeKind::Pointer { pointee } => visit(pointee, seen, types),
            ShapeKind::Result { ok, err } => {
                visit(ok, seen, types);
                visit(err, seen, types);
            }
            // Scalars, slices, opaque - no named types to collect
            _ => {}
        }
    }

    for method in service.methods {
        for arg in method.args {
            visit(arg.shape, &mut seen, &mut types);
        }
        visit(method.return_shape, &mut seen, &mut types);
    }

    types
}

/// Generate TypeScript type definitions for all named types.
pub fn generate_named_types(named_types: &[(String, &'static Shape)]) -> String {
    let mut out = String::new();

    if named_types.is_empty() {
        return out;
    }

    out.push_str("// Named type definitions\n");

    for (name, shape) in named_types {
        if let Some((_, inner)) = transparent_named_alias(shape) {
            out.push_str(&format!(
                "export type {name} = {};\n\n",
                ts_type_base_named(inner)
            ));
            continue;
        }

        match classify_shape(shape) {
            ShapeKind::Struct(StructInfo { fields, .. }) => {
                out.push_str(&format!("export interface {} {{\n", name));
                for field in fields {
                    out.push_str(&format!(
                        "  {}: {};\n",
                        field.name,
                        ts_type_base_named(field.shape())
                    ));
                }
                out.push_str("}\n\n");
            }
            ShapeKind::Enum(EnumInfo { variants, .. }) => {
                out.push_str(&format!("export type {} =\n", name));
                for (i, variant) in variants.iter().enumerate() {
                    let variant_type = match classify_variant(variant) {
                        VariantKind::Unit => format!("{{ tag: '{}' }}", variant.name),
                        VariantKind::Newtype { inner } => {
                            format!(
                                "{{ tag: '{}'; value: {} }}",
                                variant.name,
                                ts_type_base_named(inner)
                            )
                        }
                        VariantKind::Tuple { fields } | VariantKind::Struct { fields } => {
                            let field_strs = fields
                                .iter()
                                .map(|f| format!("{}: {}", f.name, ts_type_base_named(f.shape())))
                                .collect::<Vec<_>>()
                                .join("; ");
                            format!("{{ tag: '{}'; {} }}", variant.name, field_strs)
                        }
                    };
                    let sep = if i < variants.len() - 1 { "" } else { ";" };
                    out.push_str(&format!("  | {}{}\n", variant_type, sep));
                }
                out.push('\n');
            }
            _ => {}
        }
    }

    out
}

/// Convert Shape to TypeScript type string, using named types when available.
/// This handles container types recursively, using named types at every level.
pub fn ts_type_base_named(shape: &'static Shape) -> String {
    if let Some((name, _)) = transparent_named_alias(shape) {
        return name.to_string();
    }

    match classify_shape(shape) {
        // Named types - use the name directly
        ShapeKind::Struct(StructInfo {
            name: Some(name), ..
        }) => name.to_string(),
        ShapeKind::Enum(EnumInfo {
            name: Some(name), ..
        }) => name.to_string(),

        // Container types - recurse with ts_type_base_named
        ShapeKind::List { element } => {
            // Check for bytes first
            if is_bytes(shape) {
                return "Uint8Array".into();
            }
            // Wrap in parens if inner is an anonymous enum to avoid precedence issues
            if matches!(
                classify_shape(element),
                ShapeKind::Enum(EnumInfo { name: None, .. })
            ) {
                format!("({})[]", ts_type_base_named(element))
            } else {
                format!("{}[]", ts_type_base_named(element))
            }
        }
        ShapeKind::Option { inner } => format!("{} | null", ts_type_base_named(inner)),
        ShapeKind::Array { element, len } => format!("[{}; {}]", ts_type_base_named(element), len),
        ShapeKind::Map { key, value } => {
            format!(
                "Map<{}, {}>",
                ts_type_base_named(key),
                ts_type_base_named(value)
            )
        }
        ShapeKind::Set { element } => format!("Set<{}>", ts_type_base_named(element)),
        ShapeKind::Tuple { elements } => {
            let inner = elements
                .iter()
                .map(|p| ts_type_base_named(p.shape))
                .collect::<Vec<_>>()
                .join(", ");
            format!("[{inner}]")
        }
        ShapeKind::Tx { inner } => format!("Tx<{}>", ts_type_base_named(inner)),
        ShapeKind::Rx { inner } => format!("Rx<{}>", ts_type_base_named(inner)),

        // Anonymous structs - inline as object type
        ShapeKind::Struct(StructInfo {
            name: None, fields, ..
        }) => {
            let inner = fields
                .iter()
                .map(|f| format!("{}: {}", f.name, ts_type_base_named(f.shape())))
                .collect::<Vec<_>>()
                .join("; ");
            format!("{{ {inner} }}")
        }

        // Anonymous enums - inline as union type
        ShapeKind::Enum(EnumInfo {
            name: None,
            variants,
        }) => variants
            .iter()
            .map(|v| match classify_variant(v) {
                VariantKind::Unit => format!("{{ tag: '{}' }}", v.name),
                VariantKind::Newtype { inner } => {
                    format!(
                        "{{ tag: '{}'; value: {} }}",
                        v.name,
                        ts_type_base_named(inner)
                    )
                }
                VariantKind::Tuple { fields } | VariantKind::Struct { fields } => {
                    let field_strs = fields
                        .iter()
                        .map(|f| format!("{}: {}", f.name, ts_type_base_named(f.shape())))
                        .collect::<Vec<_>>()
                        .join("; ");
                    format!("{{ tag: '{}'; {} }}", v.name, field_strs)
                }
            })
            .collect::<Vec<_>>()
            .join(" | "),

        // Scalars and other types
        ShapeKind::Scalar(scalar) => ts_scalar_type(scalar),
        ShapeKind::Slice { element } => format!("{}[]", ts_type_base_named(element)),
        ShapeKind::Pointer { pointee } => ts_type_base_named(pointee),
        ShapeKind::Result { ok, err } => {
            format!(
                "{{ ok: true; value: {} }} | {{ ok: false; error: {} }}",
                ts_type_base_named(ok),
                ts_type_base_named(err)
            )
        }
        ShapeKind::TupleStruct { fields } => {
            let inner = fields
                .iter()
                .map(|f| ts_type_base_named(f.shape()))
                .collect::<Vec<_>>()
                .join(", ");
            format!("[{inner}]")
        }
        ShapeKind::Opaque => "unknown".into(),
    }
}

/// Convert ScalarType to TypeScript type string.
pub fn ts_scalar_type(scalar: ScalarType) -> String {
    match scalar {
        ScalarType::Bool => "boolean".into(),
        ScalarType::U8
        | ScalarType::U16
        | ScalarType::U32
        | ScalarType::I8
        | ScalarType::I16
        | ScalarType::I32
        | ScalarType::F32
        | ScalarType::F64 => "number".into(),
        ScalarType::U64
        | ScalarType::U128
        | ScalarType::I64
        | ScalarType::I128
        | ScalarType::USize
        | ScalarType::ISize => "bigint".into(),
        ScalarType::Char | ScalarType::Str | ScalarType::String | ScalarType::CowStr => {
            "string".into()
        }
        ScalarType::Unit => "void".into(),
        _ => "unknown".into(),
    }
}

/// Convert Shape to TypeScript type string for client arguments.
/// Schema is from server's perspective - no inversion needed.
/// Client passes the same types that server receives.
pub fn ts_type_client_arg(shape: &'static Shape) -> String {
    match classify_shape(shape) {
        ShapeKind::Tx { inner } => format!("Tx<{}>", ts_type_client_arg(inner)),
        ShapeKind::Rx { inner } => format!("Rx<{}>", ts_type_client_arg(inner)),
        _ => ts_type_base_named(shape),
    }
}

/// Convert Shape to TypeScript type string for client returns.
/// Schema is from server's perspective - no inversion needed.
pub fn ts_type_client_return(shape: &'static Shape) -> String {
    crate::targets::swift::types::assert_no_channels_in_return_shape(shape);
    ts_type_base_named(shape)
}

/// Convert Shape to TypeScript type string for server/handler arguments.
/// Schema is from server's perspective - no inversion needed.
/// Rx means server receives, Tx means server sends.
pub fn ts_type_server_arg(shape: &'static Shape) -> String {
    match classify_shape(shape) {
        ShapeKind::Tx { inner } => format!("Tx<{}>", ts_type_server_arg(inner)),
        ShapeKind::Rx { inner } => format!("Rx<{}>", ts_type_server_arg(inner)),
        _ => ts_type_base_named(shape),
    }
}

/// Schema is from server's perspective - no inversion needed.
pub fn ts_type_server_return(shape: &'static Shape) -> String {
    crate::targets::swift::types::assert_no_channels_in_return_shape(shape);
    ts_type_base_named(shape)
}

/// TypeScript type for user-facing type definitions.
/// Uses named types when available.
pub fn ts_type(shape: &'static Shape) -> String {
    ts_type_base_named(shape)
}

/// Check if a type can be fully encoded/decoded.
/// Channel types (Tx/Rx) are supported - they encode as channel IDs.
pub fn is_fully_supported(shape: &'static Shape) -> bool {
    match classify_shape(shape) {
        // Channel types are supported - they encode/decode as channel IDs
        ShapeKind::Tx { inner } | ShapeKind::Rx { inner } => is_fully_supported(inner),
        ShapeKind::List { element }
        | ShapeKind::Option { inner: element }
        | ShapeKind::Set { element }
        | ShapeKind::Array { element, .. }
        | ShapeKind::Slice { element } => is_fully_supported(element),
        ShapeKind::Map { key, value } => is_fully_supported(key) && is_fully_supported(value),
        ShapeKind::Tuple { elements } => elements.iter().all(|p| is_fully_supported(p.shape)),
        ShapeKind::TupleStruct { fields } => fields.iter().all(|f| is_fully_supported(f.shape())),
        ShapeKind::Struct(StructInfo { fields, .. }) => {
            fields.iter().all(|f| is_fully_supported(f.shape()))
        }
        ShapeKind::Enum(EnumInfo { variants, .. }) => {
            variants.iter().all(|v| match classify_variant(v) {
                VariantKind::Unit => true,
                VariantKind::Newtype { inner } => is_fully_supported(inner),
                VariantKind::Tuple { fields } | VariantKind::Struct { fields } => {
                    fields.iter().all(|f| is_fully_supported(f.shape()))
                }
            })
        }
        ShapeKind::Pointer { pointee } => is_fully_supported(pointee),
        ShapeKind::Scalar(_) => true,
        ShapeKind::Result { ok, err } => is_fully_supported(ok) && is_fully_supported(err),
        ShapeKind::Opaque => false,
    }
}