1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 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// -------------------------------------------------------------------------------------------------
1516use std::{
17 collections::hash_map::DefaultHasher,
18 hash::{Hash, Hasher},
19 ops::Deref,
20};
2122use nautilus_core::python::{IntoPyObjectNautilusExt, to_pyvalue_err};
23use pyo3::{prelude::*, pyclass::CompareOp, types::PyCapsule};
2425use super::data_to_pycapsule;
26use crate::{
27 data::{Data, OrderBookDelta, OrderBookDeltas, OrderBookDeltas_API},
28 identifiers::InstrumentId,
29 python::common::PY_MODULE_MODEL,
30};
3132#[pymethods]
33impl OrderBookDeltas {
34#[new]
35fn py_new(instrument_id: InstrumentId, deltas: Vec<OrderBookDelta>) -> PyResult<Self> {
36Self::new_checked(instrument_id, deltas).map_err(to_pyvalue_err)
37 }
3839fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
40match op {
41 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
42 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
43_ => py.NotImplemented(),
44 }
45 }
4647fn __hash__(&self) -> isize {
48let mut h = DefaultHasher::new();
49self.hash(&mut h);
50 h.finish() as isize
51 }
5253fn __repr__(&self) -> String {
54format!("{self:?}")
55 }
5657fn __str__(&self) -> String {
58self.to_string()
59 }
6061#[getter]
62 #[pyo3(name = "instrument_id")]
63fn py_instrument_id(&self) -> InstrumentId {
64self.instrument_id
65 }
6667#[getter]
68 #[pyo3(name = "deltas")]
69fn py_deltas(&self) -> Vec<OrderBookDelta> {
70// `OrderBookDelta` is `Copy`
71self.deltas.clone()
72 }
7374#[getter]
75 #[pyo3(name = "flags")]
76fn py_flags(&self) -> u8 {
77self.flags
78 }
7980#[getter]
81 #[pyo3(name = "sequence")]
82fn py_sequence(&self) -> u64 {
83self.sequence
84 }
8586#[getter]
87 #[pyo3(name = "ts_event")]
88fn py_ts_event(&self) -> u64 {
89self.ts_event.as_u64()
90 }
9192#[getter]
93 #[pyo3(name = "ts_init")]
94fn py_ts_init(&self) -> u64 {
95self.ts_init.as_u64()
96 }
9798#[staticmethod]
99 #[pyo3(name = "fully_qualified_name")]
100fn py_fully_qualified_name() -> String {
101format!("{}:{}", PY_MODULE_MODEL, stringify!(OrderBookDeltas))
102 }
103104#[staticmethod]
105 #[pyo3(name = "from_pycapsule")]
106pub fn py_from_pycapsule(capsule: Bound<'_, PyAny>) -> Self {
107let capsule: &Bound<'_, PyCapsule> = capsule
108 .downcast::<PyCapsule>()
109 .expect("Error on downcast to `&PyCapsule`");
110let data: &OrderBookDeltas_API =
111unsafe { &*(capsule.pointer() as *const OrderBookDeltas_API) };
112 data.deref().clone()
113 }
114115/// Creates a `PyCapsule` containing a raw pointer to a [`Data::Deltas`] object.
116 ///
117 /// This function takes the current object (assumed to be of a type that can be represented as
118 /// `Data::Deltas`), and encapsulates a raw pointer to it within a `PyCapsule`.
119 ///
120 /// # Safety
121 ///
122 /// This function is safe as long as the following conditions are met:
123 /// - The `Data::Deltas` object pointed to by the capsule must remain valid for the lifetime of the capsule.
124 /// - The consumer of the capsule must ensure proper handling to avoid dereferencing a dangling pointer.
125 ///
126 /// # Panics
127 ///
128 /// The function will panic if the `PyCapsule` creation fails, which can occur if the
129 /// [`Data::Deltas`] object cannot be converted into a raw pointer.
130#[pyo3(name = "as_pycapsule")]
131fn py_as_pycapsule(&self, py: Python<'_>) -> PyObject {
132let deltas = OrderBookDeltas_API::new(self.clone());
133 data_to_pycapsule(py, Data::Deltas(deltas))
134 }
135136// TODO: Implement `Serializable` and the other methods can be added
137}