nautilus_serialization/lib.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3// https://nautechsystems.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Data serialization and format conversion for [NautilusTrader](https://nautilustrader.io).
17//!
18//! The `nautilus-serialization` crate provides data serialization capabilities for converting
19//! trading data between different formats including Apache Arrow and Cap'n Proto.
20//! This enables efficient data storage, retrieval, and interoperability across different systems:
21//!
22//! - **Apache Arrow integration**: Schema definitions and encoding/decoding for market data types.
23//! - **Parquet file operations**: High-performance columnar storage for historical data analysis.
24//! - **Record batch processing**: Efficient batch operations for time-series data.
25//! - **Schema management**: Type-safe schema definitions with metadata preservation.
26//! - **Cross-format conversion**: Data interchange between Arrow, Cap'n Proto, and native types.
27//! - **Cap'n Proto serialization**: Zero-copy, schema-based serialization for efficient data interchange (requires `capnp` feature).
28//! - **SBE decode utilities**: Zero-copy cursor and shared decode errors for SBE parsers (requires `sbe` feature).
29//!
30//! # NautilusTrader
31//!
32//! [NautilusTrader](https://nautilustrader.io) is an open-source, production-grade, Rust-native
33//! engine for multi-asset, multi-venue trading systems.
34//!
35//! The system spans research, deterministic simulation, and live execution within a single
36//! event-driven architecture, providing research-to-live semantic parity.
37//!
38//! # Feature Flags
39//!
40//! This crate provides feature flags to control source code inclusion during compilation,
41//! depending on the intended use case, i.e. whether to provide Python bindings
42//! for the [nautilus_trader](https://pypi.org/project/nautilus_trader) Python package,
43//! or as part of a Rust only build.
44//!
45//! - `arrow`: Enables Apache Arrow schema definitions and RecordBatch encoding/decoding.
46//! - `arrow-display`: Enables display-friendly Arrow encoders for market data and requires `arrow`.
47//! - `capnp`: Enables [Cap'n Proto](https://capnproto.org) serialization support.
48//! - `extension-module`: Builds as a Python extension module.
49//! - `high-precision`: Enables
50//! [high-precision mode](https://nautilustrader.io/docs/nightly/getting_started/installation/#precision-mode)
51//! to use 128-bit value types.
52//! - `python`: Enables Python bindings from [PyO3](https://pyo3.rs).
53//! - `sbe`: Enables generic SBE (Simple Binary Encoding) decode utilities.
54//!
55//! **Warning:** SBE and Cap'n Proto schemas are not yet stable and may break between releases.
56
57#![warn(rustc::all)]
58#![warn(clippy::pedantic)]
59#![deny(unsafe_code)]
60#![deny(unsafe_op_in_unsafe_fn)]
61#![deny(nonstandard_style)]
62#![deny(missing_debug_implementations)]
63#![deny(clippy::missing_errors_doc)]
64#![deny(clippy::missing_panics_doc)]
65#![deny(rustdoc::broken_intra_doc_links)]
66#![allow(
67 clippy::doc_markdown,
68 reason = "serialization docs are heavy on Arrow and domain-specific type names where blanket backticks add noise"
69)]
70#![allow(
71 clippy::too_many_lines,
72 reason = "wide encode and decode functions mirror protocol schemas and Arrow record layouts"
73)]
74#![allow(
75 clippy::implicit_hasher,
76 reason = "serialization metadata uses a standardized HashMap<String, String> shape across traits and codecs"
77)]
78#![allow(
79 clippy::similar_names,
80 reason = "domain terms such as trade/trader and side/size are intentionally similar"
81)]
82#![allow(
83 clippy::cast_possible_truncation,
84 clippy::cast_possible_wrap,
85 clippy::cast_precision_loss,
86 clippy::cast_sign_loss,
87 clippy::cast_lossless,
88 reason = "wire-format and fixed-point conversions in serialization code require explicit numeric casts"
89)]
90#![allow(
91 clippy::assert_is_empty,
92 reason = "`assert!(x.is_empty())` is clearer than comparing against an empty value"
93)]
94#![cfg_attr(
95 test,
96 allow(
97 clippy::float_cmp,
98 reason = "serialization tests assert exact float encodings and decoded values"
99 )
100)]
101
102#[cfg(feature = "arrow")]
103pub mod arrow;
104
105/// Re-export custom data registration for use by persistence and tests.
106#[cfg(feature = "arrow")]
107pub use arrow::custom::ensure_custom_data_registered;
108/// Re-exports MsgPack codecs for consumers configuring serialization through this crate.
109pub use nautilus_core::serialization::msgpack;
110
111#[cfg(feature = "capnp")]
112pub mod capnp;
113
114#[cfg(feature = "sbe")]
115pub mod sbe;
116
117#[cfg(any(feature = "capnp", feature = "sbe"))]
118mod numeric;
119
120#[cfg(feature = "capnp")]
121macro_rules! include_capnp_module {
122 ($name:ident, $path:expr) => {
123 #[cfg(all(feature = "capnp", not(docsrs)))]
124 #[allow(
125 clippy::all,
126 clippy::missing_errors_doc,
127 clippy::missing_panics_doc,
128 warnings,
129 dead_code,
130 missing_debug_implementations
131 )]
132 pub mod $name {
133 include!(concat!(env!("OUT_DIR"), $path));
134 }
135
136 #[cfg(all(feature = "capnp", docsrs))]
137 #[allow(
138 clippy::all,
139 clippy::missing_errors_doc,
140 clippy::missing_panics_doc,
141 warnings,
142 dead_code,
143 missing_debug_implementations
144 )]
145 pub mod $name {
146 include!(concat!(
147 env!("CARGO_MANIFEST_DIR"),
148 "/generated/capnp",
149 $path
150 ));
151 }
152 };
153}
154
155#[cfg(feature = "capnp")]
156include_capnp_module!(base_capnp, "/common/base_capnp.rs");
157#[cfg(feature = "capnp")]
158include_capnp_module!(identifiers_capnp, "/common/identifiers_capnp.rs");
159#[cfg(feature = "capnp")]
160include_capnp_module!(types_capnp, "/common/types_capnp.rs");
161#[cfg(feature = "capnp")]
162include_capnp_module!(enums_capnp, "/common/enums_capnp.rs");
163#[cfg(feature = "capnp")]
164include_capnp_module!(trading_capnp, "/commands/trading_capnp.rs");
165#[cfg(feature = "capnp")]
166include_capnp_module!(data_capnp, "/commands/data_capnp.rs");
167#[cfg(feature = "capnp")]
168include_capnp_module!(order_capnp, "/events/order_capnp.rs");
169#[cfg(feature = "capnp")]
170include_capnp_module!(position_capnp, "/events/position_capnp.rs");
171#[cfg(feature = "capnp")]
172include_capnp_module!(account_capnp, "/events/account_capnp.rs");
173#[cfg(feature = "capnp")]
174include_capnp_module!(market_capnp, "/data/market_capnp.rs");
175#[cfg(feature = "capnp")]
176include_capnp_module!(instruments_capnp, "/data/instruments_capnp.rs");
177
178#[cfg(feature = "python")]
179pub mod python;