facet_generate 0.16.0

Generate Swift, Kotlin and TypeScript from types annotated with `#[derive(Facet)]`
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
//! Snapshot tests for the Swift emitter — **no serialization**.
//!
//! Each test defines one or more Rust types annotated with `#[derive(Facet)]`,
//! runs them through the [`emit!`] macro with `Encoding::None`, and asserts the
//! generated Swift source against an [`insta`] inline snapshot.
//!
//! Because encoding is `None`, the output contains only plain type declarations
//! (`public struct`, `indirect public enum`) with no `serialize`/`deserialize`
//! methods and no `Hashable` conformance.
//!
//! # Coverage
//!
//! | Category | What is tested |
//! |----------|----------------|
//! | Structs | Unit structs (with/without fields), newtype wrappers, tuple structs, structs with primitive and user-defined fields |
//! | Tuples | 2-tuple, 3-tuple, 4-tuple (native Swift tuples) |
//! | Enums | Unit variants, newtype/tuple variants, struct variants, mixed-variant enums |
//! | Collections | `Vec`, `HashMap`/`BTreeMap`, `HashSet`/`BTreeSet`, fixed-size arrays |
//! | Optional | `Option<T>` fields (mapped to `T?`) |
//! | Pointers | `Box`, `Rc`, `Arc` (all transparent in generated output) |
//! | Bytes | `#[facet(bytes)]`-annotated `Vec<u8>` and byte slices |
//! | Modules | Multi-module generation via [`emit_two_modules!`] |

#![allow(clippy::too_many_lines)]

use std::{
    collections::{BTreeMap, BTreeSet, HashMap, HashSet},
    rc::Rc,
    sync::Arc,
};

use crate as fg;
use facet::Facet;

use super::*;
use crate::{emit, emit_two_modules, generation::swift::CodeGenerator};

