csharp-rs 0.1.2

Generate C# type definitions from Rust structs and enums
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
// Rust guideline compliant 2026-02-11
//! Integration tests for `#[derive(CSharp)]` on tagged enums.

#![expect(dead_code, reason = "test enums are only used via derive macro")]

use csharp_rs::{CSharp, CSharpVersion, Config, Serializer};

// --- internally tagged: struct + unit variants ---

#[derive(CSharp)]
#[serde(tag = "type")]
enum Message {
    Request { id: String, method: String },
    Quit,
}

#[test]
fn internally_tagged_has_abstract_record() {
    let cfg = Config::default();
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.contains("public abstract record Message"),
        "missing abstract record:\n{def}"
    );
}

#[test]
fn internally_tagged_has_derived_records() {
    let cfg = Config::default();
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.contains("sealed record Request : Message"),
        "missing sealed record Request:\n{def}"
    );
    assert!(
        def.contains("sealed record Quit : Message"),
        "missing sealed record Quit:\n{def}"
    );
}

#[test]
fn internally_tagged_has_converter() {
    let cfg = Config::default();
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.contains("MessageConverter"),
        "missing MessageConverter (C# 9 uses converter path):\n{def}"
    );
    assert!(
        def.contains("[JsonConverter(typeof(MessageConverter))]"),
        "missing [JsonConverter] attribute:\n{def}"
    );
}

#[test]
fn internally_tagged_request_has_properties() {
    let cfg = Config::default();
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.contains("[JsonPropertyName(\"id\")]"),
        "missing JsonPropertyName for id:\n{def}"
    );
    assert!(
        def.contains("public string Id { get; init; }"),
        "missing Id property:\n{def}"
    );
    assert!(
        def.contains("[JsonPropertyName(\"method\")]"),
        "missing JsonPropertyName for method:\n{def}"
    );
    assert!(
        def.contains("public string Method { get; init; }"),
        "missing Method property:\n{def}"
    );
}

// --- internally tagged: newtype + unit ---

#[derive(CSharp)]
#[serde(tag = "kind")]
enum Value {
    Text(String),
    Number(f64),
    Null,
}

#[test]
fn internally_tagged_newtype_has_value_property() {
    let cfg = Config::default();
    let def = Value::csharp_definition(&cfg);
    assert!(
        def.contains("public string Value { get; init; }"),
        "Text variant should have string Value property:\n{def}"
    );
    assert!(
        def.contains("public double Value { get; init; }"),
        "Number variant should have double Value property:\n{def}"
    );
}

#[test]
fn internally_tagged_unit_has_no_properties() {
    let cfg = Config::default();
    let def = Value::csharp_definition(&cfg);
    assert!(
        def.contains("sealed record Null : Value;"),
        "Null should be a semicolon record with no body:\n{def}"
    );
}

// --- internally tagged: with rename_all ---

#[derive(CSharp)]
#[serde(tag = "type", rename_all = "camelCase")]
enum Event {
    UserLogin { user_id: String },
    SessionEnd,
}

#[test]
fn internally_tagged_rename_all_variant_names() {
    let cfg = Config::default();
    let def = Event::csharp_definition(&cfg);
    // Discriminator values (JSON names) should be camelCase.
    assert!(
        def.contains("\"userLogin\""),
        "UserLogin discriminator should be camelCase:\n{def}"
    );
    assert!(
        def.contains("\"sessionEnd\""),
        "SessionEnd discriminator should be camelCase:\n{def}"
    );
}

// --- externally tagged: struct + newtype + unit ---

#[derive(CSharp)]
enum Shape {
    Circle { radius: f64 },
    Point(String),
    Unknown,
}

#[test]
fn externally_tagged_has_abstract_record() {
    let cfg = Config::default();
    let def = Shape::csharp_definition(&cfg);
    assert!(
        def.contains("public abstract record Shape"),
        "missing abstract record Shape:\n{def}"
    );
}

#[test]
fn externally_tagged_has_converter() {
    let cfg = Config::default();
    let def = Shape::csharp_definition(&cfg);
    assert!(
        def.contains("ShapeConverter"),
        "missing ShapeConverter:\n{def}"
    );
    assert!(
        def.contains("[JsonConverter(typeof(ShapeConverter))]"),
        "missing [JsonConverter] attribute:\n{def}"
    );
}

#[test]
fn externally_tagged_has_variant_records() {
    let cfg = Config::default();
    let def = Shape::csharp_definition(&cfg);
    assert!(
        def.contains("sealed record Circle : Shape"),
        "missing Circle variant record:\n{def}"
    );
    assert!(
        def.contains("sealed record Point : Shape"),
        "missing Point variant record:\n{def}"
    );
    assert!(
        def.contains("sealed record Unknown : Shape"),
        "missing Unknown variant record:\n{def}"
    );
}

