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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
//! Market data types for encoding different Databento [`Schema`](crate::enums::Schema)s
//! and conversion functions.

pub(crate) mod conv;
mod impl_default;
mod methods;

use std::{ffi::CStr, mem, os::raw::c_char, ptr::NonNull, slice};

// Dummy derive macro to get around `cfg_attr` incompatibility of several
// of pyo3's attribute macros. See https://github.com/PyO3/pyo3/issues/780
#[cfg(not(feature = "python"))]
use dbn_macros::MockPyo3;

use crate::{
    enums::rtype,
    macros::{dbn_record, CsvSerialize, JsonSerialize, RecordDebug},
    Action, Error, FlagSet, InstrumentClass, MatchAlgorithm, Publisher, RType, Result,
    SecurityUpdateAction, Side, StatType, StatUpdateAction, UserDefinedInstrument, SYMBOL_CSTR_LEN,
};
pub(crate) use conv::as_u8_slice;
#[cfg(feature = "serde")]
pub(crate) use conv::cstr_serde;
pub use conv::{
    c_chars_to_str, str_to_c_chars, transmute_header_bytes, transmute_record,
    transmute_record_bytes, transmute_record_mut, ts_to_dt,
};

/// Common data for all Databento records. Always found at the beginning of a record
/// struct.
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(get_all, set_all, dict, module = "databento_dbn"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(test, derive(type_layout::TypeLayout))]
pub struct RecordHeader {
    /// The length of the record in 32-bit words.
    #[dbn(skip)]
    pub(crate) length: u8,
    /// The record type; with `0xe0..0x0F` specifying MBP levels size. Record types
    /// implement the trait [`HasRType`], and the [`has_rtype`][HasRType::has_rtype]
    /// function can be used to check if that type can be used to decode a message with
    /// a given rtype. The set of possible values is defined in [`rtype`].
    pub rtype: u8,
    /// The publisher ID assigned by Databento, which denotes the dataset and venue.
    pub publisher_id: u16,
    /// The numeric ID assigned to the instrument.
    pub instrument_id: u32,
    /// The matching-engine-received timestamp expressed as number of nanoseconds since
    /// the UNIX epoch.
    #[dbn(encode_order(0), unix_nanos)]
    pub ts_event: u64,
}

/// A market-by-order (MBO) tick message. The record of the
/// [`Mbo`](crate::enums::Schema::Mbo) schema.
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(set_all, dict, module = "databento_dbn", name = "MBOMsg"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(not(feature = "python"), derive(MockPyo3))] // bring `pyo3` attribute into scope
#[cfg_attr(test, derive(type_layout::TypeLayout))]
#[dbn_record(rtype::MBO)]
pub struct MboMsg {
    /// The common header.
    #[pyo3(get)]
    pub hd: RecordHeader,
    /// The order ID assigned at the venue.
    #[pyo3(get)]
    pub order_id: u64,
    /// The order price expressed as a signed integer where every 1 unit
    /// corresponds to 1e-9, i.e. 1/1,000,000,000 or 0.000000001.
    #[dbn(encode_order(4), fixed_price)]
    #[pyo3(get)]
    pub price: i64,
    /// The order quantity.
    #[dbn(encode_order(5))]
    #[pyo3(get)]
    pub size: u32,
    /// A bit field indicating event end, message characteristics, and data quality. See
    /// [`enums::flags`](crate::enums::flags) for possible values.
    #[pyo3(get)]
    pub flags: FlagSet,
    /// A channel ID within the venue.
    #[dbn(encode_order(6))]
    #[pyo3(get)]
    pub channel_id: u8,
    /// The event action. Can be **A**dd, **C**ancel, **M**odify, clea**R**,
    /// **T**rade, or **F**ill.
    #[dbn(c_char, encode_order(2))]
    pub action: c_char,
    /// The side that initiates the event. Can be **A**sk for a sell order (or sell
    /// aggressor in a trade), **B**id for a buy order (or buy aggressor in a trade), or
    /// **N**one where no side is specified by the original source.
    #[dbn(c_char, encode_order(3))]
    pub side: c_char,
    /// The capture-server-received timestamp expressed as number of nanoseconds since
    /// the UNIX epoch.
    #[dbn(encode_order(0), index_ts, unix_nanos)]
    #[pyo3(get)]
    pub ts_recv: u64,
    /// The delta of `ts_recv - ts_exchange_send`, max 2 seconds.
    #[pyo3(get)]
    pub ts_in_delta: i32,
    /// The message sequence number assigned at the venue.
    #[pyo3(get)]
    pub sequence: u32,
}

/// A level.
#[repr(C)]
#[derive(Clone, JsonSerialize, RecordDebug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(get_all, set_all, dict, module = "databento_dbn"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(test, derive(type_layout::TypeLayout))]
pub struct BidAskPair {
    /// The bid price.
    #[dbn(fixed_price)]
    pub bid_px: i64,
    /// The ask price.
    #[dbn(fixed_price)]
    pub ask_px: i64,
    /// The bid size.
    pub bid_sz: u32,
    /// The ask size.
    pub ask_sz: u32,
    /// The bid order count.
    pub bid_ct: u32,
    /// The ask order count.
    pub ask_ct: u32,
}

/// A price level consolidated from multiple venues.
#[repr(C)]
#[derive(Clone, JsonSerialize, RecordDebug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(get_all, set_all, dict, module = "databento_dbn"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(test, derive(type_layout::TypeLayout))]
pub struct ConsolidatedBidAskPair {
    /// The bid price.
    #[dbn(fixed_price)]
    pub bid_px: i64,
    /// The ask price.
    #[dbn(fixed_price)]
    pub ask_px: i64,
    /// The bid size.
    pub bid_sz: u32,
    /// The ask size.
    pub ask_sz: u32,
    /// The bid publisher ID assigned by Databento, which denotes the dataset and venue.
    #[dbn(fmt_method)]
    pub bid_pb: u16,
    // Reserved for later usage.
    #[doc(hidden)]
    #[cfg_attr(feature = "serde", serde(skip))]
    pub _reserved1: [c_char; 2],
    /// The ask publisher ID assigned by Databento, which denotes the dataset and venue.
    #[dbn(fmt_method)]
    pub ask_pb: u16,
    // Reserved for later usage.
    #[doc(hidden)]
    #[cfg_attr(feature = "serde", serde(skip))]
    pub _reserved2: [c_char; 2],
}

