binary-mirror-derive 0.1.17

Derive macro for parsing fixed-length binary data structures
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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
use binary_mirror::{FromBytes, FromNative, NativeStructCode, ToBytes, ToNative};
use binary_mirror_derive::{BinaryEnum, BinaryMirror};
use chrono::{NaiveDate, NaiveDateTime, NaiveTime, Timelike};
use compact_str::ToCompactString;
use rust_decimal::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, BinaryEnum, Serialize, Deserialize)]
enum OrderSide {
    #[bv(value = b"B")]
    Buy,
    #[bv(value = b"S")]
    Sell,
}

#[derive(Debug, PartialEq, BinaryEnum, Serialize, Deserialize)]
enum Direction {
    Up,   // Will use b'U'
    Down, // Will use b'D'
}

#[derive(Debug, PartialEq, BinaryEnum, Serialize, Deserialize)]
enum OrderType {
    #[bv(value = b"MKT")]
    Market,
    #[bv(value = b"LMT")]
    Limit,
}

#[repr(C)]
#[derive(BinaryMirror)]
struct TestStruct {
    #[bm(type = "str")]
    name: [u8; 10],
    #[bm(type = "i32")]
    value: [u8; 4],
    no_type: [u8; 7],
    #[bm(type = "decimal")]
    decimal: [u8; 20],
    #[bm(type = "f32")]
    f32: [u8; 5],
    #[bm(type = "str", alias = "exchange")]
    exh: [u8; 10],
    #[bm(
        type = "date",
        format = "%Y%m%d",
        datetime_with = "time",
        alias = "datetime",
        skip = true
    )]
    date: [u8; 8],
    #[bm(type = "time", format = "%H%M%S", skip = true)]
    time: [u8; 6],
    #[bm(type = "enum", enum_type = "OrderSide")]
    side: [u8; 1],
}

#[repr(C)]
#[derive(BinaryMirror)]
struct WithDateTime {
    #[bm(type = "datetime", format = "%Y%m%d%H%M%S")]
    dt: [u8; 14],
}

#[test]
fn test_with_datetime() {
    let dt = WithDateTime {
        dt: *b"20240101123456",
    };
    println!("{:?}", dt);
    assert_eq!(
        dt.dt(),
        Some(NaiveDateTime::new(
            NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            NaiveTime::from_hms_opt(12, 34, 56).unwrap()
        ))
    );
}

#[derive(BinaryMirror)]
struct WithDefaultByte {
    #[bm(type = "str", default_byte = b'0')]
    zero: [u8; 10],
}

#[test]
fn test_with_default_byte() {
    let default_ = WithDefaultByteNative {
        zero: Some(String::from("11")),
    };
    println!("{:?}", default_);
    let raw = WithDefaultByte::from_native(&default_);
    assert_eq!(raw.zero(), Some("1100000000".to_string()));
}

#[repr(C)]
#[derive(BinaryMirror)]
struct WithDefaultBytes {
    #[bm(type = "str", default_byte = b'\x08')] // 0x08 hex
    hex: [u8; 5],
    #[bm(type = "str", default_byte = b'\x00')] // Null in hex
    null: [u8; 5],
    #[bm(type = "str", default_byte = b'0')] // Regular byte literal
    zero: [u8; 5],
}

#[test]
fn test_default_bytes() {
    let native = WithDefaultBytesNative::default()
        .with_hex("abc")
        .with_null("abc")
        .with_zero("abc");

    let binary = WithDefaultBytes::from_native(&native);

    assert_eq!(&binary.hex, b"abc\x08\x08"); // Padded with spaces (0x08)
    assert_eq!(&binary.null, b"abc\x00\x00"); // Padded with nulls (0x00)
    assert_eq!(&binary.zero, b"abc00"); // Padded with '0' chars
}