// --- adjacently tagged: struct + newtype + unit ---

#[derive(CSharp)]
#[serde(tag = "t", content = "c")]
enum Block {
    Paragraph { text: String },
    Code(String),
    Break,
}

#[test]
fn adjacently_tagged_has_converter() {
    let cfg = Config::default();
    let def = Block::csharp_definition(&cfg);
    assert!(
        def.contains("BlockConverter"),
        "missing BlockConverter:\n{def}"
    );
    assert!(
        def.contains("[JsonConverter(typeof(BlockConverter))]"),
        "missing [JsonConverter] attribute:\n{def}"
    );
}

#[test]
fn adjacently_tagged_has_abstract_record() {
    let cfg = Config::default();
    let def = Block::csharp_definition(&cfg);
    assert!(
        def.contains("public abstract record Block"),
        "missing abstract record Block:\n{def}"
    );
}

// --- untagged: struct + newtype ---

#[derive(CSharp)]
#[serde(untagged)]
enum Data {
    Text(String),
    Object { key: String, value: String },
}

#[test]
fn untagged_has_converter() {
    let cfg = Config::default();
    let def = Data::csharp_definition(&cfg);
    assert!(
        def.contains("DataConverter"),
        "missing DataConverter:\n{def}"
    );
    assert!(
        def.contains("[JsonConverter(typeof(DataConverter))]"),
        "missing [JsonConverter] attribute:\n{def}"
    );
}

#[test]
fn untagged_has_abstract_record() {
    let cfg = Config::default();
    let def = Data::csharp_definition(&cfg);
    assert!(
        def.contains("public abstract record Data"),
        "missing abstract record Data:\n{def}"
    );
}

// --- namespace override ---

#[derive(CSharp)]
#[serde(tag = "type")]
#[csharp(namespace = "Game.Network")]
enum Packet {
    Ping,
    Data { payload: Vec<u8> },
}

#[test]
fn tagged_namespace_override() {
    let cfg = Config::default();
    let def = Packet::csharp_definition(&cfg);
    assert!(
        def.contains("namespace Game.Network"),
        "missing namespace override:\n{def}"
    );
}

// --- dependencies include variant field types ---

#[test]
fn tagged_enum_dependencies() {
    let cfg = Config::default();
    let deps = Message::dependencies(&cfg);
    // Message has Request { id: String, method: String } and Quit (unit).
    // Dependencies should include "string" from the struct variant fields.
    assert!(
        deps.contains(&String::from("string")),
        "dependencies should contain 'string' from Request fields: {deps:?}"
    );
}

// --- per-variant rename + skip ---

#[derive(CSharp)]
#[serde(tag = "type")]
enum Action {
    #[serde(rename = "CLICK")]
    Click {
        x: i32,
        y: i32,
    },
    #[serde(skip)]
    Internal,
    Move {
        dx: i32,
    },
}

#[test]
fn tagged_per_variant_rename() {
    let cfg = Config::default();
    let def = Action::csharp_definition(&cfg);
    // The discriminator value for Click should be "CLICK" (from serde(rename)).
    assert!(
        def.contains("\"CLICK\""),
        "Click discriminator should be renamed to CLICK:\n{def}"
    );
}

#[test]
fn tagged_skip_variant() {
    let cfg = Config::default();
    let def = Action::csharp_definition(&cfg);
    assert!(
        !def.contains("Internal"),
        "Internal variant should be skipped:\n{def}"
    );
    // Other variants should still be present.
    assert!(
        def.contains("sealed record Click : Action"),
        "Click should be present:\n{def}"
    );
    assert!(
        def.contains("sealed record Move : Action"),
        "Move should be present:\n{def}"
    );
}

// --- auto-generated comment and using directives ---

#[test]
fn tagged_enum_has_auto_generated_comment() {
    let cfg = Config::default();
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.starts_with("// <auto-generated/>"),
        "missing auto-generated comment:\n{def}"
    );
}

#[test]
fn tagged_enum_has_using_directives() {
    let cfg = Config::default();
    let def = Message::csharp_definition(&cfg);
    // C# 9 + STJ converter path requires System, System.Text.Json, and
    // System.Text.Json.Serialization.
    assert!(
        def.contains("using System;"),
        "missing System using:\n{def}"
    );
    assert!(
        def.contains("using System.Text.Json;"),
        "missing System.Text.Json using:\n{def}"
    );
    assert!(
        def.contains("using System.Text.Json.Serialization;"),
        "missing System.Text.Json.Serialization using:\n{def}"
    );
}

