use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::marker::PhantomData;
use sea_orm::sea_query::{ArrayType, Nullable, ValueType, ValueTypeErr};
use sea_orm::{ColIdx, ColumnType, DbErr, QueryResult, TryFromU64, TryGetError, TryGetable, Value};
pub type SeaOrmRepr = i64;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[repr(transparent)]
pub struct Id<T> {
id: SeaOrmRepr,
#[cfg_attr(feature = "serde", serde(skip_serializing, default))]
_phantom: PhantomData<T>,
}
impl<T> Clone for Id<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for Id<T> {}
impl<T> PartialEq for Id<T> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl<T> Eq for Id<T> {}
impl<T> PartialOrd for Id<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<T> Ord for Id<T> {
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}
impl<T> Hash for Id<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
impl<T> Id<T> {
pub fn from_raw(raw: SeaOrmRepr) -> Self {
Self {
id: raw,
_phantom: PhantomData,
}
}
pub fn into_raw(self) -> SeaOrmRepr {
self.id
}
}
impl<T> fmt::Debug for Id<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Id")
.field("T", &core::any::type_name::<T>())
.field("id", &self.id)
.finish()
}
}
impl<T> ValueType for Id<T> {
fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
<SeaOrmRepr as ValueType>::try_from(v).map(|id| Self {
id,
_phantom: PhantomData,
})
}
fn type_name() -> String {
format!("Id<{}>", &core::any::type_name::<T>())
}
fn array_type() -> ArrayType {
SeaOrmRepr::array_type()
}
fn column_type() -> ColumnType {
SeaOrmRepr::column_type()
}
}
impl<T> From<Id<T>> for Value {
fn from(value: Id<T>) -> Self {
value.id.into()
}
}
impl<T> TryGetable for Id<T> {
fn try_get_by<I: ColIdx>(res: &QueryResult, index: I) -> Result<Self, TryGetError> {
SeaOrmRepr::try_get_by(res, index).map(|id| Self {
id,
_phantom: PhantomData,
})
}
}
impl<T> TryFromU64 for Id<T> {
fn try_from_u64(n: u64) -> Result<Self, DbErr> {
SeaOrmRepr::try_from_u64(n).map(|id| Self {
id,
_phantom: PhantomData,
})
}
}
impl<T> Nullable for Id<T> {
fn null() -> Value {
SeaOrmRepr::null()
}
}
#[cfg(test)]
mod tests {
use sea_orm::{
ActiveModelBehavior, DeriveEntityModel, DerivePrimaryKey, DeriveRelation, EntityTrait,
EnumIter, PrimaryKeyTrait,
};
use super::Id;
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "ids")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
id: Id<Model>,
nullable: Option<Id<Model>>,
}
impl ActiveModelBehavior for ActiveModel {}
#[allow(dead_code)]
#[derive(Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
#[test]
#[cfg(feature = "serde")]
fn test_serde() {
let id: Id<()> = Id::from_raw(1234);
serde_test::assert_tokens(&id, &[serde_test::Token::I64(1234)]);
}
}