60 lines
1.2 KiB
Rust
60 lines
1.2 KiB
Rust
use std::{
|
|
fmt::Display,
|
|
marker::PhantomData,
|
|
ops::{Deref, DerefMut},
|
|
};
|
|
use url::Url;
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
pub struct ObjectId<Kind> {
|
|
url: Url,
|
|
_kind: PhantomData<fn() -> Kind>,
|
|
}
|
|
|
|
impl<Kind> ObjectId<Kind> {
|
|
pub fn new(url: Url) -> Self {
|
|
Self {
|
|
url,
|
|
_kind: PhantomData,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<Kind> Deref for ObjectId<Kind> {
|
|
type Target = Url;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.url
|
|
}
|
|
}
|
|
|
|
impl<Kind> DerefMut for ObjectId<Kind> {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
&mut self.url
|
|
}
|
|
}
|
|
|
|
impl<Kind> Display for ObjectId<Kind> {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
self.url.fmt(f)
|
|
}
|
|
}
|
|
|
|
impl<Kind> serde::ser::Serialize for ObjectId<Kind> {
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
where
|
|
S: serde::Serializer,
|
|
{
|
|
self.url.serialize(serializer)
|
|
}
|
|
}
|
|
|
|
impl<'de, Kind> serde::de::Deserialize<'de> for ObjectId<Kind> {
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
where
|
|
D: serde::Deserializer<'de>,
|
|
{
|
|
Url::deserialize(deserializer).map(ObjectId::new)
|
|
}
|
|
}
|