#[test]
fn tagged_enum_default_namespace() {
    let cfg = Config::default();
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.contains("namespace Generated"),
        "missing default namespace:\n{def}"
    );
}

// --- rename_all applied to struct variant field names ---

#[test]
fn internally_tagged_rename_all_field_names() {
    let cfg = Config::default();
    let def = Event::csharp_definition(&cfg);
    // user_id with camelCase rename_all should have JSON name "userId".
    assert!(
        def.contains("[JsonPropertyName(\"userId\")]"),
        "user_id field should be renamed to camelCase 'userId':\n{def}"
    );
    // C# property should be PascalCase.
    assert!(
        def.contains("public string UserId { get; init; }"),
        "C# property should be PascalCase UserId:\n{def}"
    );
}

// --- externally tagged variant with properties ---

#[test]
fn externally_tagged_circle_has_radius_property() {
    let cfg = Config::default();
    let def = Shape::csharp_definition(&cfg);
    assert!(
        def.contains("[JsonPropertyName(\"radius\")]"),
        "Circle should have JsonPropertyName for radius:\n{def}"
    );
    assert!(
        def.contains("public double Radius { get; init; }"),
        "Circle should have double Radius property:\n{def}"
    );
}

#[test]
fn externally_tagged_point_has_value_property() {
    let cfg = Config::default();
    let def = Shape::csharp_definition(&cfg);
    assert!(
        def.contains("sealed record Point : Shape"),
        "Point should be a sealed record:\n{def}"
    );
    // Newtype variant has a "Value" property.
    assert!(
        def.contains("public string Value { get; init; }"),
        "Point (newtype) should have string Value property:\n{def}"
    );
}

// --- adjacently tagged variant details ---

#[test]
fn adjacently_tagged_paragraph_has_text_property() {
    let cfg = Config::default();
    let def = Block::csharp_definition(&cfg);
    assert!(
        def.contains("[JsonPropertyName(\"text\")]"),
        "Paragraph should have JsonPropertyName for text:\n{def}"
    );
    assert!(
        def.contains("public string Text { get; init; }"),
        "Paragraph should have string Text property:\n{def}"
    );
}

#[test]
fn adjacently_tagged_unit_has_no_body() {
    let cfg = Config::default();
    let def = Block::csharp_definition(&cfg);
    assert!(
        def.contains("sealed record Break : Block;"),
        "Break should be a semicolon record with no body:\n{def}"
    );
}

// --- untagged variant details ---

#[test]
fn untagged_has_variant_records() {
    let cfg = Config::default();
    let def = Data::csharp_definition(&cfg);
    assert!(
        def.contains("sealed record Text : Data"),
        "missing Text variant record:\n{def}"
    );
    assert!(
        def.contains("sealed record Object : Data"),
        "missing Object variant record:\n{def}"
    );
}

#[test]
fn untagged_object_has_properties() {
    let cfg = Config::default();
    let def = Data::csharp_definition(&cfg);
    assert!(
        def.contains("[JsonPropertyName(\"key\")]"),
        "Object should have JsonPropertyName for key:\n{def}"
    );
    assert!(
        def.contains("public string Key { get; init; }"),
        "Object should have string Key property:\n{def}"
    );
    assert!(
        def.contains("[JsonPropertyName(\"value\")]"),
        "Object should have JsonPropertyName for value:\n{def}"
    );
    assert!(
        def.contains("public string Value { get; init; }"),
        "Object should have string Value property:\n{def}"
    );
}

// --- multi-config: serializer switching ---

#[test]
fn tagged_enum_newtonsoft_has_newtonsoft_converter() {
    let cfg = Config::default().with_serializer(Serializer::Newtonsoft);
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.contains("using Newtonsoft.Json;"),
        "Newtonsoft tagged enum should have Newtonsoft.Json using:\n{def}"
    );
    assert!(
        def.contains("JObject.Load"),
        "Newtonsoft converter should use JObject.Load:\n{def}"
    );
}

#[test]
fn tagged_enum_stj_csharp9_uses_converter() {
    let cfg = Config::default();
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.contains("MessageConverter"),
        "C# 9 + STJ should use converter path:\n{def}"
    );
    assert!(
        def.contains("JsonDocument.ParseValue"),
        "C# 9 + STJ converter should use JsonDocument:\n{def}"
    );
}

// --- multi-config: C# 11 native polymorphism ---