#[test]
fn test_struct_derivation() {
    let test = TestStruct {
        name: *b"Hello     ",
        value: *b"123 ",
        no_type: *b"no_type",
        decimal: *b"000000123.4500000000",
        f32: *b"123.4",
        exh: *b"CME       ",
        date: *b"20240101",
        time: *b"123456",
        side: *b"B",
    };
    println!("{:?}", test);

    assert_eq!(test.name(), Some("Hello".to_string()));
    assert_eq!(test.value(), Some(123));
    assert_eq!(test.decimal(), Some(Decimal::from_str("123.45").unwrap()));
    assert_eq!(test.f32(), Some(123.4));
    assert_eq!(test.exchange(), Some("CME".to_string()));
    assert_eq!(
        test.datetime(),
        Some(NaiveDateTime::new(
            NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            NaiveTime::from_hms_opt(12, 34, 56).unwrap()
        ))
    );
    assert_eq!(test.side(), Some(OrderSide::Buy));
}

#[test]
fn test_invalid_number() {
    let test = TestStruct {
        name: *b"Test      ",
        value: *b"abc\0",
        no_type: *b"no_type",
        decimal: *b"000000123.xxxxxxxxxx",
        f32: *b"123.x",
        exh: *b"CME       ",
        date: *b"xxxxxxxx",
        time: *b"xxxxxx",
        side: *b" ",
    };

    assert_eq!(test.name(), Some("Test".to_string()));
    assert_eq!(test.value(), None);
    assert_eq!(test.decimal(), None);
    assert_eq!(test.f32(), None);
    assert_eq!(test.datetime(), None);
    assert_eq!(test.side(), None);
}

#[test]
fn test_debug_format() {
    let test = TestStruct {
        name: *b"Hello     ",
        value: *b"123 ",
        no_type: *b"no_type",
        decimal: *b"000000123.4500000000",
        f32: *b"123.4",
        exh: *b"CME       ",
        date: *b"20240101",
        time: *b"123456",
        side: *b"B",
    };

    println!("{:#?}", test);
}

#[test]
fn test_display_format() {
    let test = TestStruct {
        name: *b"Hello     ",
        value: *b"123 ",
        no_type: *b"no_type",
        decimal: *b"000000123.4500000000",
        f32: *b"123.4",
        exh: *b"CME       ",
        date: *b"20240101",
        time: *b"123456",
        side: *b"B",
    };

    // Will print:
    // TestStruct { name: Hello, value: 123, decimal: 123.45, f32: 123.4, exchange: CME, datetime: 2024-01-01T12:34:56 }
    assert_eq!(format!("{}", test), "TestStruct { name: Hello, value: 123, decimal: 123.45, f32: 123.4, exchange: CME, datetime: 2024-01-01 12:34:56, side: Buy }");

    let invalid = TestStruct {
        name: *b"Test      ",
        value: *b"abc\0",
        no_type: *b"no_type",
        decimal: *b"000000123.xxxxxxxxxx",
        f32: *b"123.x",
        exh: *b"CME       ",
        date: *b"xxxxxxxx",
        time: *b"xxxxxx",
        side: *b" ",
    };

    // Will print:
    //TestStruct { name: Test, value: Error<bytes: "abc\x00">, decimal: Error<bytes: "000000123.xxxxxxxxxx">, f32: Error<bytes: "123.x">, exchange: CME, datetime: Error<bytes: "xxxxxxxx">, side: Error<bytes: " "> }
    println!("{}", invalid);
    assert_eq!(format!("{}", invalid), "TestStruct { name: Test, value: Error<bytes: \"abc\\x00\">, decimal: Error<bytes: \"000000123.xxxxxxxxxx\">, f32: Error<bytes: \"123.x\">, exchange: CME, datetime: Error<bytes: \"xxxxxxxx\">, side: Error<bytes: \" \"> }");
}

#[test]
fn test_binary_enum() {
    // Test custom byte values
    assert_eq!(OrderSide::from_bytes(b"B"), Some(OrderSide::Buy));
    assert_eq!(OrderSide::from_bytes(b"S"), Some(OrderSide::Sell));
    assert_eq!(OrderSide::from_bytes(b"X"), None);

    // Test default behavior
    assert_eq!(Direction::from_bytes(b"U"), Some(Direction::Up));
    assert_eq!(Direction::from_bytes(b"D"), Some(Direction::Down));
    assert_eq!(Direction::from_bytes(b"X"), None);
}