/// Market by price implementation with a book depth of 0. Equivalent to
/// MBP-0. The record of the [`Trades`](crate::enums::Schema::Trades) schema.
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(set_all, dict, module = "databento_dbn"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(not(feature = "python"), derive(MockPyo3))] // bring `pyo3` attribute into scope
#[cfg_attr(test, derive(type_layout::TypeLayout))]
#[dbn_record(rtype::MBP_0)]
pub struct TradeMsg {
    /// The common header.
    #[pyo3(get)]
    pub hd: RecordHeader,
    /// The order price expressed as a signed integer where every 1 unit
    /// corresponds to 1e-9, i.e. 1/1,000,000,000 or 0.000000001.
    #[dbn(fixed_price)]
    #[pyo3(get)]
    pub price: i64,
    /// The order quantity.
    #[pyo3(get)]
    pub size: u32,
    /// The event action. Always **T**rade in the trades schema.
    #[dbn(c_char, encode_order(2))]
    pub action: c_char,
    /// The side that initiates the trade. Can be **A**sk for a sell aggressor in a
    /// trade, **B**id for a buy aggressor in a trade, or **N**one where no side is
    /// specified by the original source.
    #[dbn(c_char, encode_order(3))]
    pub side: c_char,
    /// A bit field indicating event end, message characteristics, and data quality. See
    /// [`enums::flags`](crate::enums::flags) for possible values.
    #[pyo3(get)]
    pub flags: FlagSet,
    /// The depth of actual book change.
    #[dbn(encode_order(4))]
    #[pyo3(get)]
    pub depth: u8,
    /// The capture-server-received timestamp expressed as number of nanoseconds since
    /// the UNIX epoch.
    #[dbn(encode_order(0), index_ts, unix_nanos)]
    #[pyo3(get)]
    pub ts_recv: u64,
    /// The delta of `ts_recv - ts_exchange_send`, max 2 seconds.
    #[pyo3(get)]
    pub ts_in_delta: i32,
    /// The message sequence number assigned at the venue.
    #[pyo3(get)]
    pub sequence: u32,
}

/// Market by price implementation with a known book depth of 1. The record of the
/// [`Mbp1`](crate::enums::Schema::Mbp1) schema.
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(set_all, dict, module = "databento_dbn", name = "MBP1Msg"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(not(feature = "python"), derive(MockPyo3))] // bring `pyo3` attribute into scope
#[cfg_attr(test, derive(type_layout::TypeLayout))]
#[dbn_record(rtype::MBP_1)]
pub struct Mbp1Msg {
    /// The common header.
    #[pyo3(get)]
    pub hd: RecordHeader,
    /// The order price expressed as a signed integer where every 1 unit
    /// corresponds to 1e-9, i.e. 1/1,000,000,000 or 0.000000001.
    #[dbn(fixed_price)]
    #[pyo3(get)]
    pub price: i64,
    /// The order quantity.
    #[pyo3(get)]
    pub size: u32,
    /// The event action. Can be **A**dd, **C**ancel, **M**odify, clea**R**, or
    /// **T**rade.
    #[dbn(c_char, encode_order(2))]
    pub action: c_char,
    /// The side that initiates the event. Can be **A**sk for a sell order (or sell
    /// aggressor in a trade), **B**id for a buy order (or buy aggressor in a trade), or
    /// **N**one where no side is specified by the original source.
    #[dbn(c_char, encode_order(3))]
    pub side: c_char,
    /// A bit field indicating event end, message characteristics, and data quality. See
    /// [`enums::flags`](crate::enums::flags) for possible values.
    #[pyo3(get)]
    pub flags: FlagSet,
    /// The depth of actual book change.
    #[dbn(encode_order(4))]
    #[pyo3(get)]
    pub depth: u8,
    /// The capture-server-received timestamp expressed as number of nanoseconds since
    /// the UNIX epoch.
    #[dbn(encode_order(0), index_ts, unix_nanos)]
    #[pyo3(get)]
    pub ts_recv: u64,
    /// The delta of `ts_recv - ts_exchange_send`, max 2 seconds.
    #[pyo3(get)]
    pub ts_in_delta: i32,
    /// The message sequence number assigned at the venue.
    #[pyo3(get)]
    pub sequence: u32,
    /// The top of the order book.
    #[pyo3(get)]
    pub levels: [BidAskPair; 1],
}

/// Market by price implementation with a known book depth of 10. The record of the
/// [`Mbp10`](crate::enums::Schema::Mbp10) schema.
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(set_all, dict, module = "databento_dbn", name = "MBP10Msg"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(not(feature = "python"), derive(MockPyo3))] // bring `pyo3` attribute into scope
#[cfg_attr(test, derive(type_layout::TypeLayout))]
#[dbn_record(rtype::MBP_10)]
pub struct Mbp10Msg {
    /// The common header.
    #[pyo3(get)]
    pub hd: RecordHeader,
    /// The order price expressed as a signed integer where every 1 unit
    /// corresponds to 1e-9, i.e. 1/1,000,000,000 or 0.000000001.
    #[dbn(fixed_price)]
    #[pyo3(get)]
    pub price: i64,
    /// The order quantity.
    #[pyo3(get)]
    pub size: u32,
    /// The event action. Can be **A**dd, **C**ancel, **M**odify, clea**R**, or
    /// **T**rade.
    #[dbn(c_char, encode_order(2))]
    pub action: c_char,
    /// The side that initiates the event. Can be **A**sk for a sell order (or sell
    /// aggressor in a trade), **B**id for a buy order (or buy aggressor in a trade), or
    /// **N**one where no side is specified by the original source.
    #[dbn(c_char, encode_order(3))]
    pub side: c_char,
    /// A bit field indicating event end, message characteristics, and data quality. See
    /// [`enums::flags`](crate::enums::flags) for possible values.
    #[pyo3(get)]
    pub flags: FlagSet,
    /// The depth of actual book change.
    #[dbn(encode_order(4))]
    #[pyo3(get)]
    pub depth: u8,
    /// The capture-server-received timestamp expressed as number of nanoseconds since
    /// the UNIX epoch.
    #[dbn(encode_order(0), index_ts, unix_nanos)]
    #[pyo3(get)]
    pub ts_recv: u64,
    /// The delta of `ts_recv - ts_exchange_send`, max 2 seconds.
    #[pyo3(get)]
    pub ts_in_delta: i32,
    /// The message sequence number assigned at the venue.
    #[pyo3(get)]
    pub sequence: u32,
    /// The top 10 levels of the order book.
    #[pyo3(get)]
    pub levels: [BidAskPair; 10],
}

