nautilus_model/python/account/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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 cash;
17pub mod margin;
18pub mod transformer;
19
20use nautilus_core::python::to_pyvalue_err;
21use pyo3::{PyObject, PyResult, Python, conversion::IntoPyObjectExt};
22
23use crate::{
24    accounts::{AccountAny, CashAccount, MarginAccount},
25    enums::AccountType,
26};
27
28/// Converts a Python account object into a Rust `AccountAny` enum.
29///
30/// # Errors
31///
32/// Returns a `PyErr` if:
33/// - retrieving the `account_type` attribute fails.
34/// - extracting the object into `CashAccount` or `MarginAccount` fails.
35/// - the `account_type` is unsupported.
36pub fn pyobject_to_account_any(py: Python, account: PyObject) -> PyResult<AccountAny> {
37    let account_type = account
38        .getattr(py, "account_type")?
39        .extract::<AccountType>(py)?;
40    if account_type == AccountType::Cash {
41        let cash = account.extract::<CashAccount>(py)?;
42        Ok(AccountAny::Cash(cash))
43    } else if account_type == AccountType::Margin {
44        let margin = account.extract::<MarginAccount>(py)?;
45        Ok(AccountAny::Margin(margin))
46    } else {
47        Err(to_pyvalue_err("Unsupported account type"))
48    }
49}
50
51/// Converts a Rust `AccountAny` into a Python account object.
52///
53/// # Errors
54///
55/// Returns a `PyErr` if converting the underlying account into a Python object fails.
56pub fn account_any_to_pyobject(py: Python, account: AccountAny) -> PyResult<PyObject> {
57    match account {
58        AccountAny::Cash(account) => account.into_py_any(py),
59        AccountAny::Margin(account) => account.into_py_any(py),
60    }
61}