about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/storage/sql/barcode.rs
blob: f15d399714ef5c66357acea6b2c87ecdbde57a43 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
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<u32> for BarcodeIdStub {
    fn from(value: u32) -> Self {
        Self { value }
    }
}
impl From<BarcodeIdStub> for BarcodeId {
    fn from(value: BarcodeIdStub) -> Self {
        Self { value: value.value }
    }
}