/// Subsampled market by price with a known book depth of 1. The record of the
/// [`Bbo1S`](crate::Schema::Bbo1S) and [`Bbo1M`](crate::Schema::Bbo1M) schemas.
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(set_all, dict, module = "databento_dbn", name = "BBOMsg"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(not(feature = "python"), derive(MockPyo3))] // bring `pyo3` attribute into scope
#[cfg_attr(test, derive(type_layout::TypeLayout))]
#[dbn_record(rtype::BBO_1S, rtype::BBO_1M)]
pub struct BboMsg {
    /// The common header.
    #[pyo3(get)]
    pub hd: RecordHeader,
    /// The price of the last trade expressed as a signed integer where every 1 unit
    /// corresponds to 1e-9, i.e. 1/1,000,000,000 or 0.000000001.
    #[dbn(fixed_price)]
    #[pyo3(get)]
    pub price: i64,
    /// The quantity of the last trade.
    #[pyo3(get)]
    pub size: u32,
    // Reserved for later usage.
    #[doc(hidden)]
    #[cfg_attr(feature = "serde", serde(skip))]
    pub _reserved1: u8,
    /// The side that initiated the last trade. Can be **A**sk for a sell order (or sell
    /// aggressor in a trade), **B**id for a buy order (or buy aggressor in a trade), or
    /// **N**one where no side is specified by the original source.
    #[dbn(c_char, encode_order(2))]
    pub side: c_char,
    /// A bit field indicating event end, message characteristics, and data quality. See
    /// [`enums::flags`](crate::enums::flags) for possible values.
    #[pyo3(get)]
    pub flags: FlagSet,
    // Reserved for later usage.
    #[doc(hidden)]
    #[cfg_attr(feature = "serde", serde(skip))]
    pub _reserved2: u8,
    /// The interval timestamp expressed as number of nanoseconds since the UNIX epoch.
    #[dbn(encode_order(0), index_ts, unix_nanos)]
    #[pyo3(get)]
    pub ts_recv: u64,
    // Reserved for later usage.
    #[doc(hidden)]
    #[cfg_attr(feature = "serde", serde(skip))]
    pub _reserved3: [u8; 4],
    /// The sequence number assigned at the venue of the last update.
    #[pyo3(get)]
    pub sequence: u32,
    /// The top of the order book.
    #[pyo3(get)]
    pub levels: [BidAskPair; 1],
}

/// Consolidated market by price implementation with a known book depth of 1. The record of the
/// [`Cbbo`](crate::Schema::Cbbo) schema.
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(set_all, dict, module = "databento_dbn", name = "CBBOMsg"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(not(feature = "python"), derive(MockPyo3))] // bring `pyo3` attribute into scope
#[cfg_attr(test, derive(type_layout::TypeLayout))]
#[dbn_record(rtype::CBBO, rtype::CBBO_1S, rtype::CBBO_1M, rtype::TCBBO)]
pub struct CbboMsg {
    /// The common header.
    #[pyo3(get)]
    pub hd: RecordHeader,
    /// The order price expressed as a signed integer where every 1 unit
    /// corresponds to 1e-9, i.e. 1/1,000,000,000 or 0.000000001.
    #[dbn(fixed_price)]
    #[pyo3(get)]
    pub price: i64,
    /// The order quantity.
    #[pyo3(get)]
    pub size: u32,
    /// The event action. Can be **A**dd, **C**ancel, **M**odify, clea**R**, or
    /// **T**rade.
    #[dbn(c_char, encode_order(2))]
    pub action: c_char,
    /// The side that initiates the event. Can be **A**sk for a sell order (or sell
    /// aggressor in a trade), **B**id for a buy order (or buy aggressor in a trade), or
    /// **N**one where no side is specified by the original source.
    #[dbn(c_char, encode_order(3))]
    pub side: c_char,
    /// A bit field indicating event end, message characteristics, and data quality. See
    /// [`enums::flags`](crate::enums::flags) for possible values.
    #[pyo3(get)]
    pub flags: FlagSet,
    // Reserved for future usage.
    #[doc(hidden)]
    #[cfg_attr(feature = "serde", serde(skip))]
    pub _reserved: [c_char; 1],
    /// The capture-server-received timestamp expressed as number of nanoseconds since
    /// the UNIX epoch.
    #[dbn(encode_order(0), index_ts, unix_nanos)]
    #[pyo3(get)]
    pub ts_recv: u64,
    /// The delta of `ts_recv - ts_exchange_send`, max 2 seconds.
    #[pyo3(get)]
    pub ts_in_delta: i32,
    /// The message sequence number assigned at the venue.
    #[pyo3(get)]
    pub sequence: u32,
    /// The top of the order book.
    #[pyo3(get)]
    pub levels: [ConsolidatedBidAskPair; 1],
}

/// The record of the [`Tbbo`](crate::enums::Schema::Tbbo) schema.
pub type TbboMsg = Mbp1Msg;
/// The record of the [`Bbo1S`](crate::enums::Schema::Bbo1S) schema.
pub type Bbo1SMsg = BboMsg;
/// The record of the [`Bbo1M`](crate::enums::Schema::Bbo1M) schema.
pub type Bbo1MMsg = BboMsg;
/// The record of the [`Cbbo1S`](crate::enums::Schema::Cbbo1S) schema.
pub type Cbbo1SMsg = CbboMsg;
/// The record of the [`Cbbo1M`](crate::enums::Schema::Cbbo1M) schema.
pub type Cbbo1MMsg = CbboMsg;
/// The record of the [`Tcbbo`](crate::enums::Schema::Tcbbo) schema.
pub type TcbboMsg = CbboMsg;

