about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/storage/migrate/defaults.rs
blob: 3a2019caec298afbe230b43940834eefc679f585 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use crate::{
    app::App,
    storage::sql::{insert::Operations, unit::Unit, unit_property::UnitProperty},
};

#[expect(clippy::unnecessary_wraps, reason = "The API expects an Option<_>")]
fn so(input: &'static str) -> Option<String> {
    Some(s(input))
}
fn s(input: &'static str) -> String {
    String::from(input)
}

macro_rules! register {
    (
        $app:ident # $unit_prop_description:literal,  $unit_prop_name:literal
        $(
            -> $unit_full_name_singular:ident, $unit_full_name_plural:ident, $unit_short_name:ident
        )*
    ) => {
        let mut ops = Operations::new(concat!(
            "create",
            $unit_prop_name,
            "unit property (during provisioning)"
        ));
        let unit_property = UnitProperty::register(
            s($unit_prop_name),
            so($unit_prop_description),
            &mut ops,
        )
        .id;
        ops.apply(&$app).await?;

        let mut ops = Operations::new(concat!(
            "create default units for",
            $unit_prop_name,
            "property (during provisioning)"
        ));

        $(
            Unit::register(
                s(stringify!($unit_full_name_singular)),
                s(stringify!($unit_full_name_plural)),
                s(stringify!($unit_short_name)),
                None,
                unit_property,
                &mut ops,
            );
        )*

        ops.apply(&$app).await?;
    };
}

pub(super) async fn add_defaults_0_to_1(app: &App) -> Result<(), apply_defaults::Error> {
    register!(
        app # "Time mesurement units", "Time"
        -> second, seconds, s
        -> minute, minutes, min
        -> hour, hours, h
    );

    register!(
        app # "Mass (weight) mesurement units", "Mass"
        -> milligram, milligrams, mg
        -> gram, grams, g
        -> kilogram, kilograms, kg
    );

    register!(
        app # "Volume mesurement units", "Volume"
        -> milliliter, millilters, ml
        -> deciliter, deciliters, dl
        -> liter, liters, l

        // English
        -> tablespoon, tablespoons, tbsp
        -> teaspoon, teaspoons, tsp

        // Swedish
        -> tesked, teskedar, tsk
        -> matsked, matskedar, msk
    );

    Ok(())
}

pub(crate) mod apply_defaults {
    use crate::storage::sql::insert::{self, apply};

    #[derive(thiserror::Error, Debug)]
    pub(crate) enum Error {
        #[error("Failed to add new default unit-property the database: {0}")]
        AddUnitProperty(#[from] apply::Error<insert::unit_property::Operation>),

        #[error("Failed to add new default unit the database: {0}")]
        AddUnit(#[from] apply::Error<insert::unit::Operation>),
    }
}