61 lines
1.4 KiB
Rust
61 lines
1.4 KiB
Rust
use std::fmt::{Debug, Display};
|
|
|
|
use chrono::Utc;
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::{Error, Uuid};
|
|
|
|
#[derive(
|
|
Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, sqlx::Type, Serialize, Deserialize,
|
|
)]
|
|
#[sqlx(transparent)]
|
|
pub struct DbId(pub Uuid);
|
|
|
|
impl DbId {
|
|
pub fn is_nil(&self) -> bool {
|
|
self.0.is_nil()
|
|
}
|
|
|
|
pub fn new() -> Self {
|
|
Self(Uuid::new_v4())
|
|
}
|
|
|
|
pub fn from_string(s: &str) -> Result<Self, Error> {
|
|
Ok(Self(Uuid::try_parse(s)?))
|
|
}
|
|
|
|
pub fn as_string(&self) -> String {
|
|
self.0.simple().to_string()
|
|
}
|
|
|
|
pub fn created_at(&self) -> chrono::DateTime<Utc> {
|
|
chrono::DateTime::default()
|
|
}
|
|
}
|
|
|
|
//-************************************************************************
|
|
// standard trait impls
|
|
//-************************************************************************
|
|
|
|
impl Display for DbId {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{}", self.0.to_string())
|
|
}
|
|
}
|
|
|
|
impl Debug for DbId {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
f.debug_tuple("DbId").field(&self.as_string()).finish()
|
|
}
|
|
}
|
|
|
|
impl From<Uuid> for DbId {
|
|
fn from(value: Uuid) -> Self {
|
|
DbId(value)
|
|
}
|
|
}
|
|
|
|
impl From<u128> for DbId {
|
|
fn from(value: u128) -> Self {
|
|
DbId(Uuid::from_u128(value))
|
|
}
|
|
}
|