#[test]
fn test_struct_from_bytes() {
    let bytes = b"Hello     123 no_type000000123.4500000000123.4CME       20240101123456B";
    let test = TestStruct::from_bytes(bytes).expect("Should parse successfully");
    assert_eq!(test.name(), Some("Hello".to_string()));
    assert_eq!(test.value(), Some(123));

    // Test wrong size
    let wrong_size = b"too short with some \xff special \x00 bytes";
    let err = TestStruct::from_bytes(wrong_size).unwrap_err();
    assert_eq!(
        err.to_string(),
        format!(
            "bytes size mismatch: expected {} bytes but got {} bytes, content: \"{}\"",
            TestStruct::size(),
            wrong_size.len(),
            "too short with some \\xff special \\x00 bytes"
        )
    );
}

#[test]
fn test_serde_serialization() {
    let test = TestStruct {
        name: *b"Hello     ",
        value: *b"123 ",
        no_type: *b"no_type",
        decimal: *b"000000123.4500000000",
        f32: *b"123.4",
        exh: *b"CME       ",
        date: *b"20240101",
        time: *b"123456",
        side: *b"B",
    };
    let test_native = test.to_native();
    // Test JSON serialization
    let json = serde_json::to_string(&test_native).unwrap();
    assert_eq!(
        json,
        r#"{"name":"Hello","value":123,"decimal":"123.45","f32":123.4,"exchange":"CME","datetime":"2024-01-01T12:34:56","side":"Buy"}"#
    );

    // Test invalid values
    let invalid = TestStruct {
        name: *b"Test      ",
        value: *b"abc\0",
        no_type: *b"no_type",
        decimal: *b"000000123.xxxxxxxxxx",
        f32: *b"123.x",
        exh: *b"CME       ",
        date: *b"xxxxxxxx",
        time: *b"xxxxxx",
        side: *b" ",
    };
    let test_native = invalid.to_native();
    let json = serde_json::to_string(&test_native).unwrap();
    assert_eq!(
        json,
        r#"{"name":"Test","value":null,"decimal":null,"f32":null,"exchange":"CME","datetime":null,"side":null}"#
    );
}

#[test]
fn test_to_native() {
    let test = TestStruct {
        name: *b"Hello     ",
        value: *b"123 ",
        no_type: *b"no_type",
        decimal: *b"000000123.4500000000",
        f32: *b"123.4",
        exh: *b"CME       ",
        date: *b"20240101",
        time: *b"123456",
        side: *b"B",
    };
    let native = test.to_native();
    assert_eq!(native.name, test.name());
}

#[test]
fn test_struct_from_native() {
    let test = TestStruct {
        name: *b"Hello     ",
        value: *b"123 ",
        no_type: *b"       ",
        // no_type: *b"no_type",
        decimal: *b"123.45              ",
        // decimal: *b"000000123.4500000000",
        f32: *b"123.4",
        exh: *b"CME       ",
        date: *b"20240101",
        time: *b"123456",
        side: *b"B",
    };
    let native = test.to_native();
    let parsed = TestStruct::from_native(&native);
    assert_eq!(parsed.name(), test.name());
    assert_eq!(parsed.value(), test.value());
    assert_eq!(parsed.decimal(), test.decimal());
    assert_eq!(parsed.f32(), test.f32());
    assert_eq!(parsed.exchange(), test.exchange());
    assert_eq!(parsed.datetime(), test.datetime());
    assert_eq!(parsed.side(), test.side());

    let bytes = test.to_bytes();
    let parsed_bytes = parsed.to_bytes();
    println!("{:?}", test);
    println!("{:?}", parsed);
    assert_eq!(bytes, parsed_bytes);
}

#[test]
fn test_native_default() {
    let native = TestStructNative::default();
    assert_eq!(native.name, None);
    assert_eq!(native.value, None);
    assert_eq!(native.decimal, None);
    assert_eq!(native.f32, None);
    assert_eq!(native.exchange, None);
    assert_eq!(native.datetime, None);
    assert_eq!(native.side, None);
}

