From 1c09b0eb5db415985bfefb52786dbe48d757665e Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sat, 6 Sep 2025 18:31:40 +0200 Subject: feat: Provide basic barcode handling support --- crates/rocie-server/src/storage/sql/unit.rs | 59 +++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 crates/rocie-server/src/storage/sql/unit.rs (limited to 'crates/rocie-server/src/storage/sql/unit.rs') 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, +} + +#[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() + } +} -- cgit 1.4.1