about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/storage/sql/unit.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rocie-server/src/storage/sql/unit.rs')
-rw-r--r--crates/rocie-server/src/storage/sql/unit.rs100
1 files changed, 35 insertions, 65 deletions
diff --git a/crates/rocie-server/src/storage/sql/unit.rs b/crates/rocie-server/src/storage/sql/unit.rs
index 77e7a2e..d16e783 100644
--- a/crates/rocie-server/src/storage/sql/unit.rs
+++ b/crates/rocie-server/src/storage/sql/unit.rs
@@ -1,80 +1,50 @@
-use std::{fmt::Display, str::FromStr};
-
 use serde::{Deserialize, Serialize};
-use sqlx::{Database, Encode, Type};
 use utoipa::ToSchema;
-use uuid::Uuid;
+
+use crate::storage::sql::{mk_id, unit_property::UnitPropertyId};
 
 #[derive(ToSchema, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
 pub(crate) struct Unit {
+    /// Unique id for this unit.
     pub(crate) id: UnitId,
+
+    /// The singular version of this unit's name.
+    /// E.g.:
+    ///     Kilogram
+    ///     Gram
     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 {
-    value: Uuid,
-}
+    /// The plural version of this unit's name. Is used by a value of two and more.
+    /// (We do not support Slovenian dual numerus versions)
+    /// E.g.:
+    ///     Kilogram -> Kilograms
+    ///     gram -> meters
+    pub(crate) full_name_plural: String,
 
-#[derive(Deserialize, Serialize, Debug, Clone, Copy)]
-#[serde(from = "Uuid")]
-pub(crate) struct UnitIdStub {
-    value: Uuid,
-}
+    /// Short name or abbreviation of this unit.
+    /// E.g.:
+    ///     kg for Kilogram
+    ///     g for gram
+    ///     m for meter
+    pub(crate) short_name: String,
 
-impl UnitId {
-    pub(crate) fn from_db(id: &str) -> UnitId {
-        Self {
-            value: Uuid::from_str(id)
-                .expect("We put an uuid into the db, it should also go out again"),
-        }
-    }
-}
+    /// Description of this unit.
+    pub(crate) description: Option<String>,
 
-impl Display for UnitId {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(f, "{}", self.value)
-    }
+    /// Which property is described by this unit.
+    /// E.g.:
+    ///     kg -> Mass
+    ///     L -> Volume
+    ///     m/s -> Speed
+    ///     and so forth
+    pub(crate) unit_property: UnitPropertyId,
 }
 
-impl From<Uuid> for UnitId {
-    fn from(value: Uuid) -> Self {
-        Self { value }
-    }
-}
-impl From<Uuid> for UnitIdStub {
-    fn from(value: Uuid) -> Self {
-        Self { value }
-    }
-}
-impl From<UnitIdStub> for UnitId {
-    fn from(value: UnitIdStub) -> Self {
-        Self { value: value.value }
-    }
+#[derive(ToSchema, Debug, Clone, Copy, Serialize, Deserialize)]
+pub(crate) struct UnitAmount {
+    #[schema(minimum = 0)]
+    pub(crate) value: u32,
+    pub(crate) unit: UnitId,
 }
 
-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.value.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()
-    }
-}
+mk_id!(UnitId and UnitIdStub);