#[test]
fn test_struct_to_bytes() {
    let original_bytes = b"Hello     123 no_type000000123.4500000000123.4CME       20240101123456B";
    let test = TestStruct::from_bytes(original_bytes).expect("Should parse successfully");

    // Convert back to bytes
    let bytes = test.to_bytes();
    assert_eq!(bytes, original_bytes);

    // Test that we can parse the bytes back
    let reparsed = TestStruct::from_bytes(test.to_bytes()).unwrap();
    assert_eq!(reparsed.name(), test.name());
    assert_eq!(reparsed.value(), test.value());
    assert_eq!(reparsed.decimal(), test.decimal());
    assert_eq!(reparsed.f32(), test.f32());
    assert_eq!(reparsed.exchange(), test.exchange());
    assert_eq!(reparsed.datetime(), test.datetime());
    assert_eq!(reparsed.side(), test.side());
}

#[test]
fn test_struct_to_bytes_owned() {
    let test = TestStruct::from_bytes(
        b"Hello     123 no_type000000123.4500000000123.4CME       20240101123456B",
    )
    .unwrap();
    let bytes = test.to_bytes_owned();
    assert_eq!(
        bytes,
        b"Hello     123 no_type000000123.4500000000123.4CME       20240101123456B"
    );
}

#[test]
fn test_binary_enum_roundtrip() {
    // Test custom byte values
    assert_eq!(OrderSide::Buy.as_bytes(), b"B");
    assert_eq!(OrderSide::Sell.as_bytes(), b"S");

    // Test roundtrip
    let buy = OrderSide::from_bytes(b"B").unwrap();
    assert_eq!(OrderSide::from_bytes(buy.as_bytes()), Some(OrderSide::Buy));

    let sell = OrderSide::from_bytes(b"S").unwrap();
    assert_eq!(
        OrderSide::from_bytes(sell.as_bytes()),
        Some(OrderSide::Sell)
    );

    // Test default behavior
    assert_eq!(Direction::Up.as_bytes(), b"U");
    assert_eq!(Direction::Down.as_bytes(), b"D");

    // Test roundtrip with default behavior
    let up = Direction::from_bytes(b"U").unwrap();
    assert_eq!(Direction::from_bytes(up.as_bytes()), Some(Direction::Up));

    let down = Direction::from_bytes(b"D").unwrap();
    assert_eq!(
        Direction::from_bytes(down.as_bytes()),
        Some(Direction::Down)
    );
}

#[repr(C)]
#[derive(BinaryMirror)]
struct WithIgnoreWarn {
    #[bm(type = "str")]
    name: [u8; 10],
    #[bm(type = "i32", ignore_warn = true)]
    value: [u8; 4],
}

#[test]
fn test_ignore_warn() {
    let test = WithIgnoreWarn {
        name: *b"Hello     ",
        value: *b"abc ", // Invalid value
    };

    let native = test.to_native();
    // No warning will be logged for value field
    assert_eq!(native.value, None);
}

#[test]
fn test_native_builder() {
    let native = TestStructNative::default()
        .with_name("AAPL")
        .with_value(123)
        .with_decimal(Decimal::from_str("123.45").unwrap())
        .with_f32(123.4)
        .with_exchange("NYSE")
        .with_datetime(NaiveDateTime::new(
            NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            NaiveTime::from_hms_opt(12, 34, 56).unwrap(),
        ))
        .with_side(OrderSide::Buy);

    assert_eq!(native.name, Some("AAPL".to_string()));
    assert_eq!(native.value, Some(123));
    assert_eq!(native.decimal, Some(Decimal::from_str("123.45").unwrap()));
    assert_eq!(native.f32, Some(123.4));
    assert_eq!(native.exchange, Some("NYSE".to_string()));
    assert_eq!(
        native.datetime,
        Some(NaiveDateTime::new(
            NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            NaiveTime::from_hms_opt(12, 34, 56).unwrap()
        ))
    );
    assert_eq!(native.side, Some(OrderSide::Buy));

    // Convert to binary format
    let binary = TestStruct::from_native(&native);
    assert_eq!(binary.name(), Some("AAPL".to_string()));
    assert_eq!(binary.value(), Some(123));
    assert_eq!(binary.decimal(), Some(Decimal::from_str("123.45").unwrap()));
    assert_eq!(binary.f32(), Some(123.4));
    assert_eq!(binary.exchange(), Some("NYSE".to_string()));
    assert_eq!(
        binary.datetime(),
        Some(NaiveDateTime::new(
            NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            NaiveTime::from_hms_opt(12, 34, 56).unwrap()
        ))
    );
    assert_eq!(binary.side(), Some(OrderSide::Buy));
}

