architect_api/orderflow/
cancel.rs

1use crate::OrderId;
2use chrono::{DateTime, Utc};
3use schemars::{JsonSchema, JsonSchema_repr};
4use serde::{Deserialize, Serialize};
5use serde_repr::{Deserialize_repr, Serialize_repr};
6use serde_with::skip_serializing_none;
7use uuid::Uuid;
8
9#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
10pub struct Cancel {
11    #[serde(rename = "xid")]
12    #[schemars(title = "cancel_id")]
13    pub cancel_id: Uuid,
14    #[serde(rename = "id")]
15    #[schemars(title = "order_id")]
16    pub order_id: OrderId,
17    #[serde(rename = "ts")]
18    #[schemars(title = "recv_time")]
19    pub recv_time: i64,
20    #[serde(rename = "tn")]
21    #[schemars(title = "recv_time_ns")]
22    pub recv_time_ns: u32,
23    #[serde(rename = "o")]
24    #[schemars(title = "status")]
25    pub status: CancelStatus,
26    #[serde(rename = "r")]
27    #[schemars(title = "reject_reason")]
28    pub reject_reason: Option<String>,
29}
30
31impl Cancel {
32    pub fn reject(&self, message: Option<String>) -> CancelReject {
33        CancelReject { cancel_id: self.cancel_id, order_id: self.order_id, message }
34    }
35
36    pub fn recv_time(&self) -> Option<DateTime<Utc>> {
37        DateTime::from_timestamp(self.recv_time, self.recv_time_ns)
38    }
39}
40
41#[derive(
42    Debug, Clone, Copy, Serialize_repr, Deserialize_repr, PartialEq, Eq, JsonSchema_repr,
43)]
44#[serde(rename_all = "snake_case")]
45#[cfg_attr(feature = "juniper", derive(juniper::GraphQLEnum))]
46#[repr(u8)]
47pub enum CancelStatus {
48    Pending = 0,
49    Acked = 1,
50    Rejected = 2,
51    Out = 127,
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
55#[cfg_attr(feature = "juniper", derive(juniper::GraphQLObject))]
56pub struct CancelReject {
57    #[serde(rename = "xid")]
58    pub cancel_id: Uuid,
59    #[serde(rename = "id")]
60    pub order_id: OrderId,
61    #[serde(rename = "rm", skip_serializing_if = "Option::is_none")]
62    pub message: Option<String>,
63}
64
65impl CancelReject {
66    pub fn to_error_string(&self) -> String {
67        format!(
68            "cancel {} rejected: {}",
69            self.cancel_id,
70            self.message.as_deref().unwrap_or("--")
71        )
72    }
73}
74
75#[skip_serializing_none]
76#[derive(Debug, Clone, Serialize, Deserialize)]
77pub struct CancelUpdate {
78    pub cancel_id: Uuid,
79    pub order_id: OrderId,
80    pub timestamp: i64,
81    pub timestamp_ns: u32,
82    pub status: Option<CancelStatus>,
83    pub reject_reason: Option<String>,
84}
85
86impl CancelUpdate {
87    pub fn timestamp(&self) -> Option<DateTime<Utc>> {
88        DateTime::from_timestamp(self.timestamp, self.timestamp_ns)
89    }
90}