/// Open, high, low, close, and volume. The record of the following schemas:
/// - [`Ohlcv1S`](crate::enums::Schema::Ohlcv1S)
/// - [`Ohlcv1M`](crate::enums::Schema::Ohlcv1M)
/// - [`Ohlcv1H`](crate::enums::Schema::Ohlcv1H)
/// - [`Ohlcv1D`](crate::enums::Schema::Ohlcv1D)
/// - [`OhlcvEod`](crate::enums::Schema::OhlcvEod)
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(get_all, set_all, dict, module = "databento_dbn", name = "OHLCVMsg"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(test, derive(type_layout::TypeLayout))]
#[dbn_record(
    rtype::OHLCV_1S,
    rtype::OHLCV_1M,
    rtype::OHLCV_1H,
    rtype::OHLCV_1D,
    rtype::OHLCV_EOD,
    rtype::OHLCV_DEPRECATED
)]
pub struct OhlcvMsg {
    /// The common header.
    pub hd: RecordHeader,
    /// The open price for the bar.
    #[dbn(fixed_price)]
    pub open: i64,
    /// The high price for the bar.
    #[dbn(fixed_price)]
    pub high: i64,
    /// The low price for the bar.
    #[dbn(fixed_price)]
    pub low: i64,
    /// The close price for the bar.
    #[dbn(fixed_price)]
    pub close: i64,
    /// The total volume traded during the aggregation period.
    pub volume: u64,
}

/// A trading status update message. The record of the
/// [`Status`](crate::enums::Schema::Status) schema.
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(dict, module = "databento_dbn"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(not(feature = "python"), derive(MockPyo3))] // bring `pyo3` attribute into scope
#[cfg_attr(test, derive(type_layout::TypeLayout))]
#[dbn_record(rtype::STATUS)]
pub struct StatusMsg {
    /// The common header.
    #[pyo3(get, set)]
    pub hd: RecordHeader,
    /// The capture-server-received timestamp expressed as number of nanoseconds since
    /// the UNIX epoch.
    #[dbn(encode_order(0), index_ts, unix_nanos)]
    #[pyo3(get, set)]
    pub ts_recv: u64,
    /// The type of status change.
    #[dbn(fmt_method)]
    #[pyo3(get, set)]
    pub action: u16,
    /// Additional details about the cause of the status change.
    #[dbn(fmt_method)]
    #[pyo3(get, set)]
    pub reason: u16,
    /// Further information about the status change and its effect on trading.
    #[dbn(fmt_method)]
    #[pyo3(get, set)]
    pub trading_event: u16,
    /// The state of trading in the instrument.
    #[dbn(c_char)]
    #[pyo3(get, set)]
    pub is_trading: c_char,
    /// The state of quoting in the instrument.
    #[dbn(c_char)]
    #[pyo3(get, set)]
    pub is_quoting: c_char,
    /// The state of short sell restrictions for the instrument.
    #[dbn(c_char)]
    #[pyo3(get, set)]
    pub is_short_sell_restricted: c_char,
    // Filler for alignment.
    #[doc(hidden)]
    #[cfg_attr(feature = "serde", serde(skip))]
    pub _reserved: [u8; 7],
}

