rustrade_execution/order/
id.rs1use derive_more::{Display, From};
2use rand::prelude::IndexedRandom;
3use serde::{Deserialize, Serialize};
4use smol_str::SmolStr;
5
6#[derive(
7 Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Display, From,
8)]
9pub struct ClientOrderId<T = SmolStr>(pub T);
10
11impl ClientOrderId<SmolStr> {
12 pub fn new<S: Into<SmolStr>>(id: S) -> Self {
16 Self(id.into())
17 }
18
19 #[cfg(feature = "hyperliquid")]
28 pub fn uuid() -> Self {
29 Self(SmolStr::new(uuid::Uuid::new_v4().to_string()))
30 }
31
32 pub fn random() -> Self {
34 const LEN_URL_SAFE_SYMBOLS: usize = 64;
35 const URL_SAFE_SYMBOLS: [char; LEN_URL_SAFE_SYMBOLS] = [
36 '_', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
37 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
38 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
39 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
40 ];
41 const LEN_NON_ALLOCATING_CID: usize = 23;
43
44 let mut thread_rng = rand::rng();
45
46 #[allow(clippy::expect_used)] let random_utf8: [u8; LEN_NON_ALLOCATING_CID] = std::array::from_fn(|_| {
48 let symbol = URL_SAFE_SYMBOLS
49 .choose(&mut thread_rng)
50 .expect("URL_SAFE_SYMBOLS slice is not empty");
51
52 *symbol as u8
53 });
54
55 #[allow(clippy::expect_used)] let random_utf8_str =
57 std::str::from_utf8(&random_utf8).expect("URL_SAFE_SYMBOLS are valid utf8");
58
59 Self(SmolStr::new_inline(random_utf8_str))
60 }
61}
62
63impl Default for ClientOrderId<SmolStr> {
64 fn default() -> Self {
65 Self::random()
66 }
67}
68
69#[derive(
70 Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Display, From,
71)]
72pub struct OrderId<T = SmolStr>(pub T);
73
74impl OrderId {
75 pub fn new<S: AsRef<str>>(id: S) -> Self {
76 Self(SmolStr::new(id))
77 }
78}
79
80#[derive(
81 Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Display, From,
82)]
83pub struct StrategyId(pub SmolStr);
84
85impl StrategyId {
86 pub fn new<S: AsRef<str>>(id: S) -> Self {
87 Self(SmolStr::new(id))
88 }
89
90 pub fn unknown() -> Self {
91 Self(SmolStr::new_inline("unknown"))
94 }
95
96 pub const ENGINE_EXPIRY: StrategyId = StrategyId(SmolStr::new_static("__engine_expiry__"));
104}
105
106#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
112pub struct PositionId(pub SmolStr);
113
114impl PositionId {
115 pub fn new(id: impl Into<SmolStr>) -> Self {
116 Self(id.into())
117 }
118
119 pub const NETTING: PositionId = PositionId(SmolStr::new_static("netting"));
121}
122
123impl Default for PositionId {
124 fn default() -> Self {
125 Self::NETTING
126 }
127}
128
129impl std::fmt::Display for PositionId {
130 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
131 self.0.fmt(f)
132 }
133}
134
135#[cfg(test)]
136#[allow(clippy::expect_used)] mod tests {
138 use super::*;
139
140 #[test]
141 fn client_order_id_random_is_23_bytes() {
142 let cid = ClientOrderId::random();
143 assert_eq!(cid.0.len(), 23);
144 }
145
146 #[test]
147 #[cfg(feature = "hyperliquid")]
148 fn client_order_id_uuid_is_valid_uuid() {
149 let cid = ClientOrderId::uuid();
150 assert_eq!(cid.0.len(), 36);
152 uuid::Uuid::parse_str(&cid.0).expect("should be valid UUID");
154 }
155}