use std::{fmt::Display, str::FromStr}; use serde::{Deserialize, Serialize}; use sqlx::{Database, Encode, Type}; use utoipa::ToSchema; use uuid::Uuid; #[derive(ToSchema, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize)] pub(crate) struct Unit { pub(crate) id: UnitId, pub(crate) full_name_singular: String, pub(crate) full_name_plural: String, pub(crate) short_name: String, pub(crate) description: Option, } #[derive( Deserialize, Serialize, Debug, Default, ToSchema, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, )] pub(crate) struct UnitId(Uuid); impl UnitId { pub(crate) fn from_db(id: &str) -> UnitId { Self(Uuid::from_str(id).expect("We put an uuid into the db, it should also go out again")) } } impl Display for UnitId { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } impl From for UnitId { fn from(value: Uuid) -> Self { Self(value) } } impl<'q, DB: Database> Encode<'q, DB> for UnitId where String: Encode<'q, DB>, { fn encode_by_ref( &self, buf: &mut ::ArgumentBuffer<'q>, ) -> Result { let inner = self.0.to_string(); Encode::::encode_by_ref(&inner, buf) } } impl Type for UnitId where String: Type, { fn type_info() -> DB::TypeInfo { >::type_info() } }