Skip to main content

nautilus_model/ffi/data/
order.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
16use std::{
17    collections::hash_map::DefaultHasher,
18    ffi::c_char,
19    hash::{Hash, Hasher},
20};
21
22use nautilus_core::ffi::string::str_to_cstr;
23
24use crate::{
25    data::BookOrder,
26    ffi::enums::OrderSideOptional,
27    types::{Price, Quantity},
28};
29
30/// The stable C representation of a [`BookOrder`].
31#[repr(C)]
32#[derive(Clone, Copy, Debug)]
33pub struct BookOrderFfi {
34    /// The order side, including the legacy zero value.
35    pub side: OrderSideOptional,
36    /// The order price.
37    pub price: Price,
38    /// The order size.
39    pub size: Quantity,
40    /// The order ID.
41    pub order_id: u64,
42}
43
44impl From<BookOrderFfi> for BookOrder {
45    fn from(value: BookOrderFfi) -> Self {
46        Self {
47            side: value.side.as_option(),
48            price: value.price,
49            size: value.size,
50            order_id: value.order_id,
51        }
52    }
53}
54
55impl From<BookOrder> for BookOrderFfi {
56    fn from(value: BookOrder) -> Self {
57        Self {
58            side: value.side.into(),
59            price: value.price,
60            size: value.size,
61            order_id: value.order_id,
62        }
63    }
64}
65
66#[unsafe(no_mangle)]
67#[cfg_attr(feature = "high-precision", allow(improper_ctypes_definitions))]
68pub extern "C" fn book_order_new(
69    order_side: OrderSideOptional,
70    price: Price,
71    size: Quantity,
72    order_id: u64,
73) -> BookOrderFfi {
74    BookOrder::new(order_side.as_option(), price, size, order_id).into()
75}
76
77#[unsafe(no_mangle)]
78pub extern "C" fn book_order_eq(lhs: &BookOrderFfi, rhs: &BookOrderFfi) -> u8 {
79    u8::from(BookOrder::from(*lhs) == BookOrder::from(*rhs))
80}
81
82#[unsafe(no_mangle)]
83pub extern "C" fn book_order_hash(order: &BookOrderFfi) -> u64 {
84    let mut hasher = DefaultHasher::new();
85    BookOrder::from(*order).hash(&mut hasher);
86    hasher.finish()
87}
88
89#[unsafe(no_mangle)]
90pub extern "C" fn book_order_exposure(order: &BookOrderFfi) -> f64 {
91    BookOrder::from(*order).exposure()
92}
93
94#[unsafe(no_mangle)]
95pub extern "C" fn book_order_signed_size(order: &BookOrderFfi) -> f64 {
96    BookOrder::from(*order).signed_size()
97}
98
99/// Returns a [`BookOrder`] display string as a C string pointer.
100#[unsafe(no_mangle)]
101pub extern "C" fn book_order_display_to_cstr(order: &BookOrderFfi) -> *const c_char {
102    str_to_cstr(&BookOrder::from(*order).to_string())
103}
104
105/// Returns a [`BookOrder`] debug string as a C string pointer.
106#[unsafe(no_mangle)]
107pub extern "C" fn book_order_debug_to_cstr(order: &BookOrderFfi) -> *const c_char {
108    str_to_cstr(&format!("{:?}", BookOrder::from(*order)))
109}