Skip to main content

nautilus_model/python/account/
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
16pub mod betting;
17pub mod cash;
18pub mod margin;
19pub mod margin_model;
20pub mod transformer;
21pub mod wallet;
22
23use nautilus_core::python::to_pyvalue_err;
24use pyo3::{Py, PyAny, PyResult, Python, conversion::IntoPyObjectExt};
25
26use crate::{
27    accounts::{AccountAny, BettingAccount, CashAccount, MarginAccount, WalletAccount},
28    enums::AccountType,
29};
30
31/// Converts a Python account object into a Rust `AccountAny` enum.
32///
33/// # Errors
34///
35/// Returns a `PyErr` if:
36/// - retrieving the `account_type` attribute fails.
37/// - extracting the object into the account type's concrete class fails.
38/// - the `account_type` is unsupported.
39#[expect(clippy::needless_pass_by_value)]
40pub fn pyobject_to_account_any(py: Python, account: Py<PyAny>) -> PyResult<AccountAny> {
41    let account_type = account
42        .getattr(py, "account_type")?
43        .extract::<AccountType>(py)?;
44    if account_type == AccountType::Margin {
45        let margin = account.extract::<MarginAccount>(py)?;
46        Ok(AccountAny::Margin(margin))
47    } else if account_type == AccountType::Cash {
48        let cash = account.extract::<CashAccount>(py)?;
49        Ok(AccountAny::Cash(cash))
50    } else if account_type == AccountType::Betting {
51        let betting = account.extract::<BettingAccount>(py)?;
52        Ok(AccountAny::Betting(betting))
53    } else if account_type == AccountType::Wallet {
54        let wallet = account.extract::<WalletAccount>(py)?;
55        Ok(AccountAny::Wallet(wallet))
56    } else {
57        Err(to_pyvalue_err("Unsupported account type"))
58    }
59}
60
61/// Converts a Rust `AccountAny` into a Python account object.
62///
63/// # Errors
64///
65/// Returns a `PyErr` if converting the underlying account into a Python object fails.
66pub fn account_any_to_pyobject(py: Python, account: AccountAny) -> PyResult<Py<PyAny>> {
67    match account {
68        AccountAny::Margin(account) => account.into_py_any(py),
69        AccountAny::Cash(account) => account.into_py_any(py),
70        AccountAny::Betting(account) => account.into_py_any(py),
71        AccountAny::Wallet(account) => account.into_py_any(py),
72    }
73}