#[repr(C)]
#[derive(BinaryMirror)]
struct WithNumberFormat {
    #[bm(type = "i32", format = "{:04}")] // Zero-padded 4 digits
    value: [u8; 4],
    #[bm(type = "f32", format = "{:09.3}")] // 2 decimal places
    price: [u8; 9],
    #[bm(type = "f32", format = "{:010.3}")]
    nagtive: [u8; 10],
    #[bm(type = "decimal", format = "{:010.3}")]
    decimal: [u8; 10],
    #[bm(type = "decimal", format = "{:010.3}")]
    decimal_with_neg: [u8; 10],
}

#[test]
fn test_number_format() {
    let native = WithNumberFormatNative::default()
        .with_value(42)
        .with_price(123.45)
        .with_nagtive(-123.45)
        .with_decimal(Decimal::from_str("123.45").unwrap())
        .with_decimal_with_neg(Decimal::from_str("-123.45").unwrap());

    let binary = WithNumberFormat::from_native(&native);
    assert_eq!(binary.value(), Some(42));
    assert_eq!(binary.price(), Some(123.45)); // Rounded to 2 decimals
    assert_eq!(binary.nagtive(), Some(-123.45));
    assert_eq!(binary.decimal(), Some(Decimal::from_str("123.45").unwrap()));
    assert_eq!(
        binary.decimal_with_neg(),
        Some(Decimal::from_str("-123.45").unwrap())
    );
    // Check raw bytes format
    println!("{:?}", binary);
    assert_eq!(&binary.value, b"0042");
    assert_eq!(&binary.price, b"00123.450");
    assert_eq!(&binary.nagtive, b"-00123.450");
    assert_eq!(&binary.decimal, b"000123.450");
    assert_eq!(&binary.decimal_with_neg, b"-00123.450");
}

#[repr(C)]
#[derive(BinaryMirror)]
struct WithBytes {
    #[bm(type = "bytes")]
    raw: [u8; 10],
    #[bm(type = "bytes", default_byte = b'0')]
    padded: [u8; 5],
}

#[test]
fn test_bytes() {
    let native = WithBytesNative::default()
        .with_raw([1, 2, 3, 4, 5, b' ', b' ', b' ', b' ', b' '])
        .with_padded([0xFF, 0xFE, b'0', b'0', b'0']);

    let binary = WithBytes::from_native(&native);
    assert_eq!(binary.raw(), [1, 2, 3, 4, 5, b' ', b' ', b' ', b' ', b' ']);
    assert_eq!(binary.padded(), [0xFF, 0xFE, b'0', b'0', b'0']);

    // Test raw bytes format
    assert_eq!(&binary.raw, &[1, 2, 3, 4, 5, b' ', b' ', b' ', b' ', b' ']);
    assert_eq!(&binary.padded, &[0xFF, 0xFE, b'0', b'0', b'0']);
}

#[test]
fn test_bytes_serde() {
    let native = WithBytesNative {
        raw: [1, 2, 3, b' ', b' ', b' ', b' ', b' ', b' ', b' '],
        padded: [0xFF, 0xFE, b'0', b'0', b'0'],
    };

    let json = serde_json::to_string(&native).unwrap();
    assert_eq!(
        json,
        r#"{"raw":[1,2,3,32,32,32,32,32,32,32],"padded":[255,254,48,48,48]}"#
    );

    let parsed: WithBytesNative = serde_json::from_str(&json).unwrap();
    assert_eq!(
        parsed.raw,
        [1, 2, 3, b' ', b' ', b' ', b' ', b' ', b' ', b' ']
    );
    assert_eq!(parsed.padded, [0xFF, 0xFE, b'0', b'0', b'0']);
}

