nautilus_model/identifiers/
client_order_id.rs
1use std::{
19 fmt::{Debug, Display, Formatter},
20 hash::Hash,
21};
22
23use nautilus_core::correctness::{FAILED, check_valid_string};
24use ustr::Ustr;
25
26#[repr(C)]
28#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
29#[cfg_attr(
30 feature = "python",
31 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
32)]
33pub struct ClientOrderId(Ustr);
34
35impl ClientOrderId {
36 pub fn new_checked<T: AsRef<str>>(value: T) -> anyhow::Result<Self> {
47 let value = value.as_ref();
48 check_valid_string(value, stringify!(value))?;
49 Ok(Self(Ustr::from(value)))
50 }
51
52 pub fn new<T: AsRef<str>>(value: T) -> Self {
59 Self::new_checked(value).expect(FAILED)
60 }
61
62 #[allow(dead_code)]
64 pub(crate) fn set_inner(&mut self, value: &str) {
65 self.0 = Ustr::from(value);
66 }
67
68 #[must_use]
70 pub fn inner(&self) -> Ustr {
71 self.0
72 }
73
74 #[must_use]
76 pub fn as_str(&self) -> &str {
77 self.0.as_str()
78 }
79}
80
81impl Debug for ClientOrderId {
82 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
83 write!(f, "{:?}", self.0)
84 }
85}
86
87impl Display for ClientOrderId {
88 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
89 write!(f, "{}", self.0)
90 }
91}
92
93#[must_use]
94pub fn optional_ustr_to_vec_client_order_ids(s: Option<Ustr>) -> Option<Vec<ClientOrderId>> {
95 s.map(|ustr| {
96 let s_str = ustr.to_string();
97 s_str
98 .split(',')
99 .map(ClientOrderId::new)
100 .collect::<Vec<ClientOrderId>>()
101 })
102}
103
104#[must_use]
105pub fn optional_vec_client_order_ids_to_ustr(vec: Option<Vec<ClientOrderId>>) -> Option<Ustr> {
106 vec.map(|client_order_ids| {
107 let s: String = client_order_ids
108 .into_iter()
109 .map(|id| id.to_string())
110 .collect::<Vec<String>>()
111 .join(",");
112 Ustr::from(&s)
113 })
114}
115
116#[cfg(test)]
120mod tests {
121 use rstest::rstest;
122 use ustr::Ustr;
123
124 use super::ClientOrderId;
125 use crate::identifiers::{
126 client_order_id::{
127 optional_ustr_to_vec_client_order_ids, optional_vec_client_order_ids_to_ustr,
128 },
129 stubs::*,
130 };
131
132 #[rstest]
133 fn test_string_reprs(client_order_id: ClientOrderId) {
134 assert_eq!(client_order_id.as_str(), "O-19700101-000000-001-001-1");
135 assert_eq!(format!("{client_order_id}"), "O-19700101-000000-001-001-1");
136 }
137
138 #[rstest]
139 fn test_optional_ustr_to_vec_client_order_ids() {
140 assert_eq!(optional_ustr_to_vec_client_order_ids(None), None);
142
143 let ustr = Ustr::from("id1,id2,id3");
145 let client_order_ids = optional_ustr_to_vec_client_order_ids(Some(ustr)).unwrap();
146 assert_eq!(client_order_ids[0].as_str(), "id1");
147 assert_eq!(client_order_ids[1].as_str(), "id2");
148 assert_eq!(client_order_ids[2].as_str(), "id3");
149 }
150
151 #[rstest]
152 fn test_optional_vec_client_order_ids_to_ustr() {
153 assert_eq!(optional_vec_client_order_ids_to_ustr(None), None);
155
156 let client_order_ids = vec![
158 ClientOrderId::from("id1"),
159 ClientOrderId::from("id2"),
160 ClientOrderId::from("id3"),
161 ];
162 let ustr = optional_vec_client_order_ids_to_ustr(Some(client_order_ids)).unwrap();
163 assert_eq!(ustr.to_string(), "id1,id2,id3");
164 }
165}