diff options
Diffstat (limited to 'crates/rocie-server/src/storage/migrate/defaults.rs')
| -rw-r--r-- | crates/rocie-server/src/storage/migrate/defaults.rs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/crates/rocie-server/src/storage/migrate/defaults.rs b/crates/rocie-server/src/storage/migrate/defaults.rs new file mode 100644 index 0000000..3a2019c --- /dev/null +++ b/crates/rocie-server/src/storage/migrate/defaults.rs @@ -0,0 +1,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>), + } +} |
