Skip to main content

nautilus_common/
custom.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//! A user custom data type.
17
18use bytes::Bytes;
19use nautilus_core::UnixNanos;
20use nautilus_model::data::DataType;
21use serde::{Deserialize, Serialize};
22
23/// Represents a custom data.
24#[repr(C)]
25#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
26#[cfg_attr(
27    feature = "python",
28    pyo3::pyclass(module = "nautilus_trader.common", from_py_object)
29)]
30#[cfg_attr(
31    feature = "python",
32    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.common")
33)]
34pub struct CustomData {
35    pub data_type: DataType,
36    pub value: Bytes,
37    pub ts_event: UnixNanos,
38    pub ts_init: UnixNanos,
39}
40
41impl CustomData {
42    /// Creates a new [`CustomData`] instance.
43    pub const fn new(
44        data_type: DataType,
45        value: Bytes,
46        ts_event: UnixNanos,
47        ts_init: UnixNanos,
48    ) -> Self {
49        Self {
50            data_type,
51            value,
52            ts_event,
53            ts_init,
54        }
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use nautilus_model::data::DataType;
61    use rstest::rstest;
62
63    use super::*;
64
65    fn custom_data() -> CustomData {
66        CustomData::new(
67            DataType::new("MyData", None, None),
68            Bytes::from_static(b"payload"),
69            UnixNanos::from(3),
70            UnixNanos::from(5),
71        )
72    }
73
74    #[rstest]
75    fn test_new_assigns_every_field() {
76        let data = custom_data();
77
78        assert_eq!(data.data_type, DataType::new("MyData", None, None));
79        assert_eq!(data.value, Bytes::from_static(b"payload"));
80        assert_eq!(data.ts_event, UnixNanos::from(3));
81        assert_eq!(data.ts_init, UnixNanos::from(5));
82    }
83
84    #[rstest]
85    #[case::data_type(CustomData { data_type: DataType::new("OtherData", None, None), ..custom_data() })]
86    #[case::value(CustomData { value: Bytes::from_static(b"other"), ..custom_data() })]
87    #[case::ts_event(CustomData { ts_event: UnixNanos::from(4), ..custom_data() })]
88    #[case::ts_init(CustomData { ts_init: UnixNanos::from(6), ..custom_data() })]
89    fn test_equality_discriminates_each_field(#[case] other: CustomData) {
90        let data = custom_data();
91
92        assert_eq!(data, custom_data());
93        assert_ne!(data, other);
94    }
95
96    #[rstest]
97    fn test_serde_round_trips() {
98        let data = custom_data();
99
100        let json = serde_json::to_string(&data).unwrap();
101
102        assert_eq!(serde_json::from_str::<CustomData>(&json).unwrap(), data);
103    }
104}