ts3/
types.rs

1use std::fmt::{self, Display, Formatter};
2
3use crate::{Decode, Encode};
4
5#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
6pub struct ServerId(pub u64);
7
8#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
9#[repr(transparent)]
10pub struct ClientId(pub u64);
11
12#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
13#[repr(transparent)]
14pub struct ClientDatabaseId(pub u64);
15
16#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
17#[repr(transparent)]
18pub struct ChannelId(pub u64);
19
20#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
21#[repr(transparent)]
22pub struct ServerGroupId(pub u64);
23
24#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
25#[repr(transparent)]
26pub struct ChannelGroupId(pub u64);
27
28#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
29#[repr(transparent)]
30pub struct ApiKeyId(pub u64);
31
32macro_rules! id_impls {
33    ($($t:ty),*$(,)?) => {
34        $(
35            impl Display for $t {
36                #[inline]
37                fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
38                    Display::fmt(&self.0, f)
39                }
40            }
41
42            impl Encode for $t {
43                #[inline]
44                fn encode(&self, buf: &mut String) {
45                    self.0.encode(buf)
46                }
47            }
48
49            impl Decode for $t {
50                type Error = <u64 as Decode>::Error;
51
52                #[inline]
53                fn decode(buf: &[u8]) -> Result<Self, Self::Error> {
54                    u64::decode(buf).map(Self)
55                }
56            }
57
58            impl From<u64> for $t {
59                #[inline]
60                fn from(value: u64) -> Self {
61                    Self(value)
62                }
63            }
64        )*
65    };
66}
67
68id_impls! {
69    ServerId,
70    ClientId,
71    ClientDatabaseId,
72    ChannelId,
73    ServerGroupId,
74    ChannelGroupId,
75    ApiKeyId,
76}