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, 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,
26/// CatalogPathPrefix, From/TryFrom for Data. Call `nautilus_serialization::ensure_custom_data_registered::<T>()`
27/// and (for Python) `nautilus_model::data::register_rust_extractor::<T>()` once per type.
28/// Requires fields to include `ts_event` and `ts_init` (e.g. `nautilus_core::UnixNanos`).
29/// Supported field types include InstrumentId, AccountId, Currency, BarType, Params, UnixNanos,
30/// f64, f32, bool, String, u64, i64, u32, i32, `Vec<f64>`, and `Vec<u8>`.
31///
32/// Use `#[custom_data(pyo3)]` or `#[custom_data(python)]` to also generate Python bindings:
33/// `#[cfg_attr(feature = "python", pyo3::pyclass)]` on the struct and a `#[pymethods]` impl with
34/// `#[new]` constructor and `#[getter]` per field. When pyo3 is set, the Rust constructor is
35/// named `new`; Python `__init__` forwards to it.
36/// Use `#[custom_data(pyo3, no_display)]` to skip generating `repr()` and `Display` so you can implement them manually.
37#[proc_macro_attribute]
38pub fn custom_data(attr: TokenStream, item: TokenStream) -> TokenStream {
39    custom::expand_custom_data(attr.into(), item.into()).into()
40}