ids.rs

 1use crate::Result;
 2use sea_orm::{entity::prelude::*, DbErr};
 3use serde::{Deserialize, Serialize};
 4
 5macro_rules! id_type {
 6    ($name:ident) => {
 7        #[derive(
 8            Clone,
 9            Copy,
10            Debug,
11            Default,
12            PartialEq,
13            Eq,
14            PartialOrd,
15            Ord,
16            Hash,
17            Serialize,
18            Deserialize,
19            DeriveValueType,
20        )]
21        #[serde(transparent)]
22        pub struct $name(pub i32);
23
24        impl $name {
25            #[allow(unused)]
26            pub const MAX: Self = Self(i32::MAX);
27
28            #[allow(unused)]
29            pub fn from_proto(value: u64) -> Self {
30                Self(value as i32)
31            }
32
33            #[allow(unused)]
34            pub fn to_proto(self) -> u64 {
35                self.0 as u64
36            }
37        }
38
39        impl std::fmt::Display for $name {
40            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
41                self.0.fmt(f)
42            }
43        }
44
45        impl sea_orm::TryFromU64 for $name {
46            fn try_from_u64(n: u64) -> Result<Self, DbErr> {
47                Ok(Self(n.try_into().map_err(|_| {
48                    DbErr::ConvertFromU64(concat!(
49                        "error converting ",
50                        stringify!($name),
51                        " to u64"
52                    ))
53                })?))
54            }
55        }
56
57        impl sea_orm::sea_query::Nullable for $name {
58            fn null() -> Value {
59                Value::Int(None)
60            }
61        }
62    };
63}
64
65id_type!(BufferId);
66id_type!(AccessTokenId);
67id_type!(ChannelChatParticipantId);
68id_type!(ChannelId);
69id_type!(ChannelMemberId);
70id_type!(MessageId);
71id_type!(ContactId);
72id_type!(FollowerId);
73id_type!(RoomId);
74id_type!(RoomParticipantId);
75id_type!(ProjectId);
76id_type!(ProjectCollaboratorId);
77id_type!(ReplicaId);
78id_type!(ServerId);
79id_type!(SignupId);
80id_type!(UserId);
81id_type!(ChannelBufferCollaboratorId);
82id_type!(FlagId);