Skip to main content

nautilus_derive/python/
mod.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//! Python bindings from `pyo3`.
17
18pub mod config;
19pub mod enums;
20pub mod factories;
21
22use nautilus_common::factories::{ClientConfig, DataClientFactory, ExecutionClientFactory};
23use nautilus_core::python::{to_pyruntime_err, to_pyvalue_err};
24use nautilus_system::get_global_pyo3_registry;
25use pyo3::prelude::*;
26
27use crate::{
28    common::{consts::DERIVE, enums::DeriveEnvironment},
29    config::{DeriveDataClientConfig, DeriveExecClientConfig},
30    factories::{DeriveDataClientFactory, DeriveExecFactoryConfig, DeriveExecutionClientFactory},
31};
32
33#[expect(clippy::needless_pass_by_value)]
34fn extract_derive_data_factory(
35    py: Python<'_>,
36    factory: Py<PyAny>,
37) -> PyResult<Box<dyn DataClientFactory>> {
38    match factory.extract::<DeriveDataClientFactory>(py) {
39        Ok(f) => Ok(Box::new(f)),
40        Err(e) => Err(to_pyvalue_err(format!(
41            "Failed to extract DeriveDataClientFactory: {e}"
42        ))),
43    }
44}
45
46#[expect(clippy::needless_pass_by_value)]
47fn extract_derive_exec_factory(
48    py: Python<'_>,
49    factory: Py<PyAny>,
50) -> PyResult<Box<dyn ExecutionClientFactory>> {
51    match factory.extract::<DeriveExecutionClientFactory>(py) {
52        Ok(f) => Ok(Box::new(f)),
53        Err(e) => Err(to_pyvalue_err(format!(
54            "Failed to extract DeriveExecutionClientFactory: {e}"
55        ))),
56    }
57}
58
59#[expect(clippy::needless_pass_by_value)]
60fn extract_derive_data_config(
61    py: Python<'_>,
62    config: Py<PyAny>,
63) -> PyResult<Box<dyn ClientConfig>> {
64    match config.extract::<DeriveDataClientConfig>(py) {
65        Ok(c) => Ok(Box::new(c)),
66        Err(e) => Err(to_pyvalue_err(format!(
67            "Failed to extract DeriveDataClientConfig: {e}"
68        ))),
69    }
70}
71
72#[expect(clippy::needless_pass_by_value)]
73fn extract_derive_exec_config(
74    py: Python<'_>,
75    config: Py<PyAny>,
76) -> PyResult<Box<dyn ClientConfig>> {
77    match config.extract::<DeriveExecFactoryConfig>(py) {
78        Ok(c) => Ok(Box::new(c)),
79        Err(e) => Err(to_pyvalue_err(format!(
80            "Failed to extract DeriveExecFactoryConfig: {e}"
81        ))),
82    }
83}
84
85/// Loaded as `nautilus_pyo3.derive`.
86///
87/// # Errors
88///
89/// Returns an error if any bindings fail to register with the Python module.
90#[pymodule]
91pub fn derive(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
92    m.add(stringify!(DERIVE), DERIVE)?;
93    m.add_class::<DeriveEnvironment>()?;
94    m.add_class::<DeriveDataClientConfig>()?;
95    m.add_class::<DeriveExecClientConfig>()?;
96    m.add_class::<DeriveDataClientFactory>()?;
97    m.add_class::<DeriveExecFactoryConfig>()?;
98    m.add_class::<DeriveExecutionClientFactory>()?;
99
100    let registry = get_global_pyo3_registry();
101
102    if let Err(e) =
103        registry.register_factory_extractor(DERIVE.to_string(), extract_derive_data_factory)
104    {
105        return Err(to_pyruntime_err(format!(
106            "Failed to register Derive data factory extractor: {e}"
107        )));
108    }
109
110    if let Err(e) =
111        registry.register_exec_factory_extractor(DERIVE.to_string(), extract_derive_exec_factory)
112    {
113        return Err(to_pyruntime_err(format!(
114            "Failed to register Derive exec factory extractor: {e}"
115        )));
116    }
117
118    if let Err(e) = registry.register_config_extractor(
119        "DeriveDataClientConfig".to_string(),
120        extract_derive_data_config,
121    ) {
122        return Err(to_pyruntime_err(format!(
123            "Failed to register Derive data config extractor: {e}"
124        )));
125    }
126
127    if let Err(e) = registry.register_config_extractor(
128        "DeriveExecFactoryConfig".to_string(),
129        extract_derive_exec_config,
130    ) {
131        return Err(to_pyruntime_err(format!(
132            "Failed to register Derive exec config extractor: {e}"
133        )));
134    }
135
136    Ok(())
137}