df_hyperliquid_core/trade/orders/
cloid.rs1use std::{borrow::Cow, fmt, str::FromStr};
5
6use serde::{Deserialize, Deserializer, de};
7
8use crate::utils::hex::{self, BadHex};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub struct Cloid([u8; 16]);
13
14impl Cloid {
15 #[must_use]
16 pub const fn from_bytes(bytes: [u8; 16]) -> Cloid {
17 Cloid(bytes)
18 }
19
20 #[must_use]
21 pub const fn from_u128(value: u128) -> Cloid {
22 Cloid(value.to_be_bytes())
23 }
24
25 #[must_use]
26 pub const fn bytes(&self) -> &[u8; 16] {
27 &self.0
28 }
29
30 #[must_use]
31 pub fn to_hex(self) -> String {
32 hex::encode_prefixed(&self.0)
33 }
34}
35
36impl FromStr for Cloid {
37 type Err = BadHex;
38
39 fn from_str(text: &str) -> Result<Self, Self::Err> {
40 hex::decode_array(text).map(Cloid)
41 }
42}
43
44impl fmt::Display for Cloid {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 f.write_str(&self.to_hex())
47 }
48}
49
50impl<'de> Deserialize<'de> for Cloid {
52 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
53 <Cow<'de, str>>::deserialize(deserializer)?.parse().map_err(de::Error::custom)
54 }
55}