From e536cb326a67fffd511ead4a87655ca5ef98bf29 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Tue, 23 Sep 2025 17:16:23 +0200 Subject: feat(crates/rocies-server): Don't make the newtype wrappers transparent in the openapi spec This makes using the generated code significantly easier and type safer. --- crates/rocie-server/src/api/get/inventory.rs | 6 ++--- crates/rocie-server/src/api/get/product.rs | 6 ++--- crates/rocie-server/src/api/get/unit.rs | 6 ++--- crates/rocie-server/src/api/set/barcode.rs | 10 ++++---- crates/rocie-server/src/api/set/product.rs | 6 ++--- crates/rocie-server/src/storage/sql/barcode.rs | 30 ++++++++++++++++++++---- crates/rocie-server/src/storage/sql/product.rs | 32 ++++++++++++++++++++++---- crates/rocie-server/src/storage/sql/unit.rs | 31 +++++++++++++++++++++---- 8 files changed, 95 insertions(+), 32 deletions(-) (limited to 'crates/rocie-server/src') diff --git a/crates/rocie-server/src/api/get/inventory.rs b/crates/rocie-server/src/api/get/inventory.rs index 3011430..d1ca436 100644 --- a/crates/rocie-server/src/api/get/inventory.rs +++ b/crates/rocie-server/src/api/get/inventory.rs @@ -2,7 +2,7 @@ use actix_web::{HttpResponse, Responder, Result, get, web}; use crate::{ app::App, - storage::sql::{product::ProductId, product_amount::ProductAmount}, + storage::sql::{product::{ProductId, ProductIdStub}, product_amount::ProductAmount}, }; /// Get the amount of an product @@ -33,11 +33,11 @@ use crate::{ #[get("/inventory/{id}")] pub(crate) async fn amount_by_id( app: web::Data, - id: web::Path, + id: web::Path, ) -> Result { let id = id.into_inner(); - match ProductAmount::from_id(&app, id).await? { + match ProductAmount::from_id(&app, id.into()).await? { Some(product) => Ok(HttpResponse::Ok().json(product)), None => Ok(HttpResponse::NotFound().finish()), } diff --git a/crates/rocie-server/src/api/get/product.rs b/crates/rocie-server/src/api/get/product.rs index c496777..90cb8c8 100644 --- a/crates/rocie-server/src/api/get/product.rs +++ b/crates/rocie-server/src/api/get/product.rs @@ -2,7 +2,7 @@ use actix_web::{HttpResponse, Responder, Result, get, web}; use crate::{ app::App, - storage::sql::product::{Product, ProductId}, + storage::sql::product::{Product, ProductId, ProductIdStub}, }; /// Get Product by id @@ -19,11 +19,11 @@ use crate::{ #[get("/product/{id}")] pub(crate) async fn product_by_id( app: web::Data, - id: web::Path, + id: web::Path, ) -> Result { let id = id.into_inner(); - match Product::from_id(&app, id).await? { + match Product::from_id(&app, id.into()).await? { Some(product) => Ok(HttpResponse::Ok().json(product)), None => Ok(HttpResponse::NotFound().finish()), } diff --git a/crates/rocie-server/src/api/get/unit.rs b/crates/rocie-server/src/api/get/unit.rs index d006818..4854ea3 100644 --- a/crates/rocie-server/src/api/get/unit.rs +++ b/crates/rocie-server/src/api/get/unit.rs @@ -1,6 +1,6 @@ use actix_web::{get, web, HttpResponse, Responder, Result}; -use crate::{app::App, storage::sql::unit::{Unit, UnitId}}; +use crate::{app::App, storage::sql::unit::{Unit, UnitId, UnitIdStub}}; /// Return all registered units #[utoipa::path( @@ -30,11 +30,11 @@ pub(crate) async fn units(app: web::Data) -> Result { #[get("/unit/{id}")] pub(crate) async fn unit_by_id( app: web::Data, - id: web::Path, + id: web::Path, ) -> Result { let id = id.into_inner(); - match Unit::from_id(&app, id).await? { + match Unit::from_id(&app, id.into()).await? { Some(product) => Ok(HttpResponse::Ok().json(product)), None => Ok(HttpResponse::NotFound().finish()), } diff --git a/crates/rocie-server/src/api/set/barcode.rs b/crates/rocie-server/src/api/set/barcode.rs index a89bf4f..60b1650 100644 --- a/crates/rocie-server/src/api/set/barcode.rs +++ b/crates/rocie-server/src/api/set/barcode.rs @@ -4,7 +4,7 @@ use log::debug; use crate::{ app::App, storage::sql::{ - barcode::{Barcode, BarcodeId, UnitAmount}, + barcode::{Barcode, BarcodeId, BarcodeIdStub, UnitAmount}, insert::Operations, }, }; @@ -33,11 +33,11 @@ use crate::{ #[post("/barcode/{id}/buy")] pub(crate) async fn buy_barcode( app: web::Data, - barcode_id: web::Path, + barcode_id: web::Path, ) -> Result { let mut ops = Operations::new("buy barcode unit"); - let barcode = Barcode::from_id(&app, barcode_id.into_inner()).await?; + let barcode = Barcode::from_id(&app, barcode_id.into_inner().into()).await?; match barcode { Some(barcode) => { @@ -76,12 +76,12 @@ pub(crate) async fn buy_barcode( #[post("/barcode/{id}/consume")] pub(crate) async fn consume_barcode( app: web::Data, - barcode_id: web::Path, + barcode_id: web::Path, unit_amount: web::Json, ) -> Result { let mut ops = Operations::new("consume barcode unit"); - let barcode = Barcode::from_id(&app, barcode_id.into_inner()).await?; + let barcode = Barcode::from_id(&app, barcode_id.into_inner().into()).await?; debug!("Starting consume for barcode: {barcode:?}"); match barcode { diff --git a/crates/rocie-server/src/api/set/product.rs b/crates/rocie-server/src/api/set/product.rs index 19024c7..d347ee7 100644 --- a/crates/rocie-server/src/api/set/product.rs +++ b/crates/rocie-server/src/api/set/product.rs @@ -7,7 +7,7 @@ use crate::{ storage::sql::{ barcode::Barcode, insert::Operations, - product::{Product, ProductId}, + product::{Product, ProductId, ProductIdStub}, unit::Unit, }, }; @@ -84,7 +84,7 @@ pub(crate) async fn register_product( #[post("/product/{id}/associate")] pub(crate) async fn associate_barcode( app: web::Data, - id: web::Path, + id: web::Path, barcode: web::Json, ) -> Result { let mut ops = Operations::new("associated barcode with product"); @@ -97,7 +97,7 @@ pub(crate) async fn associate_barcode( } } - match Product::from_id(&app, id.into_inner()).await? { + match Product::from_id(&app, id.into_inner().into()).await? { Some(product) => { product.associate_barcode(barcode.into_inner(), &mut ops); diff --git a/crates/rocie-server/src/storage/sql/barcode.rs b/crates/rocie-server/src/storage/sql/barcode.rs index 239ed8c..1c3c55a 100644 --- a/crates/rocie-server/src/storage/sql/barcode.rs +++ b/crates/rocie-server/src/storage/sql/barcode.rs @@ -5,26 +5,46 @@ use crate::storage::sql::unit::UnitId; #[derive(ToSchema, Debug, Clone, Serialize, Deserialize)] pub(crate) struct Barcode { - #[schema(format = Int64, minimum = 0)] pub(crate) id: BarcodeId, pub(crate) amount: UnitAmount, } #[derive(ToSchema, Debug, Clone, Copy, Serialize, Deserialize)] -pub(crate) struct BarcodeId(u32); +pub(crate) struct BarcodeId { + #[schema(minimum = 0)] + pub(crate) value: u32, +} +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] +#[serde(from = "u32")] +pub(crate) struct BarcodeIdStub { + value: u32, +} impl BarcodeId { pub(crate) fn to_db(self) -> i64 { - i64::from(self.0) + i64::from(self.value) } pub(crate) fn from_db(val: i64) -> Self { - Self(u32::try_from(val).expect("Should be strictly positive")) + Self { + value: u32::try_from(val).expect("Should be strictly positive"), + } + } +} + +impl From for BarcodeIdStub { + fn from(value: u32) -> Self { + Self { value } + } +} +impl From for BarcodeId { + fn from(value: BarcodeIdStub) -> Self { + Self { value: value.value } } } #[derive(ToSchema, Debug, Clone, Copy, Serialize, Deserialize)] pub(crate) struct UnitAmount { - #[schema(format = Int64, minimum = 0)] + #[schema(minimum = 0)] pub(crate) value: u32, pub(crate) unit: UnitId, } diff --git a/crates/rocie-server/src/storage/sql/product.rs b/crates/rocie-server/src/storage/sql/product.rs index 93cc6a0..c94fcce 100644 --- a/crates/rocie-server/src/storage/sql/product.rs +++ b/crates/rocie-server/src/storage/sql/product.rs @@ -18,23 +18,45 @@ pub(crate) struct Product { #[derive( Deserialize, Serialize, Debug, Default, ToSchema, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, )] -pub(crate) struct ProductId(Uuid); +pub(crate) struct ProductId { + value: Uuid, +} + +#[derive(Deserialize, Serialize, Debug, Clone, Copy)] +#[serde(from = "Uuid")] +pub(crate) struct ProductIdStub { + value: Uuid, +} impl ProductId { pub(crate) fn from_db(id: &str) -> ProductId { - Self(Uuid::from_str(id).expect("We put an uuid into the db, it should also go out again")) + Self { + value: Uuid::from_str(id) + .expect("We put an uuid into the db, it should also go out again"), + } } } impl Display for ProductId { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) + write!(f, "{}", self.value) } } impl From for ProductId { fn from(value: Uuid) -> Self { - Self(value) + Self { value } + } +} + +impl From for ProductIdStub { + fn from(value: Uuid) -> Self { + Self { value } + } +} +impl From for ProductId { + fn from(value: ProductIdStub) -> Self { + Self { value: value.value } } } @@ -46,7 +68,7 @@ where &self, buf: &mut ::ArgumentBuffer<'q>, ) -> Result { - let inner = self.0.to_string(); + let inner = self.value.to_string(); Encode::::encode_by_ref(&inner, buf) } } diff --git a/crates/rocie-server/src/storage/sql/unit.rs b/crates/rocie-server/src/storage/sql/unit.rs index fe00b1b..77e7a2e 100644 --- a/crates/rocie-server/src/storage/sql/unit.rs +++ b/crates/rocie-server/src/storage/sql/unit.rs @@ -17,23 +17,44 @@ pub(crate) struct Unit { #[derive( Deserialize, Serialize, Debug, Default, ToSchema, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, )] -pub(crate) struct UnitId(Uuid); +pub(crate) struct UnitId { + value: Uuid, +} + +#[derive(Deserialize, Serialize, Debug, Clone, Copy)] +#[serde(from = "Uuid")] +pub(crate) struct UnitIdStub { + value: 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")) + Self { + value: 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) + write!(f, "{}", self.value) } } impl From for UnitId { fn from(value: Uuid) -> Self { - Self(value) + Self { value } + } +} +impl From for UnitIdStub { + fn from(value: Uuid) -> Self { + Self { value } + } +} +impl From for UnitId { + fn from(value: UnitIdStub) -> Self { + Self { value: value.value } } } @@ -45,7 +66,7 @@ where &self, buf: &mut ::ArgumentBuffer<'q>, ) -> Result { - let inner = self.0.to_string(); + let inner = self.value.to_string(); Encode::::encode_by_ref(&inner, buf) } } -- cgit 1.4.1