dbn/
lib.rs

1//! The official crate for working with [**D**atabento](https://databento.com)
2//! **B**inary E**n**coding (DBN), an extremely fast message encoding and storage format
3//! for normalized market data. The DBN specification includes a simple, self-describing
4//! metadata header and a fixed set of struct definitions, which enforce a standardized
5//! way to normalize market data.
6//!
7//! All official Databento client libraries use DBN under the hood, both as a data
8//! interchange format and for in-memory representation of data. DBN is also the default
9//! encoding for all Databento APIs, including live data streaming, historical data
10//! streaming, and batch flat files. For more information about the encoding, read our
11//! [introduction to DBN](https://databento.com/docs/standards-and-conventions/databento-binary-encoding).
12//!
13//! The crate supports reading and writing DBN files and streams, as well as converting
14//! them to other [`Encoding`]s. It can also be used to update legacy
15//! DBZ files to DBN.
16//!
17//! This crate provides:
18//! - [Decoders](crate::decode) for DBN and DBZ (the precursor to DBN), both
19//!   sync and async, with the `async` feature flag
20//! - [Encoders](crate::encode) for CSV, DBN, and JSON, both sync and async,
21//!   with the `async` feature flag
22//! - [Normalized market data struct definitions](crate::record) corresponding to the
23//!   different market data schemas offered by Databento
24//! - A [wrapper type](crate::RecordRef) for holding a reference to a record struct of
25//!   a dynamic type
26//! - Helper functions and [macros] for common tasks
27//!
28//! # Feature flags
29//! - `async`: enables async decoding and encoding
30//! - `python`: enables `pyo3` bindings
31//! - `serde`: enables deriving `serde` traits for types
32//! - `trivial_copy`: enables deriving the `Copy` trait for records
33
34// Experimental feature to allow docs.rs to display features
35#![cfg_attr(docsrs, feature(doc_auto_cfg))]
36#![deny(missing_docs)]
37#![deny(rustdoc::broken_intra_doc_links)]
38#![deny(clippy::missing_errors_doc)]
39
40pub mod compat;
41pub mod decode;
42pub mod encode;
43pub mod enums;
44pub mod error;
45pub mod flags;
46mod json_writer;
47pub mod macros;
48pub mod metadata;
49pub mod pretty;
50pub mod publishers;
51#[cfg(feature = "python")]
52pub mod python;
53pub mod record;
54mod record_enum;
55pub mod record_ref;
56pub mod symbol_map;
57#[cfg(test)]
58mod test_utils;
59pub mod v1;
60pub mod v2;
61pub mod v3;
62
63pub use crate::{
64    enums::{
65        rtype, Action, Compression, Encoding, ErrorCode, InstrumentClass, MatchAlgorithm, RType,
66        SType, Schema, SecurityUpdateAction, Side, StatType, StatUpdateAction, StatusAction,
67        StatusReason, SystemCode, TradingEvent, TriState, UserDefinedInstrument,
68        VersionUpgradePolicy,
69    },
70    error::{Error, Result},
71    flags::FlagSet,
72    metadata::{MappingInterval, Metadata, MetadataBuilder, SymbolMapping},
73    publishers::{Dataset, Publisher, Venue},
74    record::{
75        Bbo1MMsg, Bbo1SMsg, BboMsg, BidAskPair, Cbbo1MMsg, Cbbo1SMsg, CbboMsg, Cmbp1Msg,
76        ConsolidatedBidAskPair, ErrorMsg, HasRType, ImbalanceMsg, InstrumentDefMsg, MboMsg,
77        Mbp10Msg, Mbp1Msg, OhlcvMsg, Record, RecordHeader, RecordMut, StatMsg, StatusMsg,
78        SymbolMappingMsg, SystemMsg, TbboMsg, TradeMsg, WithTsOut,
79    },
80    record_enum::{RecordEnum, RecordRefEnum},
81    record_ref::RecordRef,
82    symbol_map::{PitSymbolMap, SymbolIndex, TsSymbolMap},
83};
84
85/// The current version of the DBN encoding, which is different from the crate version.
86pub const DBN_VERSION: u8 = 3;
87
88/// The length of fixed-length symbol strings.
89pub const SYMBOL_CSTR_LEN: usize = v3::SYMBOL_CSTR_LEN;
90/// The length of the fixed-length asset string.
91pub const ASSET_CSTR_LEN: usize = v3::ASSET_CSTR_LEN;
92
93const METADATA_DATASET_CSTR_LEN: usize = 16;
94const METADATA_RESERVED_LEN: usize = 53;
95/// Excludes magic string, version, and length.
96const METADATA_FIXED_LEN: usize = 100;
97const NULL_LIMIT: u64 = 0;
98const NULL_RECORD_COUNT: u64 = u64::MAX;
99const NULL_SCHEMA: u16 = u16::MAX;
100const NULL_STYPE: u8 = u8::MAX;
101
102/// The denominator of fixed prices in DBN.
103pub const FIXED_PRICE_SCALE: i64 = 1_000_000_000;
104/// The sentinel value for an unset or null price.
105pub const UNDEF_PRICE: i64 = i64::MAX;
106/// The sentinel value for an unset or null order quantity.
107pub const UNDEF_ORDER_SIZE: u32 = u32::MAX;
108/// The sentinel value for an unset or null stat quantity.
109pub const UNDEF_STAT_QUANTITY: i64 = v3::UNDEF_STAT_QUANTITY;
110/// The sentinel value for an unset or null timestamp.
111pub const UNDEF_TIMESTAMP: u64 = u64::MAX;
112/// The length in bytes of the largest record type.
113pub const MAX_RECORD_LEN: usize = std::mem::size_of::<WithTsOut<v3::InstrumentDefMsg>>();