diff options
Diffstat (limited to 'crates/rocie-server/src/storage/sql/unit.rs')
| -rw-r--r-- | crates/rocie-server/src/storage/sql/unit.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/crates/rocie-server/src/storage/sql/unit.rs b/crates/rocie-server/src/storage/sql/unit.rs new file mode 100644 index 0000000..fe00b1b --- /dev/null +++ b/crates/rocie-server/src/storage/sql/unit.rs @@ -0,0 +1,59 @@ +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<String>, +} + +#[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<Uuid> 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 <DB as Database>::ArgumentBuffer<'q>, + ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> { + let inner = self.0.to_string(); + Encode::<DB>::encode_by_ref(&inner, buf) + } +} +impl<DB: Database> Type<DB> for UnitId +where + String: Type<DB>, +{ + fn type_info() -> DB::TypeInfo { + <String as Type<DB>>::type_info() + } +} |
