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