#[test]
fn tagged_enum_csharp11_stj_uses_native_polymorphism() {
    let cfg = Config::default().with_target(CSharpVersion::CSharp11);
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.contains("[JsonPolymorphic"),
        "C# 11 + STJ should use native [JsonPolymorphic]:\n{def}"
    );
    assert!(
        def.contains("[JsonDerivedType"),
        "C# 11 + STJ should use [JsonDerivedType]:\n{def}"
    );
    assert!(
        !def.contains("MessageConverter"),
        "C# 11 + STJ should NOT generate converter class:\n{def}"
    );
}

#[test]
fn tagged_enum_csharp11_newtonsoft_still_uses_converter() {
    let cfg = Config::default()
        .with_serializer(Serializer::Newtonsoft)
        .with_target(CSharpVersion::CSharp11);
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.contains("MessageConverter"),
        "C# 11 + Newtonsoft should still use converter:\n{def}"
    );
    assert!(
        !def.contains("JsonPolymorphic"),
        "Newtonsoft should NOT use [JsonPolymorphic]:\n{def}"
    );
}

// --- multi-config: C# version switching ---

#[test]
fn tagged_enum_csharp10_file_scoped_namespace() {
    let cfg = Config::default().with_target(CSharpVersion::CSharp10);
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.contains("namespace Generated;"),
        "C# 10 tagged enum should use file-scoped namespace:\n{def}"
    );
}

#[test]
fn tagged_enum_csharp11_has_required_modifier() {
    let cfg = Config::default().with_target(CSharpVersion::CSharp11);
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.contains("public required string Id"),
        "C# 11 tagged enum struct variant should have required modifier:\n{def}"
    );
}

// --- rename_all_fields applies to variant struct fields independently ---

#[derive(CSharp)]
#[serde(
    tag = "type",
    rename_all = "UPPERCASE",
    rename_all_fields = "camelCase"
)]
enum ApiEvent {
    UserLogin {
        user_name: String,
        login_time: String,
    },
}

#[test]
fn rename_all_fields_applies_to_variant_fields_independently() {
    let cfg = Config::default();
    let def = ApiEvent::csharp_definition(&cfg);
    // Variant discriminator should use rename_all (UPPERCASE): "USERLOGIN"
    // UPPERCASE joins words without separator per serde convention.
    assert!(
        def.contains("\"USERLOGIN\""),
        "variant discriminator should use rename_all UPPERCASE:\n{def}"
    );
    // Field names should use rename_all_fields (camelCase): "userName", "loginTime"
    assert!(
        def.contains("\"userName\""),
        "field should use rename_all_fields camelCase:\n{def}"
    );
    assert!(
        def.contains("\"loginTime\""),
        "field should use rename_all_fields camelCase:\n{def}"
    );
}

#[test]
fn rename_all_fields_does_not_affect_variant_names() {
    let cfg = Config::default();
    let def = ApiEvent::csharp_definition(&cfg);
    // C# type name stays PascalCase regardless.
    assert!(
        def.contains("sealed record UserLogin : ApiEvent"),
        "C# variant name should remain PascalCase:\n{def}"
    );
}

// --- Unity target ---

#[test]
fn unity_tagged_enum_generates_class_not_record() {
    let cfg = Config::default().with_target(CSharpVersion::Unity);
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.contains("public abstract class Message"),
        "Unity tagged enum should generate abstract class:\n{def}"
    );
    assert!(
        !def.contains("abstract record"),
        "Unity should NOT generate abstract record:\n{def}"
    );
}

#[test]
fn unity_tagged_enum_variants_use_class() {
    let cfg = Config::default().with_target(CSharpVersion::Unity);
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.contains("sealed class Request : Message"),
        "Unity variant should be sealed class:\n{def}"
    );
    assert!(
        def.contains("sealed class Quit : Message"),
        "Unity unit variant should be sealed class:\n{def}"
    );
}

#[test]
fn unity_tagged_enum_uses_get_set() {
    let cfg = Config::default().with_target(CSharpVersion::Unity);
    let def = Message::csharp_definition(&cfg);
    assert!(
        def.contains("{ get; set; }"),
        "Unity tagged enum should use get; set;:\n{def}"
    );
    assert!(
        !def.contains("get; init;"),
        "Unity should NOT use get; init;:\n{def}"
    );
}

#[test]
fn unity_tagged_enum_no_required_modifier() {
    let cfg = Config::default().with_target(CSharpVersion::Unity);
    let def = Message::csharp_definition(&cfg);
    assert!(
        !def.contains("required"),
        "Unity should NOT have required modifier:\n{def}"
    );
}