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#![cfg_attr(
89 test,
90 allow(
91 clippy::float_cmp,
92 reason = "serialization tests assert exact float encodings and decoded values"
93 )
94)]
95
96#[cfg(feature = "arrow")]
97pub mod arrow;
98
99/// Re-export custom data registration for use by persistence and tests.
100#[cfg(feature = "arrow")]
101pub use arrow::custom::ensure_custom_data_registered;
102/// Re-export MsgPack serialization helpers for consumers expecting to configure codecs via this crate.
103pub use nautilus_core::serialization::msgpack;
104
105#[cfg(feature = "capnp")]
106pub mod capnp;
107
108#[cfg(feature = "sbe")]
109pub mod sbe;
110
111#[cfg(feature = "capnp")]
112macro_rules! include_capnp_module {
113 ($name:ident, $path:expr) => {
114 #[cfg(all(feature = "capnp", not(docsrs)))]
115 #[allow(
116 clippy::all,
117 clippy::missing_errors_doc,
118 clippy::missing_panics_doc,
119 warnings,
120 dead_code,
121 missing_debug_implementations
122 )]
123 pub mod $name {
124 include!(concat!(env!("OUT_DIR"), $path));
125 }
126
127 #[cfg(all(feature = "capnp", docsrs))]
128 #[allow(
129 clippy::all,
130 clippy::missing_errors_doc,
131 clippy::missing_panics_doc,
132 warnings,
133 dead_code,
134 missing_debug_implementations
135 )]
136 pub mod $name {
137 include!(concat!(
138 env!("CARGO_MANIFEST_DIR"),
139 "/generated/capnp",
140 $path
141 ));
142 }
143 };
144}
145
146#[cfg(feature = "capnp")]
147include_capnp_module!(base_capnp, "/common/base_capnp.rs");
148#[cfg(feature = "capnp")]
149include_capnp_module!(identifiers_capnp, "/common/identifiers_capnp.rs");
150#[cfg(feature = "capnp")]
151include_capnp_module!(types_capnp, "/common/types_capnp.rs");
152#[cfg(feature = "capnp")]
153include_capnp_module!(enums_capnp, "/common/enums_capnp.rs");
154#[cfg(feature = "capnp")]
155include_capnp_module!(trading_capnp, "/commands/trading_capnp.rs");
156#[cfg(feature = "capnp")]
157include_capnp_module!(data_capnp, "/commands/data_capnp.rs");
158#[cfg(feature = "capnp")]
159include_capnp_module!(order_capnp, "/events/order_capnp.rs");
160#[cfg(feature = "capnp")]
161include_capnp_module!(position_capnp, "/events/position_capnp.rs");
162#[cfg(feature = "capnp")]
163include_capnp_module!(account_capnp, "/events/account_capnp.rs");
164#[cfg(feature = "capnp")]
165include_capnp_module!(market_capnp, "/data/market_capnp.rs");
166
167#[cfg(feature = "python")]
168pub mod python;