boltffi_bindgen 0.24.1

Code generation library for BoltFFI - generates Swift, Kotlin, and TypeScript bindings
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
//! Askama template bindings: one struct per `.txt` template, each
//! borrowing the plan node it renders. Templates only interpolate.
//! All branching and decision logic lives upstream in `lower`.
//!
//! Snapshot tests pin the rendered shape against curated plan
//! fixtures, so a template change surfaces as a single `.snap` diff
//! rather than rippling across emit-level tests.

use askama::Template;

use super::plan::{CSharpEnum, CSharpModule, CSharpRecord};

/// Renders the file header: auto-generated comment, `using` directives,
/// and namespace declaration.
#[derive(Template)]
#[template(path = "render_csharp/preamble.txt", escape = "none")]
pub struct PreambleTemplate<'a> {
    pub module: &'a CSharpModule,
}

/// Renders the public static wrapper class with methods that delegate
/// to the native P/Invoke declarations.
#[derive(Template)]
#[template(path = "render_csharp/functions.txt", escape = "none")]
pub struct FunctionsTemplate<'a> {
    pub module: &'a CSharpModule,
}

/// Renders the `NativeMethods` static class containing `[DllImport]`
/// declarations for the C FFI functions.
#[derive(Template)]
#[template(path = "render_csharp/native.txt", escape = "none")]
pub struct NativeTemplate<'a> {
    pub module: &'a CSharpModule,
}

/// Renders a single record as a standalone `.cs` file. Each record becomes
/// a `readonly record struct`, with a `[StructLayout(Sequential)]`
/// attribute for blittable records (passed directly across P/Invoke) and
/// wire encode/decode helpers for the wire-encoded path.
#[derive(Template)]
#[template(path = "render_csharp/record.txt", escape = "none")]
pub struct RecordTemplate<'a> {
    pub record: &'a CSharpRecord,
    pub namespace: &'a str,
}

/// Renders a single C-style enum as a standalone `.cs` file: the native
/// `public enum` declaration plus the `*Wire` static helper class that
/// supplies `Decode` and the `WireEncodeTo` extension method. The enum
/// itself passes across P/Invoke as its declared integral backing type;
/// the wire helpers exist so records and data-enum variants embedding the
/// enum can stay on the same `this.Field.WireEncodeTo(wire)` call shape
/// as records.
#[derive(Template)]
#[template(path = "render_csharp/enum_c_style.txt", escape = "none")]
pub struct EnumCStyleTemplate<'a> {
    pub enumeration: &'a CSharpEnum,
    pub namespace: &'a str,
}

