Skip to main content

clob_sync/
execution.rs

1use crate::order_book_side::{FullFill, PartialFill};
2use nonempty::NonEmpty;
3
4use crate::{OrderId, Quantity, order_book_side::Match};
5
6/// Represents the result of a single order execution.
7///
8/// # Example
9///
10/// ```
11/// use clob_sync::prelude::{Execution, OrderId, Quantity};
12///
13/// let full_exec = Execution::FullExecution(OrderId::default());
14/// let partial_exec = Execution::PartialExecution(OrderId::default(), Quantity::from(1u64));
15/// ```
16#[derive(Debug)]
17pub enum Execution {
18    /// Order was fully executed
19    FullExecution(OrderId),
20    /// Order was partially executed with remaining quantity
21    PartialExecution(OrderId, Quantity),
22    /// Order was fully cancelled
23    FullCancel(OrderId),
24    /// Order was partially cancelled with remaining quantity
25    PartialCancel(OrderId, Quantity),
26}
27impl From<&Match> for Vec<Execution> {
28    fn from(match_: &Match) -> Self {
29        use Match::*;
30        match match_ {
31            FullPartial { existing, incoming } => {
32                vec![existing.into(), incoming.into()]
33            }
34            PartialFull { existing, incoming } => {
35                vec![existing.into(), incoming.into()]
36            }
37            FullFull { existing, incoming } => {
38                vec![existing.into(), incoming.into()]
39            }
40        }
41    }
42}
43
44impl From<&FullFill> for Execution {
45    fn from(value: &FullFill) -> Self {
46        Execution::FullExecution(value.0)
47    }
48}
49
50impl From<&PartialFill> for Execution {
51    fn from(value: &PartialFill) -> Self {
52        Execution::PartialExecution(value.0, value.leaves())
53    }
54}
55
56/// A list of executions.
57#[derive(Debug)]
58pub struct ExecutionList(pub Vec<Execution>);
59impl ExecutionList {
60    /// Returns true if there are no executions.
61    pub fn is_empty(&self) -> bool {
62        self.0.is_empty()
63    }
64}
65
66/// The result of executing an order against the order book.
67///
68/// # Example
69///
70/// ```
71/// use clob_sync::prelude::{Executions, Execution, OrderId, Quantity};
72/// use nonempty::NonEmpty;
73///
74/// let no_exec = Executions::AllocatedNoExecutions;
75/// let executed = Executions::Executed(NonEmpty::new(
76///     Execution::FullExecution(OrderId::default())
77/// ));
78/// ```
79#[derive(Debug)]
80pub enum Executions {
81    /// Order was added to the book without any executions
82    AllocatedNoExecutions,
83    /// Order was fully or partially executed
84    Executed(NonEmpty<Execution>),
85}