1use crate::Result;
2use sea_orm::DbErr;
3use sea_query::{Value, ValueTypeErr};
4use serde::{Deserialize, Serialize};
5
6macro_rules! id_type {
7 ($name:ident) => {
8 #[derive(
9 Clone,
10 Copy,
11 Debug,
12 Default,
13 PartialEq,
14 Eq,
15 PartialOrd,
16 Ord,
17 Hash,
18 Serialize,
19 Deserialize,
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 From<$name> for sea_query::Value {
46 fn from(value: $name) -> Self {
47 sea_query::Value::Int(Some(value.0))
48 }
49 }
50
51 impl sea_orm::TryGetable for $name {
52 fn try_get(
53 res: &sea_orm::QueryResult,
54 pre: &str,
55 col: &str,
56 ) -> Result<Self, sea_orm::TryGetError> {
57 Ok(Self(i32::try_get(res, pre, col)?))
58 }
59 }
60
61 impl sea_query::ValueType for $name {
62 fn try_from(v: Value) -> Result<Self, sea_query::ValueTypeErr> {
63 Ok(Self(value_to_integer(v)?))
64 }
65
66 fn type_name() -> String {
67 stringify!($name).into()
68 }
69
70 fn array_type() -> sea_query::ArrayType {
71 sea_query::ArrayType::Int
72 }
73
74 fn column_type() -> sea_query::ColumnType {
75 sea_query::ColumnType::Integer(None)
76 }
77 }
78
79 impl sea_orm::TryFromU64 for $name {
80 fn try_from_u64(n: u64) -> Result<Self, DbErr> {
81 Ok(Self(n.try_into().map_err(|_| {
82 DbErr::ConvertFromU64(concat!(
83 "error converting ",
84 stringify!($name),
85 " to u64"
86 ))
87 })?))
88 }
89 }
90
91 impl sea_query::Nullable for $name {
92 fn null() -> Value {
93 Value::Int(None)
94 }
95 }
96 };
97}
98
99fn value_to_integer(v: Value) -> Result<i32, ValueTypeErr> {
100 match v {
101 Value::TinyInt(Some(int)) => int.try_into().map_err(|_| ValueTypeErr),
102 Value::SmallInt(Some(int)) => int.try_into().map_err(|_| ValueTypeErr),
103 Value::Int(Some(int)) => int.try_into().map_err(|_| ValueTypeErr),
104 Value::BigInt(Some(int)) => int.try_into().map_err(|_| ValueTypeErr),
105 Value::TinyUnsigned(Some(int)) => int.try_into().map_err(|_| ValueTypeErr),
106 Value::SmallUnsigned(Some(int)) => int.try_into().map_err(|_| ValueTypeErr),
107 Value::Unsigned(Some(int)) => int.try_into().map_err(|_| ValueTypeErr),
108 Value::BigUnsigned(Some(int)) => int.try_into().map_err(|_| ValueTypeErr),
109 _ => Err(ValueTypeErr),
110 }
111}
112
113id_type!(BufferId);
114id_type!(AccessTokenId);
115id_type!(ChannelId);
116id_type!(ChannelMemberId);
117id_type!(ContactId);
118id_type!(FollowerId);
119id_type!(RoomId);
120id_type!(RoomParticipantId);
121id_type!(ProjectId);
122id_type!(ProjectCollaboratorId);
123id_type!(ReplicaId);
124id_type!(ServerId);
125id_type!(SignupId);
126id_type!(UserId);
127id_type!(ChannelBufferCollaboratorId);
128id_type!(FlagId);