/// Definition of an instrument. The record of the
/// [`Definition`](crate::enums::Schema::Definition) schema.
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(dict, module = "databento_dbn"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(not(feature = "python"), derive(MockPyo3))] // bring `pyo3` attribute into scope
#[cfg_attr(test, derive(type_layout::TypeLayout))]
#[dbn_record(rtype::INSTRUMENT_DEF)]
pub struct InstrumentDefMsg {
    /// The common header.
    #[pyo3(get, set)]
    pub hd: RecordHeader,
    /// The capture-server-received timestamp expressed as number of nanoseconds since the
    /// UNIX epoch.
    #[dbn(encode_order(0), index_ts, unix_nanos)]
    #[pyo3(get, set)]
    pub ts_recv: u64,
    /// The minimum constant tick for the instrument in units of 1e-9, i.e.
    /// 1/1,000,000,000 or 0.000000001.
    #[dbn(fixed_price)]
    #[pyo3(get, set)]
    pub min_price_increment: i64,
    /// The multiplier to convert the venue’s display price to the conventional price,
    /// in units of 1e-9, i.e. 1/1,000,000,000 or 0.000000001.
    #[dbn(fixed_price)]
    #[pyo3(get, set)]
    pub display_factor: i64,
    /// The last eligible trade time expressed as a number of nanoseconds since the
    /// UNIX epoch. Will be [`crate::UNDEF_TIMESTAMP`] when null, such as for equities.
    #[dbn(unix_nanos)]
    #[pyo3(get, set)]
    pub expiration: u64,
    /// The time of instrument activation expressed as a number of nanoseconds since the
    /// UNIX epoch. Will be [`crate::UNDEF_TIMESTAMP`] when null, such as for equities.
    #[dbn(unix_nanos)]
    #[pyo3(get, set)]
    pub activation: u64,
    /// The allowable high limit price for the trading day in units of 1e-9, i.e.
    /// 1/1,000,000,000 or 0.000000001.
    #[dbn(fixed_price)]
    #[pyo3(get, set)]
    pub high_limit_price: i64,
    /// The allowable low limit price for the trading day in units of 1e-9, i.e.
    /// 1/1,000,000,000 or 0.000000001.
    #[dbn(fixed_price)]
    #[pyo3(get, set)]
    pub low_limit_price: i64,
    /// The differential value for price banding in units of 1e-9, i.e. 1/1,000,000,000
    /// or 0.000000001.
    #[dbn(fixed_price)]
    #[pyo3(get, set)]
    pub max_price_variation: i64,
    /// The trading session settlement price on `trading_reference_date`.
    #[dbn(fixed_price)]
    #[pyo3(get, set)]
    pub trading_reference_price: i64,
    /// The contract size for each instrument, in combination with `unit_of_measure`, in units
    /// of 1e-9, i.e. 1/1,000,000,000 or 0.000000001.
    #[dbn(fixed_price)]
    #[pyo3(get, set)]
    pub unit_of_measure_qty: i64,
    /// The value currently under development by the venue. Converted to units of 1e-9, i.e.
    /// 1/1,000,000,000 or 0.000000001.
    #[dbn(fixed_price)]
    #[pyo3(get, set)]
    pub min_price_increment_amount: i64,
    /// The value used for price calculation in spread and leg pricing in units of 1e-9,
    /// i.e. 1/1,000,000,000 or 0.000000001.
    #[dbn(fixed_price)]
    #[pyo3(get, set)]
    pub price_ratio: i64,
    /// The strike price of the option. Converted to units of 1e-9, i.e. 1/1,000,000,000
    /// or 0.000000001.
    #[dbn(fixed_price, encode_order(46))]
    #[pyo3(get, set)]
    pub strike_price: i64,
    /// A bitmap of instrument eligibility attributes.
    #[dbn(fmt_binary)]
    #[pyo3(get, set)]
    pub inst_attrib_value: i32,
    /// The `instrument_id` of the first underlying instrument.
    #[pyo3(get, set)]
    pub underlying_id: u32,
    /// The instrument ID assigned by the publisher. May be the same as `instrument_id`.
    #[pyo3(get, set)]
    pub raw_instrument_id: u32,
    /// The implied book depth on the price level data feed.
    #[pyo3(get, set)]
    pub market_depth_implied: i32,
    /// The (outright) book depth on the price level data feed.
    #[pyo3(get, set)]
    pub market_depth: i32,
    /// The market segment of the instrument.
    #[pyo3(get, set)]
    pub market_segment_id: u32,
    /// The maximum trading volume for the instrument.
    #[pyo3(get, set)]
    pub max_trade_vol: u32,
    /// The minimum order entry quantity for the instrument.
    #[pyo3(get, set)]
    pub min_lot_size: i32,
    /// The minimum quantity required for a block trade of the instrument.
    #[pyo3(get, set)]
    pub min_lot_size_block: i32,
    /// The minimum quantity required for a round lot of the instrument. Multiples of
    /// this quantity are also round lots.
    #[pyo3(get, set)]
    pub min_lot_size_round_lot: i32,
    /// The minimum trading volume for the instrument.
    #[pyo3(get, set)]
    pub min_trade_vol: u32,
    /// The number of deliverables per instrument, i.e. peak days.
    #[pyo3(get, set)]
    pub contract_multiplier: i32,
    /// The quantity that a contract will decay daily, after `decay_start_date` has
    /// been reached.
    #[pyo3(get, set)]
    pub decay_quantity: i32,
    /// The fixed contract value assigned to each instrument.
    #[pyo3(get, set)]
    pub original_contract_size: i32,
    /// The trading session date corresponding to the settlement price in
    /// `trading_reference_price`, in number of days since the UNIX epoch.
    #[pyo3(get, set)]
    pub trading_reference_date: u16,
    /// The channel ID assigned at the venue.
    #[pyo3(get, set)]
    pub appl_id: i16,
    /// The calendar year reflected in the instrument symbol.
    #[pyo3(get, set)]
    pub maturity_year: u16,
    /// The date at which a contract will begin to decay.
    #[pyo3(get, set)]
    pub decay_start_date: u16,
    /// The channel ID assigned by Databento as an incrementing integer starting at zero.
    #[pyo3(get, set)]
    pub channel_id: u16,
    /// The currency used for price fields.
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub currency: [c_char; 4],
    /// The currency used for settlement, if different from `currency`.
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub settl_currency: [c_char; 4],
    /// The strategy type of the spread.
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub secsubtype: [c_char; 6],
    /// The instrument raw symbol assigned by the publisher.
    #[dbn(encode_order(2), fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub raw_symbol: [c_char; SYMBOL_CSTR_LEN],
    /// The security group code of the instrument.
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub group: [c_char; 21],
    /// The exchange used to identify the instrument.
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub exchange: [c_char; 5],
    /// The underlying asset code (product code) of the instrument.
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub asset: [c_char; 7],
    /// The ISO standard instrument categorization code.
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub cfi: [c_char; 7],
    /// The type of the instrument, e.g. FUT for future or future spread.
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub security_type: [c_char; 7],
    /// The unit of measure for the instrument’s original contract size, e.g. USD or LBS.
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub unit_of_measure: [c_char; 31],
    /// The symbol of the first underlying instrument.
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub underlying: [c_char; 21],
    /// The currency of [`strike_price`](Self::strike_price).
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub strike_price_currency: [c_char; 4],
    /// The classification of the instrument.
    #[dbn(c_char, encode_order(4))]
    #[pyo3(set)]
    pub instrument_class: c_char,
    /// The matching algorithm used for the instrument, typically **F**IFO.
    #[dbn(c_char)]
    #[pyo3(set)]
    pub match_algorithm: c_char,
    /// The current trading state of the instrument.
    #[pyo3(get, set)]
    pub md_security_trading_status: u8,
    /// The price denominator of the main fraction.
    #[pyo3(get, set)]
    pub main_fraction: u8,
    ///  The number of digits to the right of the tick mark, to display fractional prices.
    #[pyo3(get, set)]
    pub price_display_format: u8,
    /// The type indicators for the settlement price, as a bitmap.
    #[pyo3(get, set)]
    pub settl_price_type: u8,
    /// The price denominator of the sub fraction.
    #[pyo3(get, set)]
    pub sub_fraction: u8,
    /// The product complex of the instrument.
    #[pyo3(get, set)]
    pub underlying_product: u8,
    /// Indicates if the instrument definition has been added, modified, or deleted.
    #[dbn(c_char, encode_order(3))]
    #[pyo3(set)]
    pub security_update_action: c_char,
    /// The calendar month reflected in the instrument symbol.
    #[pyo3(get, set)]
    pub maturity_month: u8,
    /// The calendar day reflected in the instrument symbol, or 0.
    #[pyo3(get, set)]
    pub maturity_day: u8,
    /// The calendar week reflected in the instrument symbol, or 0.
    #[pyo3(get, set)]
    pub maturity_week: u8,
    /// Indicates if the instrument is user defined: **Y**es or **N**o.
    #[pyo3(set)]
    pub user_defined_instrument: UserDefinedInstrument,
    /// The type of `contract_multiplier`. Either `1` for hours, or `2` for days.
    #[pyo3(get, set)]
    pub contract_multiplier_unit: i8,
    /// The schedule for delivering electricity.
    #[pyo3(get, set)]
    pub flow_schedule_type: i8,
    /// The tick rule of the spread.
    #[pyo3(get, set)]
    pub tick_rule: u8,
    // Filler for alignment.
    #[doc(hidden)]
    #[cfg_attr(feature = "serde", serde(skip))]
    pub _reserved: [u8; 10],
}

