Les catégories suivent l'ordre de l'enum (parcours du magasin) ; les noms sont comparés sans tenir compte de la casse ni des accents. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
337 lines
8.6 KiB
Rust
337 lines
8.6 KiB
Rust
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,
|
||
/// False when the list was shared with the current user.
|
||
pub is_owner: bool,
|
||
pub owner_name: String,
|
||
}
|
||
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
pub struct User {
|
||
pub id: i64,
|
||
pub name: String,
|
||
pub email: String,
|
||
}
|
||
|
||
impl User {
|
||
/// "Camille Lefèvre" → "CL", "romain" → "RO".
|
||
pub fn initials(&self) -> String {
|
||
let words: Vec<&str> = self.name.split_whitespace().collect();
|
||
let initials: String = match words.as_slice() {
|
||
[first, second, ..] => first.chars().take(1).chain(second.chars().take(1)).collect(),
|
||
[single] => single.chars().take(2).collect(),
|
||
[] => String::new(),
|
||
};
|
||
initials.to_uppercase()
|
||
}
|
||
}
|
||
|
||
/// Someone a list is shared with.
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||
pub struct Member {
|
||
pub user_id: i64,
|
||
pub name: String,
|
||
pub email: 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,
|
||
}
|
||
|
||
impl Item {
|
||
/// Display order: by category (aisle order of `Category`), then by name.
|
||
pub fn sort_key(&self) -> (Category, String) {
|
||
(self.category, fold_name(&self.name))
|
||
}
|
||
}
|
||
|
||
/// Lowercases and strips French accents so "Œufs" sorts with "o" and
|
||
/// "Épinards" with "e".
|
||
fn fold_name(name: &str) -> String {
|
||
let mut folded = String::with_capacity(name.len());
|
||
for c in name.chars().flat_map(char::to_lowercase) {
|
||
match c {
|
||
'à' | 'â' | 'ä' => folded.push('a'),
|
||
'é' | 'è' | 'ê' | 'ë' => folded.push('e'),
|
||
'î' | 'ï' => folded.push('i'),
|
||
'ô' | 'ö' => folded.push('o'),
|
||
'ù' | 'û' | 'ü' => folded.push('u'),
|
||
'ÿ' => folded.push('y'),
|
||
'ç' => folded.push('c'),
|
||
'œ' => folded.push_str("oe"),
|
||
'æ' => folded.push_str("ae"),
|
||
_ => folded.push(c),
|
||
}
|
||
}
|
||
folded
|
||
}
|
||
|
||
#[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",
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Variant order is the display order of items.
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, 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 initials() {
|
||
let user = |name: &str| User { id: 1, name: name.into(), email: String::new() };
|
||
assert_eq!(user("Camille Lefèvre").initials(), "CL");
|
||
assert_eq!(user("romain").initials(), "RO");
|
||
assert_eq!(user("élise").initials(), "ÉL");
|
||
}
|
||
|
||
#[test]
|
||
fn items_sort_by_category_then_name() {
|
||
let item = |name: &str, category| Item {
|
||
id: 0,
|
||
list_id: 0,
|
||
name: name.into(),
|
||
quantity: None,
|
||
icon: Icon::ALL[0],
|
||
category,
|
||
checked: false,
|
||
};
|
||
let mut items = vec![
|
||
item("Pain", Category::Boulangerie),
|
||
item("Yaourts", Category::Frais),
|
||
item("Œufs", Category::Frais),
|
||
item("beurre", Category::Frais),
|
||
item("Épinards", Category::FruitsLegumes),
|
||
item("Citrons", Category::FruitsLegumes),
|
||
item("Olives", Category::Frais),
|
||
];
|
||
items.sort_by_key(Item::sort_key);
|
||
let names: Vec<&str> = items.iter().map(|i| i.name.as_str()).collect();
|
||
assert_eq!(names, ["Citrons", "Épinards", "beurre", "Œufs", "Olives", "Yaourts", "Pain"]);
|
||
}
|
||
|
||
#[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));
|
||
}
|
||
}
|
||
}
|