Skip to main content

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//! - `python`: Enables Python bindings from [PyO3](https://pyo3.rs).
47//! - `high-precision`: Enables [high-precision mode](https://nautilustrader.io/docs/nightly/getting_started/installation#precision-mode) to use 128-bit value types.
48//! - `extension-module`: Builds the crate as a Python extension module.
49//! - `capnp`: Enables [Cap'n Proto](https://capnproto.org/) serialization support.
50//! - `sbe`: Enables generic SBE (Simple Binary Encoding) decode utilities.
51
52#![warn(rustc::all)]
53#![deny(unsafe_code)]
54#![deny(unsafe_op_in_unsafe_fn)]
55#![deny(nonstandard_style)]
56#![deny(missing_debug_implementations)]
57#![deny(clippy::missing_errors_doc)]
58#![deny(clippy::missing_panics_doc)]
59#![deny(rustdoc::broken_intra_doc_links)]
60
61#[cfg(feature = "arrow")]
62pub mod arrow;
63
64/// Re-export custom data registration for use by persistence and tests.
65#[cfg(feature = "arrow")]
66pub use arrow::custom::ensure_custom_data_registered;
67/// Re-export MsgPack serialization helpers for consumers expecting to configure codecs via this crate.
68pub use nautilus_core::serialization::msgpack;
69
70#[cfg(feature = "capnp")]
71pub mod capnp;
72
73#[cfg(feature = "sbe")]
74pub mod sbe;
75
76#[cfg(feature = "capnp")]
77macro_rules! include_capnp_module {
78    ($name:ident, $path:expr) => {
79        #[cfg(all(feature = "capnp", not(docsrs)))]
80        #[allow(
81            clippy::all,
82            clippy::missing_errors_doc,
83            clippy::missing_panics_doc,
84            warnings,
85            dead_code,
86            missing_debug_implementations
87        )]
88        pub mod $name {
89            include!(concat!(env!("OUT_DIR"), $path));
90        }
91
92        #[cfg(all(feature = "capnp", docsrs))]
93        #[allow(
94            clippy::all,
95            clippy::missing_errors_doc,
96            clippy::missing_panics_doc,
97            warnings,
98            dead_code,
99            missing_debug_implementations
100        )]
101        pub mod $name {
102            include!(concat!(
103                env!("CARGO_MANIFEST_DIR"),
104                "/generated/capnp",
105                $path
106            ));
107        }
108    };
109}
110
111#[cfg(feature = "capnp")]
112include_capnp_module!(base_capnp, "/common/base_capnp.rs");
113#[cfg(feature = "capnp")]
114include_capnp_module!(identifiers_capnp, "/common/identifiers_capnp.rs");
115#[cfg(feature = "capnp")]
116include_capnp_module!(types_capnp, "/common/types_capnp.rs");
117#[cfg(feature = "capnp")]
118include_capnp_module!(enums_capnp, "/common/enums_capnp.rs");
119#[cfg(feature = "capnp")]
120include_capnp_module!(trading_capnp, "/commands/trading_capnp.rs");
121#[cfg(feature = "capnp")]
122include_capnp_module!(data_capnp, "/commands/data_capnp.rs");
123#[cfg(feature = "capnp")]
124include_capnp_module!(order_capnp, "/events/order_capnp.rs");
125#[cfg(feature = "capnp")]
126include_capnp_module!(position_capnp, "/events/position_capnp.rs");
127#[cfg(feature = "capnp")]
128include_capnp_module!(account_capnp, "/events/account_capnp.rs");
129#[cfg(feature = "capnp")]
130include_capnp_module!(market_capnp, "/data/market_capnp.rs");
131
132#[cfg(feature = "python")]
133pub mod python;