/// An auction imbalance message.
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(set_all, dict, module = "databento_dbn"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(not(feature = "python"), derive(MockPyo3))] // bring `pyo3` attribute into scope
#[cfg_attr(test, derive(type_layout::TypeLayout))]
#[dbn_record(rtype::IMBALANCE)]
pub struct ImbalanceMsg {
    /// The common header.
    #[pyo3(get)]
    pub hd: RecordHeader,
    /// The capture-server-received timestamp expressed as the number of nanoseconds
    /// since the UNIX epoch.
    #[dbn(encode_order(0), index_ts, unix_nanos)]
    #[pyo3(get)]
    pub ts_recv: u64,
    /// The price at which the imbalance shares are calculated, where every 1 unit corresponds to
    /// 1e-9, i.e. 1/1,000,000,000 or 0.000000001.
    #[dbn(fixed_price)]
    #[pyo3(get)]
    pub ref_price: i64,
    /// Reserved for future use.
    #[pyo3(get)]
    pub auction_time: u64,
    /// The hypothetical auction-clearing price for both cross and continuous orders.
    #[dbn(fixed_price)]
    #[pyo3(get)]
    pub cont_book_clr_price: i64,
    /// The hypothetical auction-clearing price for cross orders only.
    #[dbn(fixed_price)]
    #[pyo3(get)]
    pub auct_interest_clr_price: i64,
    /// Reserved for future use.
    #[dbn(fixed_price)]
    #[pyo3(get)]
    pub ssr_filling_price: i64,
    /// Reserved for future use.
    #[dbn(fixed_price)]
    #[pyo3(get)]
    pub ind_match_price: i64,
    /// Reserved for future use.
    #[dbn(fixed_price)]
    #[pyo3(get)]
    pub upper_collar: i64,
    /// Reserved for future use.
    #[dbn(fixed_price)]
    #[pyo3(get)]
    pub lower_collar: i64,
    /// The quantity of shares that are eligible to be matched at `ref_price`.
    #[pyo3(get)]
    pub paired_qty: u32,
    /// The quantity of shares that are not paired at `ref_price`.
    #[pyo3(get)]
    pub total_imbalance_qty: u32,
    /// Reserved for future use.
    #[pyo3(get)]
    pub market_imbalance_qty: u32,
    /// Reserved for future use.
    #[pyo3(get)]
    pub unpaired_qty: u32,
    /// Venue-specific character code indicating the auction type.
    #[dbn(c_char)]
    pub auction_type: c_char,
    /// The market side of the `total_imbalance_qty`. Can be **A**sk, **B**id, or **N**one.
    #[dbn(c_char)]
    pub side: c_char,
    /// Reserved for future use.
    #[pyo3(get)]
    pub auction_status: u8,
    /// Reserved for future use.
    #[pyo3(get)]
    pub freeze_status: u8,
    /// Reserved for future use.
    #[pyo3(get)]
    pub num_extensions: u8,
    /// Reserved for future use.
    #[dbn(c_char)]
    pub unpaired_side: c_char,
    /// Venue-specific character code. For Nasdaq, contains the raw Price Variation Indicator.
    #[dbn(c_char)]
    pub significant_imbalance: c_char,
    // Filler for alignment.
    #[doc(hidden)]
    #[cfg_attr(feature = "serde", serde(skip))]
    pub _reserved: [u8; 1],
}

/// A statistics message. A catchall for various data disseminated by publishers.
/// The [`stat_type`](Self::stat_type) indicates the statistic contained in the message.
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(get_all, set_all, dict, module = "databento_dbn"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(not(feature = "python"), derive(MockPyo3))] // bring `pyo3` attribute into scope
#[cfg_attr(test, derive(type_layout::TypeLayout))]
#[dbn_record(rtype::STATISTICS)]
pub struct StatMsg {
    /// The common header.
    pub hd: RecordHeader,
    /// The capture-server-received timestamp expressed as the number of nanoseconds
    /// since the UNIX epoch.
    #[dbn(encode_order(0), index_ts, unix_nanos)]
    pub ts_recv: u64,
    /// The reference timestamp of the statistic value expressed as the number of
    /// nanoseconds since the UNIX epoch. Will be [`crate::UNDEF_TIMESTAMP`] when
    /// unused.
    #[dbn(unix_nanos)]
    pub ts_ref: u64,
    /// The value for price statistics expressed as a signed integer where every 1 unit
    /// corresponds to 1e-9, i.e. 1/1,000,000,000 or 0.000000001. Will be
    /// [`crate::UNDEF_PRICE`] when unused.
    #[dbn(fixed_price)]
    pub price: i64,
    /// The value for non-price statistics. Will be [`crate::UNDEF_STAT_QUANTITY`] when
    /// unused.
    pub quantity: i32,
    /// The message sequence number assigned at the venue.
    pub sequence: u32,
    /// The delta of `ts_recv - ts_exchange_send`, max 2 seconds.
    pub ts_in_delta: i32,
    /// The type of statistic value contained in the message. Refer to the
    /// [`StatType`](crate::enums::StatType) for variants.
    #[dbn(fmt_method)]
    pub stat_type: u16,
    /// A channel ID within the venue.
    pub channel_id: u16,
    /// Indicates if the statistic is newly added (1) or deleted (2). (Deleted is only used with
    /// some stat types)
    #[dbn(fmt_method)]
    pub update_action: u8,
    /// Additional flags associate with certain stat types.
    #[dbn(fmt_binary)]
    pub stat_flags: u8,
    // Filler for alignment
    #[doc(hidden)]
    #[cfg_attr(feature = "serde", serde(skip))]
    pub _reserved: [u8; 6],
}

/// An error message from the Databento Live Subscription Gateway (LSG).
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(dict, module = "databento_dbn"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(not(feature = "python"), derive(MockPyo3))] // bring `pyo3` attribute into scope
#[cfg_attr(test, derive(type_layout::TypeLayout))]
#[dbn_record(rtype::ERROR)]
pub struct ErrorMsg {
    /// The common header.
    #[pyo3(get, set)]
    pub hd: RecordHeader,
    /// The error message.
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub err: [c_char; 302],
    /// The error code. Currently unused.
    #[pyo3(get, set)]
    pub code: u8,
    /// Sometimes multiple errors are sent together. This field will be non-zero for the
    /// last error.
    #[pyo3(get, set)]
    pub is_last: u8,
}

