Skip to main content

bat_markets_core/
ids.rs

1use core::fmt;
2
3use serde::{Deserialize, Serialize};
4
5macro_rules! string_id {
6    ($name:ident) => {
7        #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
8        pub struct $name(Box<str>);
9
10        impl $name {
11            #[must_use]
12            pub fn new(value: impl Into<Box<str>>) -> Self {
13                Self(value.into())
14            }
15
16            #[must_use]
17            pub fn as_str(&self) -> &str {
18                self.0.as_ref()
19            }
20        }
21
22        impl From<&str> for $name {
23            fn from(value: &str) -> Self {
24                Self(value.into())
25            }
26        }
27
28        impl From<String> for $name {
29            fn from(value: String) -> Self {
30                Self(value.into_boxed_str())
31            }
32        }
33
34        impl AsRef<str> for $name {
35            fn as_ref(&self) -> &str {
36                self.as_str()
37            }
38        }
39
40        impl fmt::Display for $name {
41            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42                f.write_str(self.as_str())
43            }
44        }
45    };
46}
47
48string_id!(AssetCode);
49string_id!(InstrumentId);
50string_id!(OrderId);
51string_id!(ClientOrderId);
52string_id!(PositionId);
53string_id!(RequestId);
54string_id!(TradeId);