#[test]
fn unit_struct_1() {
    /// line 1
    #[derive(Facet)]
    /// line 2
    struct UnitStruct;

    let actual = emit!(UnitStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    /// line 1
    /// line 2
    public struct UnitStruct {
        public init() {
        }
    }
    ");
}

#[test]
fn unit_struct_2() {
    /// line 1
    #[derive(Facet)]
    /// line 2
    struct UnitStruct {}

    let actual = emit!(UnitStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    /// line 1
    /// line 2
    public struct UnitStruct {
        public init() {
        }
    }
    ");
}

#[test]
fn newtype_struct() {
    /// line 1
    #[derive(Facet)]
    /// line 2
    struct NewType(String);

    let actual = emit!(NewType as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    /// line 1
    /// line 2
    public struct NewType {
        public var value: String

        public init(value: String) {
            self.value = value
        }
    }
    ");
}

#[test]
fn tuple_struct() {
    /// line 1
    #[derive(Facet)]
    /// line 2
    struct TupleStruct(String, i32);

    let actual = emit!(TupleStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    /// line 1
    /// line 2
    public struct TupleStruct {
        public var field0: String
        public var field1: Int32

        public init(field0: String, field1: Int32) {
            self.field0 = field0
            self.field1 = field1
        }
    }
    ");
}

#[test]
fn struct_with_fields_of_primitive_types() {
    /// line 1
    #[derive(Facet)]
    /// line 2
    struct StructWithFields {
        /// unit type
        unit: (),
        /// boolean
        bool: bool,
        i8: i8,
        i16: i16,
        i32: i32,
        i64: i64,
        i128: i128,
        u8: u8,
        u16: u16,
        u32: u32,
        u64: u64,
        u128: u128,
        f32: f32,
        f64: f64,
        char: char,
        string: String,
    }

    let actual = emit!(StructWithFields as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    /// line 1
    /// line 2
    public struct StructWithFields {
        /// unit type
        public var unit: Void
        /// boolean
        public var bool: Bool
        public var i8: Int8
        public var i16: Int16
        public var i32: Int32
        public var i64: Int64
        public var i128: Int128
        public var u8: UInt8
        public var u16: UInt16
        public var u32: UInt32
        public var u64: UInt64
        public var u128: UInt128
        public var f32: Float
        public var f64: Double
        public var char: Character
        public var string: String

        public init(unit: Void, bool: Bool, i8: Int8, i16: Int16, i32: Int32, i64: Int64, i128: Int128, u8: UInt8, u16: UInt16, u32: UInt32, u64: UInt64, u128: UInt128, f32: Float, f64: Double, char: Character, string: String) {
            self.unit = unit
            self.bool = bool
            self.i8 = i8
            self.i16 = i16
            self.i32 = i32
            self.i64 = i64
            self.i128 = i128
            self.u8 = u8
            self.u16 = u16
            self.u32 = u32
            self.u64 = u64
            self.u128 = u128
            self.f32 = f32
            self.f64 = f64
            self.char = char
            self.string = string
        }
    }
    ");
}

#[test]
fn struct_with_fields_of_user_types() {
    #[derive(Facet)]
    struct Inner1 {
        field1: String,
    }

    #[derive(Facet)]
    struct Inner2(String);

    #[derive(Facet)]
    struct Inner3(String, i32);

    #[derive(Facet)]
    struct Outer {
        one: Inner1,
        two: Inner2,
        three: Inner3,
    }

    let actual = emit!(Outer as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct Inner1 {
        public var field1: String

        public init(field1: String) {
            self.field1 = field1
        }
    }

    public struct Inner2 {
        public var value: String

        public init(value: String) {
            self.value = value
        }
    }

    public struct Inner3 {
        public var field0: String
        public var field1: Int32

        public init(field0: String, field1: Int32) {
            self.field0 = field0
            self.field1 = field1
        }
    }

    public struct Outer {
        public var one: Inner1
        public var two: Inner2
        public var three: Inner3

        public init(one: Inner1, two: Inner2, three: Inner3) {
            self.one = one
            self.two = two
            self.three = three
        }
    }
    ");
}

#[test]
fn struct_with_field_that_is_a_2_tuple() {
    #[derive(Facet)]
    struct MyStruct {
        one: (String, i32),
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var one: (String, Int32)

        public init(one: (String, Int32)) {
            self.one = one
        }
    }
    ");
}

#[test]
fn struct_with_field_that_is_a_3_tuple() {
    #[derive(Facet)]
    struct MyStruct {
        one: (String, i32, u16),
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var one: (String, Int32, UInt16)

        public init(one: (String, Int32, UInt16)) {
            self.one = one
        }
    }
    ");
}

#[test]
fn struct_with_field_that_is_a_4_tuple() {
    #[derive(Facet)]
    struct MyStruct {
        one: (String, i32, u16, f32),
    }

    // TODO: The NTuple4 struct should be emitted in the preamble if required, e.g.
    // data class NTuple4<T1, T2, T3, T4>(val t1: T1, val t2: T2, val t3: T3, val t4: T4)

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var one: (String, Int32, UInt16, Float)

        public init(one: (String, Int32, UInt16, Float)) {
            self.one = one
        }
    }
    ");
}

#[test]
fn enum_with_unit_variants() {
    /// line one
    #[derive(Facet)]
    #[repr(C)]
    /// line two
    #[allow(unused)]
    enum EnumWithUnitVariants {
        /// variant one
        Variant1,
        Variant2,
        /// variant three
        Variant3,
    }

    let actual = emit!(EnumWithUnitVariants as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    /// line one
    /// line two
    indirect public enum EnumWithUnitVariants {
        /// variant one
        case variant1
        case variant2
        /// variant three
        case variant3
    }
    ");
}

#[test]
fn enum_with_unit_struct_variants() {
    #[derive(Facet)]
    #[repr(C)]
    #[allow(unused)]
    enum MyEnum {
        Variant1 {},
    }

    let actual = emit!(MyEnum as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    indirect public enum MyEnum {
        case variant1
    }
    ");
}

#[test]
fn enum_with_1_tuple_variants() {
    #[derive(Facet)]
    #[repr(C)]
    #[allow(unused)]
    enum MyEnum {
        Variant1(String),
    }

    let actual = emit!(MyEnum as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    indirect public enum MyEnum {
        case variant1(String)
    }
    ");
}

#[test]
fn enum_with_newtype_variants() {
    #[derive(Facet)]
    #[repr(C)]
    #[allow(unused)]
    enum MyEnum {
        Variant1(String),
        Variant2(i32),
    }

    let actual = emit!(MyEnum as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    indirect public enum MyEnum {
        case variant1(String)
        case variant2(Int32)
    }
    ");
}

#[test]
fn enum_with_tuple_variants() {
    #[derive(Facet)]
    #[repr(C)]
    #[allow(unused)]
    enum MyEnum {
        Variant1(String, i32),
        Variant2(bool, f64, u8),
    }

    let actual = emit!(MyEnum as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    indirect public enum MyEnum {
        case variant1(String, Int32)
        case variant2(Bool, Double, UInt8)
    }
    ");
}

#[test]
fn enum_with_struct_variants() {
    #[derive(Facet)]
    #[repr(C)]
    #[allow(unused)]
    enum MyEnum {
        Variant1 { field1: String, field2: i32 },
    }

    let actual = emit!(MyEnum as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    indirect public enum MyEnum {
        case variant1(field1: String, field2: Int32)
    }
    ");
}

#[test]
fn enum_with_mixed_variants() {
    #[derive(Facet)]
    #[repr(C)]
    #[allow(unused)]
    enum MyEnum {
        Unit,
        NewType(String),
        Tuple(String, i32),
        Struct { field: bool },
    }

    let actual = emit!(MyEnum as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    indirect public enum MyEnum {
        case unit
        case newType(String)
        case tuple(String, Int32)
        case struct(field: Bool)
    }
    ");
}

#[test]
fn struct_with_vec_field() {
    #[derive(Facet)]
    struct MyStruct {
        items: Vec<String>,
        numbers: Vec<i32>,
        nested_items: Vec<Vec<String>>,
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var items: [String]
        public var numbers: [Int32]
        public var nestedItems: [[String]]

        public init(items: [String], numbers: [Int32], nestedItems: [[String]]) {
            self.items = items
            self.numbers = numbers
            self.nestedItems = nestedItems
        }
    }
    ");
}

#[test]
fn struct_with_option_field() {
    #[derive(Facet)]
    #[allow(clippy::struct_field_names)]
    struct MyStruct {
        optional_string: Option<String>,
        optional_number: Option<i32>,
        optional_bool: Option<bool>,
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var optionalString: String?
        public var optionalNumber: Int32?
        public var optionalBool: Bool?

        public init(optionalString: String?, optionalNumber: Int32?, optionalBool: Bool?) {
            self.optionalString = optionalString
            self.optionalNumber = optionalNumber
            self.optionalBool = optionalBool
        }
    }
    ");
}

#[test]
fn struct_with_hashmap_field() {
    #[derive(Facet)]
    struct MyStruct {
        string_to_int: HashMap<String, i32>,
        int_to_bool: HashMap<i32, bool>,
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var stringToInt: [String: Int32]
        public var intToBool: [Int32: Bool]

        public init(stringToInt: [String: Int32], intToBool: [Int32: Bool]) {
            self.stringToInt = stringToInt
            self.intToBool = intToBool
        }
    }
    ");
}

#[test]
fn struct_with_nested_generics() {
    #[derive(Facet)]
    struct MyStruct {
        optional_list: Option<Vec<String>>,
        list_of_options: Vec<Option<i32>>,
        map_to_list: HashMap<String, Vec<bool>>,
        optional_map: Option<HashMap<String, i32>>,
        complex: Vec<Option<HashMap<String, Vec<bool>>>>,
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var optionalList: [String]?
        public var listOfOptions: [Int32?]
        public var mapToList: [String: [Bool]]
        public var optionalMap: [String: Int32]?
        public var complex: [[String: [Bool]]?]

        public init(optionalList: [String]?, listOfOptions: [Int32?], mapToList: [String: [Bool]], optionalMap: [String: Int32]?, complex: [[String: [Bool]]?]) {
            self.optionalList = optionalList
            self.listOfOptions = listOfOptions
            self.mapToList = mapToList
            self.optionalMap = optionalMap
            self.complex = complex
        }
    }
    ");
}

#[test]
fn struct_with_array_field() {
    #[derive(Facet)]
    #[allow(clippy::struct_field_names)]
    struct MyStruct {
        fixed_array: [i32; 5],
        byte_array: [u8; 32],
        string_array: [String; 3],
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var fixedArray: [Int32]
        public var byteArray: [UInt8]
        public var stringArray: [String]

        public init(fixedArray: [Int32], byteArray: [UInt8], stringArray: [String]) {
            self.fixedArray = fixedArray
            self.byteArray = byteArray
            self.stringArray = stringArray
        }
    }
    ");
}

#[test]
fn struct_with_map_fields() {
    #[derive(Facet)]
    struct MyStruct {
        string_to_int: BTreeMap<String, i32>,
        int_to_bool: BTreeMap<i32, bool>,
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var stringToInt: [String: Int32]
        public var intToBool: [Int32: Bool]

        public init(stringToInt: [String: Int32], intToBool: [Int32: Bool]) {
            self.stringToInt = stringToInt
            self.intToBool = intToBool
        }
    }
    ");
}

#[test]
fn struct_with_hashset_field() {
    // NOTE: HashSet<T> now maps to Set<T> in Kotlin with the new Format::Set variant.
    // This preserves the uniqueness constraint and provides better type safety.
    #[derive(Facet)]
    struct MyStruct {
        string_set: HashSet<String>,
        int_set: HashSet<i32>,
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var stringSet: Set<String>
        public var intSet: Set<Int32>

        public init(stringSet: Set<String>, intSet: Set<Int32>) {
            self.stringSet = stringSet
            self.intSet = intSet
        }
    }
    ");
}

#[test]
fn struct_with_set_fields() {
    // NOTE: BTreeSet<T> now maps to Set<T> in Kotlin with the new Format::Set variant.
    // This preserves the uniqueness constraint and provides better type safety.
    #[derive(Facet)]
    struct MyStruct {
        string_set: BTreeSet<String>,
        int_set: BTreeSet<i32>,
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var stringSet: Set<String>
        public var intSet: Set<Int32>

        public init(stringSet: Set<String>, intSet: Set<Int32>) {
            self.stringSet = stringSet
            self.intSet = intSet
        }
    }
    ");
}

#[test]
fn struct_with_box_field() {
    #[derive(Facet)]
    #[allow(clippy::box_collection)]
    struct MyStruct {
        boxed_string: Box<String>,
        boxed_int: Box<i32>,
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var boxedString: String
        public var boxedInt: Int32

        public init(boxedString: String, boxedInt: Int32) {
            self.boxedString = boxedString
            self.boxedInt = boxedInt
        }
    }
    ");
}

#[test]
fn struct_with_rc_field() {
    #[derive(Facet)]
    struct MyStruct {
        rc_string: Rc<String>,
        rc_int: Rc<i32>,
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var rcString: String
        public var rcInt: Int32

        public init(rcString: String, rcInt: Int32) {
            self.rcString = rcString
            self.rcInt = rcInt
        }
    }
    ");
}

#[test]
fn struct_with_arc_field() {
    #[derive(Facet)]
    struct MyStruct {
        arc_string: Arc<String>,
        arc_int: Arc<i32>,
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var arcString: String
        public var arcInt: Int32

        public init(arcString: String, arcInt: Int32) {
            self.arcString = arcString
            self.arcInt = arcInt
        }
    }
    ");
}

#[test]
fn struct_with_mixed_collections_and_pointers() {
    #[derive(Facet)]
    #[allow(clippy::box_collection)]
    struct MyStruct {
        vec_of_sets: Vec<HashSet<String>>,
        optional_btree: Option<BTreeMap<String, i32>>,
        boxed_vec: Box<Vec<String>>,
        arc_option: Arc<Option<String>>,
        array_of_boxes: [Box<i32>; 3],
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var vecOfSets: [Set<String>]
        public var optionalBtree: [String: Int32]?
        public var boxedVec: [String]
        public var arcOption: String?
        public var arrayOfBoxes: [Int32]

        public init(vecOfSets: [Set<String>], optionalBtree: [String: Int32]?, boxedVec: [String], arcOption: String?, arrayOfBoxes: [Int32]) {
            self.vecOfSets = vecOfSets
            self.optionalBtree = optionalBtree
            self.boxedVec = boxedVec
            self.arcOption = arcOption
            self.arrayOfBoxes = arrayOfBoxes
        }
    }
    ");
}

#[test]
fn struct_with_bytes_field() {
    #[derive(Facet)]
    struct MyStruct {
        #[facet(fg::bytes)]
        data: Vec<u8>,
        name: String,
        #[facet(fg::bytes)]
        header: Vec<u8>,
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var data: [UInt8]
        public var name: String
        public var header: [UInt8]

        public init(data: [UInt8], name: String, header: [UInt8]) {
            self.data = data
            self.name = name
            self.header = header
        }
    }
    ");
}

#[test]
fn struct_with_bytes_field_and_slice() {
    #[derive(Facet)]
    struct MyStruct<'a> {
        #[facet(fg::bytes)]
        data: &'a [u8],
        name: String,
        #[facet(fg::bytes)]
        header: Vec<u8>,
        optional_bytes: Option<Vec<u8>>,
    }

    let actual = emit!(MyStruct as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct MyStruct {
        public var data: [UInt8]
        public var name: String
        public var header: [UInt8]
        public var optionalBytes: [UInt8]?

        public init(data: [UInt8], name: String, header: [UInt8], optionalBytes: [UInt8]?) {
            self.data = data
            self.name = name
            self.header = header
            self.optionalBytes = optionalBytes
        }
    }
    ");
}

#[test]
fn namespaced_child() {
    #[derive(Facet)]
    #[facet(fg::namespace = "Test")]
    struct Child {
        test: String,
    }

    #[derive(Facet)]
    struct Parent {
        child: Child,
    }

    let actual = emit!(Parent as Swift with Encoding::None).unwrap();
    insta::assert_snapshot!(actual, @"

    public struct Parent {
        public var child: Test.Child

        public init(child: Test.Child) {
            self.child = child
        }
    }

    public struct Child {
        public var test: String

        public init(test: String) {
            self.test = test
        }
    }
    ");
}

#[test]
fn type_in_root_and_named_namespace() {
    #[derive(Facet)]
    struct Child {
        value: String,
    }

    mod other {
        use crate as fg;
        use facet::Facet;

        #[derive(Facet)]
        #[facet(fg::namespace = "other")]
        pub struct Child {
            value: i32,
        }
    }

    #[derive(Facet)]
    struct Parent {
        child: Child,
        other_child: other::Child,
    }

    let (other, root) = emit_two_modules!(CodeGenerator, Parent, "root");
    insta::assert_snapshot!(other, @"

    public struct Child {
        public var value: Int32

        public init(value: Int32) {
            self.value = value
        }
    }
    ");

    insta::assert_snapshot!(root, @"
    import Other

    public struct Child {
        public var value: String

        public init(value: String) {
            self.value = value
        }
    }

    public struct Parent {
        public var child: Child
        public var otherChild: Other.Child

        public init(child: Child, otherChild: Other.Child) {
            self.child = child
            self.otherChild = otherChild
        }
    }
    ");
}