/// Renders a data enum as an `abstract record` with nested `sealed record`
/// variants. Closed hierarchy (private constructor), value equality per
/// variant, and pattern-matching wire codec using switch expressions for
/// the pure paths and a switch statement for the side-effecting encode.
#[derive(Template)]
#[template(path = "render_csharp/enum_data.txt", escape = "none")]
pub struct EnumDataTemplate<'a> {
    pub enumeration: &'a CSharpEnum,
    pub namespace: &'a str,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::render::csharp::plan::{
        CSharpEnum, CSharpEnumKind, CSharpEnumVariant, CSharpMethod, CSharpParam, CSharpParamKind,
        CSharpReceiver, CSharpRecord, CSharpRecordField, CSharpReturnKind, CSharpType,
    };

    fn record_field(
        name: &str,
        csharp_type: CSharpType,
        decode: &str,
        size: &str,
        encode: &str,
    ) -> CSharpRecordField {
        CSharpRecordField {
            name: name.to_string(),
            csharp_type,
            wire_decode_expr: decode.to_string(),
            wire_size_expr: size.to_string(),
            wire_encode_expr: encode.to_string(),
        }
    }

    /// Point: the canonical blittable record. Two f64 fields, `#[repr(C)]`
    /// in Rust. Carries `[StructLayout(Sequential)]` and still emits wire
    /// helpers so it can be embedded inside a non-blittable record's
    /// wire encode/decode path without a second code shape.
    #[test]
    fn snapshot_blittable_record_point() {
        let record = CSharpRecord {
            class_name: "Point".to_string(),
            is_blittable: true,
            fields: vec![
                record_field(
                    "X",
                    CSharpType::Double,
                    "reader.ReadF64()",
                    "8",
                    "wire.WriteF64(this.X)",
                ),
                record_field(
                    "Y",
                    CSharpType::Double,
                    "reader.ReadF64()",
                    "8",
                    "wire.WriteF64(this.Y)",
                ),
            ],
        };
        let template = RecordTemplate {
            record: &record,
            namespace: "Demo",
        };
        insta::assert_snapshot!(template.render().unwrap());
    }

    /// Person: the canonical non-blittable record: a string field (which
    /// forces the wire path) plus a primitive. No StructLayout attribute.
    /// Imports `System.Text` because the size expression uses
    /// `Encoding.UTF8.GetByteCount`.
    #[test]
    fn snapshot_non_blittable_record_person_with_string() {
        let record = CSharpRecord {
            class_name: "Person".to_string(),
            is_blittable: false,
            fields: vec![
                record_field(
                    "Name",
                    CSharpType::String,
                    "reader.ReadString()",
                    "(4 + Encoding.UTF8.GetByteCount(this.Name))",
                    "wire.WriteString(this.Name)",
                ),
                record_field(
                    "Age",
                    CSharpType::UInt,
                    "reader.ReadU32()",
                    "4",
                    "wire.WriteU32(this.Age)",
                ),
            ],
        };
        let template = RecordTemplate {
            record: &record,
            namespace: "Demo",
        };
        insta::assert_snapshot!(template.render().unwrap());
    }

    /// Line: a record whose fields are themselves records. The decode
    /// expression for a record-typed field is `Point.Decode(reader)` and
    /// the encode is `this.Start.WireEncodeTo(wire)`, the recursive
    /// glue that lets records compose.
    #[test]
    fn snapshot_nested_record_line() {
        let record = CSharpRecord {
            class_name: "Line".to_string(),
            is_blittable: false,
            fields: vec![
                record_field(
                    "Start",
                    CSharpType::Record("Point".to_string()),
                    "Point.Decode(reader)",
                    "16",
                    "this.Start.WireEncodeTo(wire)",
                ),
                record_field(
                    "End",
                    CSharpType::Record("Point".to_string()),
                    "Point.Decode(reader)",
                    "16",
                    "this.End.WireEncodeTo(wire)",
                ),
            ],
        };
        let template = RecordTemplate {
            record: &record,
            namespace: "Demo",
        };
        insta::assert_snapshot!(template.render().unwrap());
    }

    /// A fieldless record: the template must still produce valid C#.
    /// `WireEncodedSize` returns 0 and `WireEncodeTo` is an empty method.
    #[test]
    fn snapshot_empty_record() {
        let record = CSharpRecord {
            class_name: "Unit".to_string(),
            is_blittable: true,
            fields: vec![],
        };
        let template = RecordTemplate {
            record: &record,
            namespace: "Demo",
        };
        insta::assert_snapshot!(template.render().unwrap());
    }

    /// Flag: the canonical "blittable record with a C-style enum field."
    /// Status is an `enum : int` here, so embedding it alongside a `uint`
    /// keeps the record on the zero-copy P/Invoke path with
    /// `[StructLayout(Sequential)]`. The wire helpers are still emitted
    /// so non-blittable records that embed `Flag` can reach its
    /// wire encoder without a second rendering shape.
    #[test]
    fn snapshot_blittable_record_with_cstyle_enum_field() {
        let record = CSharpRecord {
            class_name: "Flag".to_string(),
            is_blittable: true,
            fields: vec![
                record_field(
                    "Status",
                    CSharpType::CStyleEnum("Status".to_string()),
                    "StatusWire.Decode(reader)",
                    "4",
                    "this.Status.WireEncodeTo(wire)",
                ),
                record_field(
                    "Count",
                    CSharpType::UInt,
                    "reader.ReadU32()",
                    "4",
                    "wire.WriteU32(this.Count)",
                ),
            ],
        };
        let template = RecordTemplate {
            record: &record,
            namespace: "Demo",
        };
        insta::assert_snapshot!(template.render().unwrap());
    }

    fn method(
        owner_class_name: &str,
        name: &str,
        ffi_name: &str,
        receiver: CSharpReceiver,
        params: Vec<CSharpParam>,
        return_type: CSharpType,
        return_kind: CSharpReturnKind,
    ) -> CSharpMethod {
        CSharpMethod {
            name: name.to_string(),
            native_method_name: format!("{owner_class_name}{name}"),
            ffi_name: ffi_name.to_string(),
            receiver,
            params,
            return_type,
            return_kind,
            wire_writers: vec![],
        }
    }

    fn param(name: &str, csharp_type: CSharpType) -> CSharpParam {
        CSharpParam {
            name: name.to_string(),
            csharp_type,
            kind: CSharpParamKind::Direct,
        }
    }

    /// Direction: C-style enum with a mix of static factories and
    /// instance methods. Renders alongside the `DirectionWire` helper
    /// plus a `DirectionMethods` companion static class; instance
    /// methods become C# extension methods (`this Direction self`) so
    /// `d.Opposite()` works without members on the enum itself.
    #[test]
    fn snapshot_c_style_enum_with_methods_direction() {
        let enumeration = CSharpEnum {
            class_name: "Direction".to_string(),
            kind: CSharpEnumKind::CStyle,
            c_style_tag_type: Some(crate::ir::types::PrimitiveType::I32),
            variants: vec![
                CSharpEnumVariant {
                    name: "North".to_string(),
                    tag: 0,
                    wire_tag: 0,
                    fields: vec![],
                },
                CSharpEnumVariant {
                    name: "South".to_string(),
                    tag: 1,
                    wire_tag: 1,
                    fields: vec![],
                },
                CSharpEnumVariant {
                    name: "East".to_string(),
                    tag: 2,
                    wire_tag: 2,
                    fields: vec![],
                },
                CSharpEnumVariant {
                    name: "West".to_string(),
                    tag: 3,
                    wire_tag: 3,
                    fields: vec![],
                },
            ],
            methods: vec![
                method(
                    "Direction",
                    "FromDegrees",
                    "boltffi_direction_from_degrees",
                    CSharpReceiver::Static,
                    vec![param("degrees", CSharpType::Double)],
                    CSharpType::CStyleEnum("Direction".to_string()),
                    CSharpReturnKind::Direct,
                ),
                method(
                    "Direction",
                    "Count",
                    "boltffi_direction_count",
                    CSharpReceiver::Static,
                    vec![],
                    CSharpType::UInt,
                    CSharpReturnKind::Direct,
                ),
                method(
                    "Direction",
                    "Opposite",
                    "boltffi_direction_opposite",
                    CSharpReceiver::InstanceExtension,
                    vec![],
                    CSharpType::CStyleEnum("Direction".to_string()),
                    CSharpReturnKind::Direct,
                ),
                method(
                    "Direction",
                    "IsHorizontal",
                    "boltffi_direction_is_horizontal",
                    CSharpReceiver::InstanceExtension,
                    vec![],
                    CSharpType::Bool,
                    CSharpReturnKind::Direct,
                ),
                method(
                    "Direction",
                    "Label",
                    "boltffi_direction_label",
                    CSharpReceiver::InstanceExtension,
                    vec![],
                    CSharpType::String,
                    CSharpReturnKind::WireDecodeString,
                ),
            ],
        };
        let template = EnumCStyleTemplate {
            enumeration: &enumeration,
            namespace: "Demo",
        };
        insta::assert_snapshot!(template.render().unwrap());
    }

    /// Status: the canonical C-style enum. Three unit variants with
    /// ordinal-index tags. Renders as a native `public enum : int` plus
    /// the `StatusWire` static helper with the `WireEncodeTo` extension.
    #[test]
    fn snapshot_c_style_enum_status() {
        let enumeration = CSharpEnum {
            class_name: "Status".to_string(),
            kind: CSharpEnumKind::CStyle,
            c_style_tag_type: Some(crate::ir::types::PrimitiveType::I32),
            variants: vec![
                CSharpEnumVariant {
                    name: "Active".to_string(),
                    tag: 0,
                    wire_tag: 0,
                    fields: vec![],
                },
                CSharpEnumVariant {
                    name: "Inactive".to_string(),
                    tag: 1,
                    wire_tag: 1,
                    fields: vec![],
                },
                CSharpEnumVariant {
                    name: "Pending".to_string(),
                    tag: 2,
                    wire_tag: 2,
                    fields: vec![],
                },
            ],
            methods: vec![],
        };
        let template = EnumCStyleTemplate {
            enumeration: &enumeration,
            namespace: "Demo",
        };
        insta::assert_snapshot!(template.render().unwrap());
    }

    /// LogLevel: a non-default C-style enum backing type. The C# surface
    /// must preserve the `byte` backing type so direct P/Invoke matches
    /// Rust's `#[repr(u8)]`, and the wire helper must use the 1-byte read /
    /// write ops rather than hard-coding `I32`.
    #[test]
    fn snapshot_c_style_enum_log_level_u8() {
        let enumeration = CSharpEnum {
            class_name: "LogLevel".to_string(),
            kind: CSharpEnumKind::CStyle,
            c_style_tag_type: Some(crate::ir::types::PrimitiveType::U8),
            variants: vec![
                CSharpEnumVariant {
                    name: "Trace".to_string(),
                    tag: 0,
                    wire_tag: 0,
                    fields: vec![],
                },
                CSharpEnumVariant {
                    name: "Debug".to_string(),
                    tag: 1,
                    wire_tag: 1,
                    fields: vec![],
                },
                CSharpEnumVariant {
                    name: "Info".to_string(),
                    tag: 2,
                    wire_tag: 2,
                    fields: vec![],
                },
                CSharpEnumVariant {
                    name: "Warn".to_string(),
                    tag: 3,
                    wire_tag: 3,
                    fields: vec![],
                },
                CSharpEnumVariant {
                    name: "Error".to_string(),
                    tag: 4,
                    wire_tag: 4,
                    fields: vec![],
                },
            ],
            methods: vec![],
        };
        let template = EnumCStyleTemplate {
            enumeration: &enumeration,
            namespace: "Demo",
        };
        insta::assert_snapshot!(template.render().unwrap());
    }

    /// Shape: the canonical data enum exercising every payload shape:
    /// a single-field variant (Circle), a multi-field variant (Rectangle),
    /// and a unit variant (Point). Renders as `abstract record Shape`
    /// with nested `sealed record` variants, switch-expression Decode,
    /// and pattern-match WireEncodeTo. Field wire expressions reference
    /// the switch-bound local `_v`, not `this`.
    #[test]
    fn snapshot_data_enum_shape() {
        let enumeration = CSharpEnum {
            class_name: "Shape".to_string(),
            kind: CSharpEnumKind::Data,
            c_style_tag_type: None,
            variants: vec![
                CSharpEnumVariant {
                    name: "Circle".to_string(),
                    tag: 0,
                    wire_tag: 0,
                    fields: vec![record_field(
                        "Radius",
                        CSharpType::Double,
                        "reader.ReadF64()",
                        "8",
                        "wire.WriteF64(_v.Radius)",
                    )],
                },
                CSharpEnumVariant {
                    name: "Rectangle".to_string(),
                    tag: 1,
                    wire_tag: 1,
                    fields: vec![
                        record_field(
                            "Width",
                            CSharpType::Double,
                            "reader.ReadF64()",
                            "8",
                            "wire.WriteF64(_v.Width)",
                        ),
                        record_field(
                            "Height",
                            CSharpType::Double,
                            "reader.ReadF64()",
                            "8",
                            "wire.WriteF64(_v.Height)",
                        ),
                    ],
                },
                CSharpEnumVariant {
                    name: "Point".to_string(),
                    tag: 2,
                    wire_tag: 2,
                    fields: vec![],
                },
            ],
            methods: vec![],
        };
        let template = EnumDataTemplate {
            enumeration: &enumeration,
            namespace: "Demo",
        };
        insta::assert_snapshot!(template.render().unwrap());
    }

    /// Shape with methods: a data enum carrying both static factories
    /// (UnitCircle, VariantCount) and instance methods (Area, Describe).
    /// Methods render inside the abstract record body. Instance ones
    /// wire-encode `this` into `_selfBytes` before the native call.
    #[test]
    fn snapshot_data_enum_with_methods_shape() {
        let enumeration = CSharpEnum {
            class_name: "Shape".to_string(),
            kind: CSharpEnumKind::Data,
            c_style_tag_type: None,
            variants: vec![CSharpEnumVariant {
                name: "Circle".to_string(),
                tag: 0,
                wire_tag: 0,
                fields: vec![record_field(
                    "Radius",
                    CSharpType::Double,
                    "reader.ReadF64()",
                    "8",
                    "wire.WriteF64(_v.Radius)",
                )],
            }],
            methods: vec![
                method(
                    "Shape",
                    "UnitCircle",
                    "boltffi_shape_unit_circle",
                    CSharpReceiver::Static,
                    vec![],
                    CSharpType::DataEnum("Shape".to_string()),
                    CSharpReturnKind::WireDecodeObject {
                        class_name: "Shape".to_string(),
                    },
                ),
                method(
                    "Shape",
                    "VariantCount",
                    "boltffi_shape_variant_count",
                    CSharpReceiver::Static,
                    vec![],
                    CSharpType::UInt,
                    CSharpReturnKind::Direct,
                ),
                method(
                    "Shape",
                    "Area",
                    "boltffi_shape_area",
                    CSharpReceiver::InstanceNative,
                    vec![],
                    CSharpType::Double,
                    CSharpReturnKind::Direct,
                ),
                method(
                    "Shape",
                    "Describe",
                    "boltffi_shape_describe",
                    CSharpReceiver::InstanceNative,
                    vec![],
                    CSharpType::String,
                    CSharpReturnKind::WireDecodeString,
                ),
            ],
        };
        let template = EnumDataTemplate {
            enumeration: &enumeration,
            namespace: "Demo",
        };
        insta::assert_snapshot!(template.render().unwrap());
    }
}