/// A symbol mapping message which maps a symbol of one [`SType`](crate::enums::SType)
/// to another.
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(dict, module = "databento_dbn"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(not(feature = "python"), derive(MockPyo3))] // bring `pyo3` attribute into scope
#[cfg_attr(test, derive(type_layout::TypeLayout))]
#[dbn_record(rtype::SYMBOL_MAPPING)]
pub struct SymbolMappingMsg {
    /// The common header.
    #[pyo3(get, set)]
    pub hd: RecordHeader,
    // TODO(carter): special serialization to string?
    /// The input symbology type of `stype_in_symbol`.
    #[dbn(fmt_method)]
    #[pyo3(get, set)]
    pub stype_in: u8,
    /// The input symbol.
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub stype_in_symbol: [c_char; SYMBOL_CSTR_LEN],
    /// The output symbology type of `stype_out_symbol`.
    #[dbn(fmt_method)]
    #[pyo3(get, set)]
    pub stype_out: u8,
    /// The output symbol.
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub stype_out_symbol: [c_char; SYMBOL_CSTR_LEN],
    /// The start of the mapping interval expressed as the number of nanoseconds since
    /// the UNIX epoch.
    #[dbn(unix_nanos)]
    #[pyo3(get, set)]
    pub start_ts: u64,
    /// The end of the mapping interval expressed as the number of nanoseconds since
    /// the UNIX epoch.
    #[dbn(unix_nanos)]
    #[pyo3(get, set)]
    pub end_ts: u64,
}

/// A non-error message from the Databento Live Subscription Gateway (LSG). Also used
/// for heartbeating.
#[repr(C)]
#[derive(Clone, CsvSerialize, JsonSerialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(dict, module = "databento_dbn"),
    derive(crate::macros::PyFieldDesc)
)]
#[cfg_attr(not(feature = "python"), derive(MockPyo3))] // bring `pyo3` attribute into scope
#[cfg_attr(test, derive(type_layout::TypeLayout))]
#[dbn_record(rtype::SYSTEM)]
pub struct SystemMsg {
    /// The common header.
    #[pyo3(get, set)]
    pub hd: RecordHeader,
    /// The message from the Databento Live Subscription Gateway (LSG).
    #[dbn(fmt_method)]
    #[cfg_attr(feature = "serde", serde(with = "conv::cstr_serde"))]
    pub msg: [c_char; 303],
    /// Type of system message, currently unused.
    #[pyo3(get, set)]
    pub code: u8,
}

/// Used for polymorphism around types all beginning with a [`RecordHeader`] where
/// `rtype` is the discriminant used for indicating the type of record.
pub trait Record {
    /// Returns a reference to the `RecordHeader` that comes at the beginning of all
    /// record types.
    fn header(&self) -> &RecordHeader;

    /// Returns the size of the record in bytes.
    fn record_size(&self) -> usize {
        self.header().record_size()
    }

    /// Tries to convert the raw record type into an enum which is useful for exhaustive
    /// pattern matching.
    ///
    /// # Errors
    /// This function returns an error if the `rtype` field does not
    /// contain a valid, known [`RType`].
    fn rtype(&self) -> crate::Result<RType> {
        self.header().rtype()
    }

    /// Tries to convert the raw `publisher_id` into an enum which is useful for
    /// exhaustive pattern matching.
    ///
    /// # Errors
    /// This function returns an error if the `publisher_id` does not correspond with
    /// any known [`Publisher`].
    fn publisher(&self) -> crate::Result<Publisher> {
        self.header().publisher()
    }

    /// Returns the raw primary timestamp for the record.
    ///
    /// This timestamp should be used for sorting records as well as indexing into any
    /// symbology data structure.
    fn raw_index_ts(&self) -> u64 {
        self.header().ts_event
    }

    /// Returns the primary timestamp for the record. Returns `None` if the primary
    /// timestamp contains the sentinel value for a null timestamp.
    ///
    /// This timestamp should be used for sorting records as well as indexing into any
    /// symbology data structure.
    fn index_ts(&self) -> Option<time::OffsetDateTime> {
        ts_to_dt(self.raw_index_ts())
    }

    /// Returns the primary date for the record; the date component of the primary
    /// timestamp (`index_ts()`). Returns `None` if the primary timestamp contains the
    /// sentinel value for a null timestamp.
    fn index_date(&self) -> Option<time::Date> {
        self.index_ts().map(|dt| dt.date())
    }
}

/// Used for polymorphism around mutable types beginning with a [`RecordHeader`].
pub trait RecordMut {
    /// Returns a mutable reference to the `RecordHeader` that comes at the beginning of
    /// all record types.
    fn header_mut(&mut self) -> &mut RecordHeader;
}

/// An extension of the [`Record`] trait for types with a static [`RType`]. Used for
/// determining if a rtype matches a type.
pub trait HasRType: Record + RecordMut {
    /// Returns `true` if `rtype` matches the value associated with the implementing type.
    fn has_rtype(rtype: u8) -> bool;
}

/// Wrapper object for records that include the live gateway send timestamp (`ts_out`).
#[repr(C)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trivial_copy", derive(Copy))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct WithTsOut<T: HasRType> {
    /// The inner record.
    pub rec: T,
    /// The live gateway send timestamp expressed as number of nanoseconds since the UNIX epoch.
    pub ts_out: u64,
}

#[cfg(test)]
mod tests {
    use mem::offset_of;
    use rstest::rstest;
    use type_layout::{Field, TypeLayout};

    use crate::Schema;
    use crate::UNDEF_TIMESTAMP;

    use super::*;

    const OHLCV_MSG: OhlcvMsg = OhlcvMsg {
        hd: RecordHeader {
            length: 56,
            rtype: rtype::OHLCV_1S,
            publisher_id: 1,
            instrument_id: 5482,
            ts_event: 1609160400000000000,
        },
        open: 372025000000000,
        high: 372050000000000,
        low: 372025000000000,
        close: 372050000000000,
        volume: 57,
    };

