Skip to main content

nautilus_persistence_macros/
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//! Procedural macros for Nautilus. Provides `#[custom_data]` for defining custom data types
17//! with generated boilerplate (constructor, HasTsInit, CustomDataTrait, optional Arrow, derives).
18
19mod custom;
20
21use proc_macro::TokenStream;
22
23/// Expands a struct into a custom data type with generated impls: `#[derive(Debug, Clone,
24/// Serialize, Deserialize, PartialEq)]`, constructor, HasTsInit, CustomDataTrait,
25/// ArrowSchemaProvider, EncodeToRecordBatch, DecodeDataFromRecordBatch unless `no_arrow` is set,
26/// CatalogPathPrefix, From/TryFrom for Data. Call `nautilus_serialization::ensure_custom_data_registered::<T>()`
27/// once per Arrow-enabled type, or `nautilus_model::data::ensure_custom_data_json_registered::<T>()`
28/// for `no_arrow` types. For Python, also call `nautilus_model::data::register_rust_extractor::<T>()`
29/// once per type.
30/// Requires fields to include `ts_event` and `ts_init` (e.g. `nautilus_core::UnixNanos`).
31/// Supported field types include InstrumentId, AccountId, Currency, BarType, Params, UnixNanos,
32/// f64, f32, bool, String, u64, i64, u32, i32, `Vec<f64>`, and `Vec<u8>`.
33/// Use `#[custom_data_field(serde)]` on a field to store any Serde serializable field as a
34/// Serde JSON-backed Arrow `Utf8` column. Python access uses typed dict conversion when both
35/// `K` and `V` of `HashMap<K, V>` or `IndexMap<K, V>` are in the typed-element whitelist:
36/// `InstrumentId`, `AccountId`, `Currency`, `BarType`, `Price`, `Quantity`, `Money`, `String`,
37/// `f64`, `f32`, `bool`, `u64`, `i64`, `u32`, `i32` (see `is_typed_json_map_segment` in
38/// `custom.rs`). All other Serde-backed fields use the generic JSON bridge and accept/return
39/// JSON-compatible Python values.
40///
41/// Use `#[custom_data(pyo3)]` or `#[custom_data(python)]` to also generate Python bindings:
42/// `#[cfg_attr(feature = "python", pyo3::pyclass)]` on the struct and a `#[pymethods]` impl with
43/// `#[new]` constructor and `#[getter]` per field. When pyo3 is set, the Rust constructor is
44/// named `new`; Python `__init__` forwards to it.
45/// Use `#[custom_data(pyo3, no_display)]` to skip generating `repr()` and `Display` so you can implement them manually.
46/// Use `#[custom_data(pyo3, no_arrow)]` for live-only custom data that does not need Arrow or catalog persistence.
47/// Use `stub_module = "nautilus_trader.<module>"` with `pyo3` to emit pyo3-stub-gen metadata.
48#[proc_macro_attribute]
49pub fn custom_data(attr: TokenStream, item: TokenStream) -> TokenStream {
50    custom::expand_custom_data(attr.into(), item.into()).into()
51}