first commit

This commit is contained in:
Romulus21 committed 2026-10-09 12:33:27 +02:00
commit 9536cf0dbe
16 files changed
+5170

No files matched your search

+243
View File
@@ -0,0 +1,243 @@
use serde::{Deserialize, Serialize};
/// Generates `as_str` / `from_str` / `ALL` for a fieldless enum stored as TEXT.
macro_rules! text_enum {
($name:ident { $($variant:ident => $text:literal),+ $(,)? }) => {
impl $name {
pub const ALL: &'static [$name] = &[$($name::$variant),+];
pub fn as_str(self) -> &'static str {
match self {
$($name::$variant => $text),+
}
}
pub fn parse(s: &str) -> Option<Self> {
match s {
$($text => Some($name::$variant),)+
_ => None,
}
}
}
};
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ShoppingList {
pub id: i64,
pub name: String,
pub color: ListColor,
pub created_at: String,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ListColor {
Lime,
Pink,
Sky,
Peach,
}
text_enum!(ListColor {
Lime => "lime",
Pink => "pink",
Sky => "sky",
Peach => "peach",
});
impl ListColor {
pub fn dot_class(self) -> &'static str {
match self {
ListColor::Lime => "bg-lime",
ListColor::Pink => "bg-pink",
ListColor::Sky => "bg-sky",
ListColor::Peach => "bg-peach",
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Item {
pub id: i64,
pub list_id: i64,
pub name: String,
pub quantity: Option<Quantity>,
pub icon: Icon,
pub category: Category,
pub checked: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct Quantity {
pub amount: f64,
pub unit: Unit,
}
impl Quantity {
/// Short label for the tile badge: "×3", "500 g", "1,5 L".
pub fn label(&self) -> String {
let amount = if self.amount.fract() == 0.0 {
format!("{}", self.amount as i64)
} else {
format!("{}", self.amount).replace('.', ",")
};
match self.unit {
Unit::Piece => format!("×{amount}"),
unit => format!("{amount} {}", unit.label()),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Unit {
Piece,
Gram,
Kilogram,
Millilitre,
Litre,
}
text_enum!(Unit {
Piece => "pc",
Gram => "g",
Kilogram => "kg",
Millilitre => "ml",
Litre => "l",
});
impl Unit {
pub fn label(self) -> &'static str {
match self {
Unit::Piece => "pièce(s)",
Unit::Gram => "g",
Unit::Kilogram => "kg",
Unit::Millilitre => "mL",
Unit::Litre => "L",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Category {
FruitsLegumes,
Frais,
Epicerie,
Viande,
Boulangerie,
Autre,
}
text_enum!(Category {
FruitsLegumes => "fruits_legumes",
Frais => "frais",
Epicerie => "epicerie",
Viande => "viande",
Boulangerie => "boulangerie",
Autre => "autre",
});
impl Category {
pub fn label(self) -> &'static str {
match self {
Category::FruitsLegumes => "Fruits & légumes",
Category::Frais => "Frais",
Category::Epicerie => "Épicerie",
Category::Viande => "Viande & poisson",
Category::Boulangerie => "Boulangerie",
Category::Autre => "Autre",
}
}
/// Tailwind background class for the item tile.
pub fn tile_class(self) -> &'static str {
match self {
Category::FruitsLegumes => "bg-lime",
Category::Frais => "bg-sky",
Category::Epicerie => "bg-peach",
Category::Viande => "bg-rose",
Category::Boulangerie => "bg-sand",
Category::Autre => "bg-lilac",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Icon {
Apple,
Banana,
Carrot,
Lemon,
Glass,
Milk,
IceCream,
Cheese,
Egg,
Coffee,
Bread,
Croissant,
Pasta,
Meat,
Fish,
Bottle,
Basket,
}
text_enum!(Icon {
Apple => "apple",
Banana => "banana",
Carrot => "carrot",
Lemon => "lemon",
Glass => "glass",
Milk => "milk",
IceCream => "ice_cream",
Cheese => "cheese",
Egg => "egg",
Coffee => "coffee",
Bread => "bread",
Croissant => "croissant",
Pasta => "pasta",
Meat => "meat",
Fish => "fish",
Bottle => "bottle",
Basket => "basket",
});
/// A frequently bought product, offered as a dashed "+ Beurre" tile.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Suggestion {
pub name: String,
pub icon: Icon,
pub category: Category,
}
/// Payload for creating or editing an item.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ItemInput {
pub name: String,
pub quantity: Option<Quantity>,
pub icon: Icon,
pub category: Category,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn quantity_labels() {
let q = |amount, unit| Quantity { amount, unit }.label();
assert_eq!(q(3.0, Unit::Piece), "×3");
assert_eq!(q(500.0, Unit::Gram), "500 g");
assert_eq!(q(1.5, Unit::Litre), "1,5 L");
}
#[test]
fn text_roundtrip() {
for icon in Icon::ALL {
assert_eq!(Icon::parse(icon.as_str()), Some(*icon));
}
for cat in Category::ALL {
assert_eq!(Category::parse(cat.as_str()), Some(*cat));
}
}
}