#![allow(unsafe_code)]
use std::ops::Deref;
use nautilus_core::Params;
use nautilus_model::{
enums::OrderSide,
identifiers::{ClientId, ClientOrderId, InstrumentId},
};
#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CancelOrderCommand {
pub client_order_id: ClientOrderId,
pub client_id: Option<ClientId>,
pub params: Option<Params>,
}
impl CancelOrderCommand {
#[must_use]
pub const fn new(
client_order_id: ClientOrderId,
client_id: Option<ClientId>,
params: Option<Params>,
) -> Self {
Self {
client_order_id,
client_id,
params,
}
}
}
#[repr(C)]
#[derive(Debug, Clone)]
pub struct CancelOrderHandle(Box<CancelOrderCommand>);
impl CancelOrderHandle {
#[must_use]
pub fn new(command: CancelOrderCommand) -> Self {
Self(Box::new(command))
}
#[must_use]
pub fn command(&self) -> &CancelOrderCommand {
&self.0
}
#[must_use]
pub fn into_inner(self) -> CancelOrderCommand {
*self.0
}
}
impl Deref for CancelOrderHandle {
type Target = CancelOrderCommand;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CancelOrdersCommand {
pub client_order_ids: Vec<ClientOrderId>,
pub client_id: Option<ClientId>,
pub params: Option<Params>,
}
impl CancelOrdersCommand {
#[must_use]
pub const fn new(
client_order_ids: Vec<ClientOrderId>,
client_id: Option<ClientId>,
params: Option<Params>,
) -> Self {
Self {
client_order_ids,
client_id,
params,
}
}
}
#[repr(C)]
#[derive(Debug, Clone)]
pub struct CancelOrdersHandle(Box<CancelOrdersCommand>);
impl CancelOrdersHandle {
#[must_use]
pub fn new(command: CancelOrdersCommand) -> Self {
Self(Box::new(command))
}
#[must_use]
pub fn command(&self) -> &CancelOrdersCommand {
&self.0
}
#[must_use]
pub fn into_inner(self) -> CancelOrdersCommand {
*self.0
}
}
impl Deref for CancelOrdersHandle {
type Target = CancelOrdersCommand;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CancelAllOrdersCommand {
pub instrument_id: InstrumentId,
pub order_side: Option<OrderSide>,
pub client_id: Option<ClientId>,
pub params: Option<Params>,
}
impl CancelAllOrdersCommand {
#[must_use]
pub const fn new(
instrument_id: InstrumentId,
order_side: Option<OrderSide>,
client_id: Option<ClientId>,
params: Option<Params>,
) -> Self {
Self {
instrument_id,
order_side,
client_id,
params,
}
}
}
#[repr(C)]
#[derive(Debug, Clone)]
pub struct CancelAllOrdersHandle(Box<CancelAllOrdersCommand>);
impl CancelAllOrdersHandle {
#[must_use]
pub fn new(command: CancelAllOrdersCommand) -> Self {
Self(Box::new(command))
}
#[must_use]
pub fn command(&self) -> &CancelAllOrdersCommand {
&self.0
}
#[must_use]
pub fn into_inner(self) -> CancelAllOrdersCommand {
*self.0
}
}
impl Deref for CancelAllOrdersHandle {
type Target = CancelAllOrdersCommand;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[cfg(test)]
mod tests {
use nautilus_model::{
enums::OrderSide,
identifiers::{ClientOrderId, InstrumentId},
};
use rstest::rstest;
use super::*;
#[rstest]
fn cancel_order_handle_round_trips_command() {
let cmd = CancelOrderCommand::new(ClientOrderId::from("O-1"), None, None);
let handle = CancelOrderHandle::new(cmd.clone());
assert_eq!(handle.command(), &cmd);
assert_eq!(&*handle, &cmd);
assert_eq!(handle.into_inner(), cmd);
}
#[rstest]
fn cancel_orders_handle_round_trips_command() {
let cmd = CancelOrdersCommand::new(
vec![ClientOrderId::from("O-1"), ClientOrderId::from("O-2")],
None,
None,
);
let handle = CancelOrdersHandle::new(cmd.clone());
assert_eq!(handle.command(), &cmd);
assert_eq!(&*handle, &cmd);
assert_eq!(handle.into_inner(), cmd);
}
#[rstest]
fn cancel_all_orders_handle_round_trips_command() {
let cmd = CancelAllOrdersCommand::new(
InstrumentId::from("ETH-USDT.BINANCE"),
Some(OrderSide::Buy),
None,
None,
);
let handle = CancelAllOrdersHandle::new(cmd.clone());
assert_eq!(handle.command(), &cmd);
assert_eq!(&*handle, &cmd);
assert_eq!(handle.into_inner(), cmd);
}
}