#[test]
fn test_field_specs() {
    let name_spec = TestStruct::name_spec();
    assert_eq!(name_spec.offset, 0);
    assert_eq!(name_spec.limit, 10);
    assert_eq!(name_spec.size, 10);

    let value_spec = TestStruct::value_spec();
    assert_eq!(value_spec.offset, 10);
    assert_eq!(value_spec.limit, 14);
    assert_eq!(value_spec.size, 4);

    // Test field access using spec
    let bytes = b"Hello     123 no_type000000123.4500000000123.4CME       20240101123456B";
    let _test = TestStruct::from_bytes(bytes).unwrap();

    // Get field slices using spec
    let spec = TestStruct::name_spec();
    assert_eq!(&bytes[spec.offset..spec.limit], b"Hello     ");

    let spec = TestStruct::decimal_spec();
    assert_eq!(&bytes[spec.offset..spec.limit], b"000000123.4500000000");

    // Test field boundaries
    assert_eq!(
        TestStruct::name_spec().limit,
        TestStruct::value_spec().offset
    );
    assert_eq!(
        TestStruct::value_spec().limit,
        TestStruct::no_type_spec().offset
    );
}

#[test]
fn test_multi_byte_enum() {
    // Test multi-byte values
    assert_eq!(OrderType::from_bytes(b"MKT"), Some(OrderType::Market));
    assert_eq!(OrderType::from_bytes(b"LMT"), Some(OrderType::Limit));
    assert_eq!(OrderType::from_bytes(b"XXX"), None);

    // Test roundtrip
    let market = OrderType::Market;
    assert_eq!(market.as_bytes(), b"MKT");
    assert_eq!(
        OrderType::from_bytes(market.as_bytes()),
        Some(OrderType::Market)
    );

    let limit = OrderType::Limit;
    assert_eq!(limit.as_bytes(), b"LMT");
    assert_eq!(
        OrderType::from_bytes(limit.as_bytes()),
        Some(OrderType::Limit)
    );

    // Test in struct
    #[repr(C)]
    #[derive(BinaryMirror)]
    struct Order {
        #[bm(type = "enum", enum_type = "OrderType")]
        order_type: [u8; 3],
    }

    let order = Order {
        order_type: *b"MKT",
    };

    assert_eq!(order.order_type(), Some(OrderType::Market));
    let native = order.to_native();
    assert_eq!(native.order_type, Some(OrderType::Market));
}

fn default_i32() -> i32 {
    42
}

fn default_str() -> String {
    "UNKNOWN".to_string()
}

fn now() -> chrono::NaiveDateTime {
    chrono::Local::now().naive_utc().with_nanosecond(0).unwrap()
}

fn today() -> chrono::NaiveDate {
    chrono::Local::now().date_naive()
}

fn now_time() -> chrono::NaiveTime {
    chrono::Local::now().time().with_nanosecond(0).unwrap()
}

fn order_type_default() -> OrderType {
    OrderType::Limit
}

fn decimal_default() -> Decimal {
    Decimal::from_str("123.45").unwrap()
}

#[repr(C)]
#[derive(BinaryMirror)]
struct WithDefaults {
    #[bm(type = "str", default_func = "default_str")]
    name: [u8; 10],
    #[bm(type = "i32", default_func = "default_i32")]
    value: [u8; 4],
    #[bm(type = "datetime", default_func = "now", format = "%Y%m%d%H%M%S")]
    timestamp: [u8; 14],
    #[bm(type = "date", default_func = "today", format = "%Y%m%d")]
    today: [u8; 8],
    #[bm(type = "time", default_func = "now_time", format = "%H%M%S")]
    time: [u8; 6],
    #[bm(
        type = "date",
        default_func = "now",
        format = "%Y-%m-%d",
        datetime_with = "tt",
        alias = "concate_datetime"
    )]
    date: [u8; 10],
    #[bm(type = "time", format = "%H:%M:%S")]
    tt: [u8; 8],
    #[bm(
        type = "enum",
        enum_type = "OrderType",
        default_func = "order_type_default"
    )]
    order_type: [u8; 3],
    #[bm(type = "decimal", default_func = "decimal_default")]
    decimal: [u8; 10],
}

