Skip to main content

rustrade_execution/order/
id.rs

1use 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    /// Construct a `ClientOrderId` from the specified string.
13    ///
14    /// Use [`Self::random`] to generate a random stack-allocated `ClientOrderId`.
15    pub fn new<S: Into<SmolStr>>(id: S) -> Self {
16        Self(id.into())
17    }
18
19    /// Construct a stack-allocated `ClientOrderId` backed by a 23 byte [`SmolStr`].
20    pub fn random() -> Self {
21        const LEN_URL_SAFE_SYMBOLS: usize = 64;
22        const URL_SAFE_SYMBOLS: [char; LEN_URL_SAFE_SYMBOLS] = [
23            '_', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
24            'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
25            'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
26            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
27        ];
28        // SmolStr can be up to 23 bytes long without allocating
29        const LEN_NON_ALLOCATING_CID: usize = 23;
30
31        let mut thread_rng = rand::rng();
32
33        #[allow(clippy::expect_used)] // Invariant: URL_SAFE_SYMBOLS is a const 64-element array
34        let random_utf8: [u8; LEN_NON_ALLOCATING_CID] = std::array::from_fn(|_| {
35            let symbol = URL_SAFE_SYMBOLS
36                .choose(&mut thread_rng)
37                .expect("URL_SAFE_SYMBOLS slice is not empty");
38
39            *symbol as u8
40        });
41
42        #[allow(clippy::expect_used)] // Invariant: all URL_SAFE_SYMBOLS chars are ASCII
43        let random_utf8_str =
44            std::str::from_utf8(&random_utf8).expect("URL_SAFE_SYMBOLS are valid utf8");
45
46        Self(SmolStr::new_inline(random_utf8_str))
47    }
48}
49
50impl Default for ClientOrderId<SmolStr> {
51    fn default() -> Self {
52        Self::random()
53    }
54}
55
56#[derive(
57    Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Display, From,
58)]
59pub struct OrderId<T = SmolStr>(pub T);
60
61impl OrderId {
62    pub fn new<S: AsRef<str>>(id: S) -> Self {
63        Self(SmolStr::new(id))
64    }
65}
66
67#[derive(
68    Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Display, From,
69)]
70pub struct StrategyId(pub SmolStr);
71
72impl StrategyId {
73    pub fn new<S: AsRef<str>>(id: S) -> Self {
74        Self(SmolStr::new(id))
75    }
76
77    pub fn unknown() -> Self {
78        // "unknown" is 7 bytes — always inline; new_inline avoids the heap-allocation
79        // branch in SmolStr::new and is safe because the literal fits.
80        Self(SmolStr::new_inline("unknown"))
81    }
82
83    /// The fixed `StrategyId` used for synthetic settlement trades generated by the engine
84    /// on `ContractExpiry`. Using a `pub const` prevents bypassing any future validation.
85    ///
86    /// Note: conceptually this constant belongs to `rustrade::engine` (it is used exclusively
87    /// by the engine's contract lifecycle logic). It lives here because `StrategyId` is
88    /// defined in `rustrade-execution`; moving it to `rustrade::engine` would add a `smol_str`
89    /// import to that crate for a single constant.
90    pub const ENGINE_EXPIRY: StrategyId = StrategyId(SmolStr::new_static("__engine_expiry__"));
91}
92
93/// Opaque identifier for a tracked position.
94///
95/// In `OmsMode::Netting` this is always `"netting"` (at most one position per instrument).
96/// In `OmsMode::Hedging` this is derived from the `ClientOrderId` of the opening order,
97/// specified via [`crate::order::request::RequestOpen::position_id`] at order submission.
98#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
99pub struct PositionId(pub SmolStr);
100
101impl PositionId {
102    pub fn new(id: impl Into<SmolStr>) -> Self {
103        Self(id.into())
104    }
105
106    /// The fixed `PositionId` used for all netting-mode positions.
107    pub const NETTING: PositionId = PositionId(SmolStr::new_static("netting"));
108}
109
110impl Default for PositionId {
111    fn default() -> Self {
112        Self::NETTING
113    }
114}
115
116impl std::fmt::Display for PositionId {
117    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118        self.0.fmt(f)
119    }
120}