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//! - `display`: Enables display-friendly Arrow encoders for market data (requires `arrow`).
47//! - `python`: Enables Python bindings from [PyO3](https://pyo3.rs).
48//! - `high-precision`: Enables [high-precision mode](https://nautilustrader.io/docs/nightly/getting_started/installation#precision-mode) to use 128-bit value types.
49//! - `extension-module`: Builds the crate as a Python extension module.
50//! - `capnp`: Enables [Cap'n Proto](https://capnproto.org/) serialization support.
51//! - `sbe`: Enables generic SBE (Simple Binary Encoding) decode utilities.
52//!
53//! **Warning:** SBE and Cap'n Proto schemas are not yet stable and may break between releases.
54
55#![warn(rustc::all)]
56#![warn(clippy::pedantic)]
57#![deny(unsafe_code)]
58#![deny(unsafe_op_in_unsafe_fn)]
59#![deny(nonstandard_style)]
60#![deny(missing_debug_implementations)]
61#![deny(clippy::missing_errors_doc)]
62#![deny(clippy::missing_panics_doc)]
63#![deny(rustdoc::broken_intra_doc_links)]
64#![allow(
65 clippy::doc_markdown,
66 reason = "serialization docs are heavy on Arrow and domain-specific type names where blanket backticks add noise"
67)]
68#![allow(
69 clippy::too_many_lines,
70 reason = "wide encode and decode functions mirror protocol schemas and Arrow record layouts"
71)]
72#![allow(
73 clippy::implicit_hasher,
74 reason = "serialization metadata uses a standardized HashMap<String, String> shape across traits and helpers"
75)]
76#![allow(
77 clippy::similar_names,
78 reason = "domain terms such as trade/trader and side/size are intentionally similar"
79)]
80#![allow(
81 clippy::cast_possible_truncation,
82 clippy::cast_possible_wrap,
83 clippy::cast_precision_loss,
84 clippy::cast_sign_loss,
85 clippy::cast_lossless,
86 reason = "wire-format and fixed-point conversions in serialization code require explicit numeric casts"
87)]
88#![allow(
89 clippy::assert_is_empty,
90 reason = "`assert!(x.is_empty())` is clearer than comparing against an empty value"
91)]
92#![cfg_attr(
93 test,
94 allow(
95 clippy::float_cmp,
96 reason = "serialization tests assert exact float encodings and decoded values"
97 )
98)]
99
100#[cfg(feature = "arrow")]
101pub mod arrow;
102
103/// Re-export custom data registration for use by persistence and tests.
104#[cfg(feature = "arrow")]
105pub use arrow::custom::ensure_custom_data_registered;
106/// Re-export MsgPack serialization helpers for consumers expecting to configure codecs via this crate.
107pub use nautilus_core::serialization::msgpack;
108
109#[cfg(feature = "capnp")]
110pub mod capnp;
111
112#[cfg(feature = "sbe")]
113pub mod sbe;
114
115#[cfg(any(feature = "capnp", feature = "sbe"))]
116mod numeric;
117
118#[cfg(feature = "capnp")]
119macro_rules! include_capnp_module {
120 ($name:ident, $path:expr) => {
121 #[cfg(all(feature = "capnp", not(docsrs)))]
122 #[allow(
123 clippy::all,
124 clippy::missing_errors_doc,
125 clippy::missing_panics_doc,
126 warnings,
127 dead_code,
128 missing_debug_implementations
129 )]
130 pub mod $name {
131 include!(concat!(env!("OUT_DIR"), $path));
132 }
133
134 #[cfg(all(feature = "capnp", docsrs))]
135 #[allow(
136 clippy::all,
137 clippy::missing_errors_doc,
138 clippy::missing_panics_doc,
139 warnings,
140 dead_code,
141 missing_debug_implementations
142 )]
143 pub mod $name {
144 include!(concat!(
145 env!("CARGO_MANIFEST_DIR"),
146 "/generated/capnp",
147 $path
148 ));
149 }
150 };
151}
152
153#[cfg(feature = "capnp")]
154include_capnp_module!(base_capnp, "/common/base_capnp.rs");
155#[cfg(feature = "capnp")]
156include_capnp_module!(identifiers_capnp, "/common/identifiers_capnp.rs");
157#[cfg(feature = "capnp")]
158include_capnp_module!(types_capnp, "/common/types_capnp.rs");
159#[cfg(feature = "capnp")]
160include_capnp_module!(enums_capnp, "/common/enums_capnp.rs");
161#[cfg(feature = "capnp")]
162include_capnp_module!(trading_capnp, "/commands/trading_capnp.rs");
163#[cfg(feature = "capnp")]
164include_capnp_module!(data_capnp, "/commands/data_capnp.rs");
165#[cfg(feature = "capnp")]
166include_capnp_module!(order_capnp, "/events/order_capnp.rs");
167#[cfg(feature = "capnp")]
168include_capnp_module!(position_capnp, "/events/position_capnp.rs");
169#[cfg(feature = "capnp")]
170include_capnp_module!(account_capnp, "/events/account_capnp.rs");
171#[cfg(feature = "capnp")]
172include_capnp_module!(market_capnp, "/data/market_capnp.rs");
173
174#[cfg(feature = "python")]
175pub mod python;