#[test]
fn test_defaults() {
    let native = WithDefaultsNative::default();
    println!("{:?}", native);
    let binary = WithDefaults::from_native(&native);
    println!("{:?}", binary);
    assert_eq!(binary.name(), Some("UNKNOWN".to_string()));
    assert_eq!(binary.value(), Some(42));
    assert_eq!(binary.timestamp(), Some(now()));
    assert_eq!(binary.today(), Some(today()));
    assert_eq!(binary.time(), Some(now_time()));
    assert_eq!(binary.concate_datetime(), Some(now()));
    assert_eq!(binary.order_type(), Some(OrderType::Limit));
    assert_eq!(binary.decimal(), Some(Decimal::from_str("123.45").unwrap()));
    // Test overriding defaults
    let native = WithDefaultsNative::default()
        .with_name("TEST")
        .with_value(100)
        .with_decimal(Decimal::from_str("999.99").unwrap())
        .with_order_type(OrderType::Market);

    let binary = WithDefaults::from_native(&native);
    assert_eq!(binary.name(), Some("TEST".to_string()));
    assert_eq!(binary.value(), Some(100));
    assert_eq!(binary.decimal(), Some(Decimal::from_str("999.99").unwrap()));
    assert_eq!(binary.order_type(), Some(OrderType::Market));
}

#[test]
fn test_native_to_raw() {
    // Create native struct with builder pattern
    let native = TestStructNative::default()
        .with_name("AAPL")
        .with_value(123)
        .with_decimal(Decimal::from_str("123.45").unwrap())
        .with_f32(123.4)
        .with_exchange("NYSE")
        .with_datetime(NaiveDateTime::new(
            NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            NaiveTime::from_hms_opt(12, 34, 56).unwrap(),
        ))
        .with_side(OrderSide::Buy);

    // Convert directly to raw binary struct
    let raw = native.to_raw();

    // Verify values
    assert_eq!(raw.name(), Some("AAPL".to_string()));
    assert_eq!(raw.value(), Some(123));
    assert_eq!(raw.decimal(), Some(Decimal::from_str("123.45").unwrap()));
    assert_eq!(raw.f32(), Some(123.4));
    assert_eq!(raw.exchange(), Some("NYSE".to_string()));
    assert_eq!(
        raw.datetime(),
        Some(NaiveDateTime::new(
            NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            NaiveTime::from_hms_opt(12, 34, 56).unwrap()
        ))
    );
    assert_eq!(raw.side(), Some(OrderSide::Buy));

    // Test roundtrip
    let native2 = raw.to_native();
    let raw2 = native2.to_raw();
    assert_eq!(raw.to_bytes(), raw2.to_bytes());
}

#[test]
fn test_native_struct_code() {
    let code = TestStruct::native_struct_code();
    assert_eq!(
        code,
        r#"pub struct TestStructNative {
    pub name: Option<String>,
    pub value: Option<i32>,
    pub decimal: Option<rust_decimal::Decimal>,
    pub f32: Option<f32>,
    pub exchange: Option<String>,
    pub datetime: Option<chrono::NaiveDateTime>,
    pub side: Option<OrderSide>,
}"#
    );
}

#[repr(C)]
#[derive(BinaryMirror)]
#[bm(derive(Debug, Clone))]
struct CustomDerives {
    #[bm(type = "str")]
    name: [u8; 10],
}

#[test]
fn test_custom_native_derives() {
    let code = CustomDerives::native_struct_code();
    assert_eq!(
        code,
        r#"pub struct CustomDerivesNative {
    pub name: Option<String>,
}"#
    );

    // Test that the actual struct has the same derives
    let native = CustomDerivesNative::default();
    let _cloned = native.clone(); // Should compile because we have Clone
    let _debug = format!("{:?}", native); // Should compile because we have Debug
}

#[repr(C)]
#[derive(BinaryMirror)]
struct WithSkippedFields {
    #[bm(type = "str")]
    name: [u8; 10],
    #[bm(type = "i32", skip = true, default_byte = b'\x33')]
    skipped_value: [u8; 4],
    #[bm(type = "str")]
    description: [u8; 20],
}

