use serde::{Deserialize, Serialize}; use utoipa::ToSchema; use crate::storage::sql::unit::UnitAmount; #[derive(ToSchema, Debug, Clone, Serialize, Deserialize)] pub(crate) struct Barcode { pub(crate) id: BarcodeId, pub(crate) amount: UnitAmount, } #[derive(ToSchema, Debug, Clone, Copy, Serialize, Deserialize)] 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.value) } pub(crate) fn from_db(val: i64) -> Self { 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 } } }