use std::{fmt::Display, marker::PhantomData};
use serde::{Deserialize, Serialize};
use super::Id;
use crate::replica::entity::Entity;
#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct EntityId<E: Entity>(Id, PhantomData<E>);
impl<E: Entity> PartialEq for EntityId<E> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<E: Entity> Ord for EntityId<E> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.cmp(&other.0)
}
}
impl<E: Entity> PartialOrd for EntityId<E> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<E: Entity> Eq for EntityId<E> {}
impl<E: Entity> Copy for EntityId<E> {}
impl<E: Entity> Clone for EntityId<E> {
fn clone(&self) -> Self {
*self
}
}
impl<E: Entity> EntityId<E> {
#[must_use]
pub unsafe fn from_id(value: Id) -> Self {
Self(value, PhantomData)
}
#[must_use]
pub fn as_id(&self) -> Id {
self.0
}
#[must_use]
pub fn to_ref_path(&self) -> String {
let mut id_buf = [0; 64];
let id_str = self.as_id().hex_to_buf(&mut id_buf);
format!("refs/{}/{id_str}", E::NAMESPACE)
}
}
impl<E: Entity> Display for EntityId<E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.as_id().fmt(f)
}
}