about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/storage/sql/barcode.rs
blob: 239ed8c44ab5eadc4a74ddb9b9753f775b1fb43a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

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);

impl BarcodeId {
    pub(crate) fn to_db(self) -> i64 {
        i64::from(self.0)
    }
    pub(crate) fn from_db(val: i64) -> Self {
        Self(u32::try_from(val).expect("Should be strictly positive"))
    }
}

#[derive(ToSchema, Debug, Clone, Copy, Serialize, Deserialize)]
pub(crate) struct UnitAmount {
    #[schema(format = Int64, minimum = 0)]
    pub(crate) value: u32,
    pub(crate) unit: UnitId,
}