about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/storage/sql/barcode.rs
blob: c16ac3c24f293d60a7ce6c2007a6820781955de6 (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
44
45
46
47
48
49
50
51
52
53
// rocie - An enterprise grocery management system
//
// Copyright (C) 2026 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Rocie.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

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 }
    }
}