#[test]
fn test_skipped_fields() {
    let native = WithSkippedFieldsNative::default()
        .with_name("TEST")
        .with_description("Description");

    let raw = native.to_raw();
    let bytes = raw.to_bytes();
    let parsed = WithSkippedFields::from_bytes(&bytes).unwrap();

    // Verify values
    assert_eq!(raw.name(), Some("TEST".to_string()));
    assert_eq!(raw.description(), Some("Description".to_string()));
    assert_eq!(parsed.name(), Some("TEST".to_string()));
    assert_eq!(parsed.description(), Some("Description".to_string()));

    // Verify native struct code doesn't include skipped field
    let code = WithSkippedFields::native_struct_code();
    assert_eq!(
        code,
        r#"pub struct WithSkippedFieldsNative {
    pub name: Option<String>,
    pub description: Option<String>,
}"#
    );

    // Test roundtrip
    let native2 = raw.to_native();
    let raw2 = native2.to_raw();
    assert_eq!(raw.to_bytes(), raw2.to_bytes());
    assert_eq!(WithSkippedFields::size(), 34);
    assert_eq!(WithSkippedFields::SIZE, 34);
}

#[repr(C)]
#[derive(BinaryMirror)]
struct WithSkipNativeStruct {
    #[bm(type = "str")]
    name: [u8; 10],
    #[bm(type = "i32", skip_native = true)]
    raw_value: [u8; 4],
    #[bm(type = "str")]
    description: [u8; 5],
}

#[test]
fn test_skip_native() {
    let raw = WithSkipNativeStruct {
        name: *b"TEST      ",
        raw_value: *b"1234",
        description: *b"Descr",
    };

    // Field should be accessible in raw struct
    assert_eq!(raw.raw_value(), Some(1234));

    // Convert to native
    let native = raw.to_native();

    // Field should not appear in native struct
    let code = WithSkipNativeStruct::native_struct_code();
    assert_eq!(
        code,
        r#"pub struct WithSkipNativeStructNative {
    pub name: Option<String>,
    pub description: Option<String>,
}"#
    );

    // Roundtrip should preserve the raw value
    let raw2 = native.to_raw();
    assert_eq!(raw2.raw_value(), None);
}

#[repr(C)]
#[derive(BinaryMirror)]
struct WithStringTypes {
    #[bm(type = "str")]
    regular: [u8; 10],
    #[bm(type = "compact_str")]
    compact: [u8; 10],
    // #[bm(type = "hipstr")]
    // hip: [u8; 10],
}

#[test]
fn test_string_types() {
    let native = WithStringTypesNative::default()
        .with_regular("test")
        .with_compact("test");
    // .with_hip(HipStr::from("test"));

    let raw = native.to_raw();
    assert_eq!(raw.regular(), Some("test".to_string()));
    assert_eq!(raw.compact(), Some("test".to_compact_string()));
    // assert_eq!(raw.hip(), "test");

    // Test roundtrip
    // let native2 = raw.to_native();
    // assert_eq!(native2.hip, HipStr::from("test"));
}

#[repr(C)]
#[derive(BinaryMirror)]
struct ReversedDateTime {
    #[bm(
        type = "time",
        format = "%H:%M:%S",
        datetime_with = "date",
        alias = "datetime"
    )]
    time: [u8; 8],
    #[bm(type = "date", format = "%Y-%m-%d")]
    date: [u8; 10],
    #[bm(type = "date", format = "%Y-%m-%d", datetime_with = "tt", alias = "ddtt")]
    dd: [u8; 10],
    #[bm(type = "time", format = "%H:%M:%S")]
    tt: [u8; 8],
}

#[test]
fn test_reversed_datetime() {
    let raw = ReversedDateTime {
        time: *b"12:34:56",
        date: *b"2024-01-01",
        dd: *b"2024-01-02",
        tt: *b"12:34:56",
    };

    // Test parsing
    assert_eq!(
        raw.datetime(),
        Some(NaiveDateTime::new(
            NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            NaiveTime::from_hms_opt(12, 34, 56).unwrap()
        ))
    );
    assert_eq!(raw.ddtt(), Some(NaiveDateTime::new(
        NaiveDate::from_ymd_opt(2024, 1, 2).unwrap(),
        NaiveTime::from_hms_opt(12, 34, 56).unwrap()
    )));

    // Test roundtrip
    let native = raw.to_native();
    let raw2 = native.to_raw();
    assert_eq!(raw.datetime(), raw2.datetime());
    assert_eq!(raw.to_bytes(), raw2.to_bytes());
}