    #[test]
    fn test_transmute_record_bytes() {
        unsafe {
            let ohlcv_bytes = std::slice::from_raw_parts(
                &OHLCV_MSG as *const OhlcvMsg as *const u8,
                mem::size_of::<OhlcvMsg>(),
            )
            .to_vec();
            let ohlcv = transmute_record_bytes::<OhlcvMsg>(ohlcv_bytes.as_slice()).unwrap();
            assert_eq!(*ohlcv, OHLCV_MSG);
        };
    }

    #[test]
    #[should_panic]
    fn test_transmute_record_bytes_small_buffer() {
        let source = OHLCV_MSG;
        unsafe {
            let slice = std::slice::from_raw_parts(
                &source as *const OhlcvMsg as *const u8,
                mem::size_of::<OhlcvMsg>() - 5,
            );
            transmute_record_bytes::<OhlcvMsg>(slice);
        };
    }

    #[test]
    fn test_transmute_record() {
        let source = Box::new(OHLCV_MSG);
        let ohlcv_ref: &OhlcvMsg = unsafe { transmute_record(&source.hd) }.unwrap();
        assert_eq!(*ohlcv_ref, OHLCV_MSG);
    }

    #[test]
    fn test_transmute_record_mut() {
        let mut source = Box::new(OHLCV_MSG);
        let ohlcv_ref: &OhlcvMsg = unsafe { transmute_record_mut(&mut source.hd) }.unwrap();
        assert_eq!(*ohlcv_ref, OHLCV_MSG);
    }

    #[rstest]
    #[case::header(RecordHeader::default::<MboMsg>(rtype::MBO), 16)]
    #[case::mbo(MboMsg::default(), 56)]
    #[case::ba_pair(BidAskPair::default(), 32)]
    #[case::mbp1(Mbp1Msg::default(), mem::size_of::<TradeMsg>() + mem::size_of::<BidAskPair>())]
    #[case::mbp10(Mbp10Msg::default(), mem::size_of::<TradeMsg>() + mem::size_of::<BidAskPair>() * 10)]
    #[case::bbo(BboMsg::default_for_schema(Schema::Bbo1S), mem::size_of::<Mbp1Msg>())]
    #[case::cbbo(CbboMsg::default_for_schema(Schema::Cbbo), mem::size_of::<Mbp1Msg>())]
    #[case::trade(TradeMsg::default(), 48)]
    #[case::definition(InstrumentDefMsg::default(), 400)]
    #[case::status(StatusMsg::default(), 40)]
    #[case::imbalance(ImbalanceMsg::default(), 112)]
    #[case::stat(StatMsg::default(), 64)]
    #[case::error(ErrorMsg::default(), 320)]
    #[case::symbol_mapping(SymbolMappingMsg::default(), 176)]
    #[case::system(SystemMsg::default(), 320)]
    #[case::with_ts_out(WithTsOut::new(SystemMsg::default(), 0), mem::size_of::<SystemMsg>() + 8)]
    fn test_sizes<R: Sized>(#[case] _rec: R, #[case] exp: usize) {
        assert_eq!(mem::size_of::<R>(), exp);
        assert!(mem::size_of::<R>() <= crate::MAX_RECORD_LEN);
    }

    #[rstest]
    #[case::header(RecordHeader::default::<MboMsg>(rtype::MBO))]
    #[case::mbo(MboMsg::default())]
    #[case::ba_pair(BidAskPair::default())]
    #[case::mbp1(Mbp1Msg::default())]
    #[case::bbo(BboMsg::default_for_schema(crate::Schema::Bbo1S))]
    #[case::cbbo(CbboMsg::default_for_schema(crate::Schema::Cbbo))]
    #[case::mbp10(Mbp10Msg::default())]
    #[case::trade(TradeMsg::default())]
    #[case::definition(InstrumentDefMsg::default())]
    #[case::status(StatusMsg::default())]
    #[case::imbalance(ImbalanceMsg::default())]
    #[case::stat(StatMsg::default())]
    #[case::error(ErrorMsg::default())]
    #[case::symbol_mapping(SymbolMappingMsg::default())]
    #[case::system(SystemMsg::default())]
    fn test_alignment_and_no_padding<R: TypeLayout>(#[case] _rec: R) {
        let layout = R::type_layout();
        assert_eq!(layout.alignment, 8, "Unexpected alignment: {layout}");
        for field in layout.fields.iter() {
            assert!(
                matches!(field, Field::Field { .. }),
                "Detected padding: {layout}"
            );
        }
    }

    #[test]
    fn test_bbo_alignment_matches_mbp1() {
        assert_eq!(offset_of!(BboMsg, hd), offset_of!(Mbp1Msg, hd));
        assert_eq!(offset_of!(BboMsg, price), offset_of!(Mbp1Msg, price));
        assert_eq!(offset_of!(BboMsg, size), offset_of!(Mbp1Msg, size));
        assert_eq!(offset_of!(BboMsg, side), offset_of!(Mbp1Msg, side));
        assert_eq!(offset_of!(BboMsg, flags), offset_of!(Mbp1Msg, flags));
        assert_eq!(offset_of!(BboMsg, ts_recv), offset_of!(Mbp1Msg, ts_recv));
        assert_eq!(offset_of!(BboMsg, sequence), offset_of!(Mbp1Msg, sequence));
        assert_eq!(offset_of!(BboMsg, levels), offset_of!(Mbp1Msg, levels));
    }

    #[test]
    fn test_mbo_index_ts() {
        let rec = MboMsg {
            ts_recv: 1,
            ..Default::default()
        };
        assert_eq!(rec.raw_index_ts(), 1);
    }

    #[test]
    fn test_def_index_ts() {
        let rec = InstrumentDefMsg {
            ts_recv: 1,
            ..Default::default()
        };
        assert_eq!(rec.raw_index_ts(), 1);
    }

    #[test]
    fn test_db_ts_always_valid_time_offsetdatetime() {
        assert!(time::OffsetDateTime::from_unix_timestamp_nanos(0).is_ok());
        assert!(time::OffsetDateTime::from_unix_timestamp_nanos((u64::MAX - 1) as i128).is_ok());
        assert!(time::OffsetDateTime::from_unix_timestamp_nanos(UNDEF_TIMESTAMP as i128).is_ok());
    }

    #[test]
    fn test_record_object_safe() {
        let _record: Box<dyn Record> = Box::new(ErrorMsg::new(1, "Boxed record", true));
    }
}