Skip to content

fix(shopping-list): merge case-variant ingredients into one item - #747

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/694-case-variant-ingredients
Oct 17, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/694-case-variant-ingredients

Conversation

@drawmeanelephant

@drawmeanelephant drawmeanelephant commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #694

Summary

sugar and Sugar travelled the whole pipeline as two ingredients, but everything downstream matched case-insensitively — upstream's categorize showed only the last variant, and the pantry's stock was subtracted from each variant (sugar 100 g + Sugar 200 g with 100 g stocked showed 100 g to buy instead of 200 g). The server's checked set ticked both variants too.

merge_case_variants in cookcli_core::shopping_list now folds case variants into one entry under the first spelling seen, summing quantities with add_ingredient. It runs in generate (before use_common_names and the pantry subtraction) and in the server's merged(), so the CLI list, POST /api/shopping_list and the pantry all see one correct total — which #694 allows ("or their sum").

Test plan

  • Add @sugar{1%cup} and @Sugar{2%cup}. lists one sugar 3 cup in both flat and categorised views.
  • @sugar{100%g} + @Sugar{200%g} with pantry sugar = "100%g" leaves 200 g, not 100 g — covered in core (merged_case_variants_subtract_the_pantry_once) and end-to-end via POST /api/shopping_list (case_variants_merge_and_share_the_pantry_once).

Generated with Devin

@LeMyst LeMyst left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This fixes the CLI's categorised output, but two parts of #694 remain.

1. The web UI isn't fixed

src/server/handlers/shopping_list.rs:165 still calls final_list.categorize(&aisle), and this PR doesn't touch that file. So POST /api/shopping_list still returns only Sugar, which doesn't meet the issue's "the web UI answer contains both names". The new categorize (crates/core/src/shopping_list/mod.rs:351) is private, so the server can't reuse it.

2. Keeping the variants separate under-buys when the pantry has stock

Pantry lookup is case-insensitive (PantryConf::find_ingredient lowercases), so stock is subtracted from each variant. With @sugar{100%g} @Sugar{200%g} and pantry sugar = "100%g", the list shows Sugar 100 g; the right answer is 200 g. This predates the PR (the flat items view already did it), but the PR leaves it in place. Aisle lookup and the server's checked set (store.rs:377) are case-insensitive too, so ticking sugar also ticks Sugar.

Suggestion

Merge case variants into one entry under the first spelling seen, before use_common_names and the pantry step, in generate and in the server's merged() (shopping_list.rs:755). That fixes the CLI, the web UI and the pantry problem in one place, and #694 explicitly allows "or their sum". The root cause is cooklang-rs's IngredientList::categorize, which could also get an upstream issue.

Minor points

  • The doc comment on build (mod.rs:289) still points to IngredientList::categorize.
  • The "other" bucket now orders items sugar, Sugar, flour, while items has sugar, flour, Sugar.
  • Non-ASCII: É/é stay apart and ß/SS don't fold, which matches aisle and pantry lookup, so that's fine.
  • Tests: there is one core test, but none for the server or the pantry case.

This merges cleanly with #752, but needs a rebase after it if the merging moves into generate.


🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

@drawmeanelephant
drawmeanelephant force-pushed the fix/694-case-variant-ingredients branch from 02e365c to bafa8c7 Compare October 13, 2026 20:50
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Reworked per review: case variants now merge into one entry under the first spelling seen in generate and the server's merged(), before use_common_names and the pantry subtraction. That fixes the web UI (POST /api/shopping_list returns one sugar item) and the pantry double-subtraction (sugar 100 g + Sugar 200 g with 100 g stocked now leaves 200 g). Custom categorize is gone — upstream's works once variants are merged. Tests cover the merge, the pantry case in core, and the API path end-to-end.

@drawmeanelephant drawmeanelephant changed the title fix(shopping-list): keep case-variant ingredients in categorisation fix(shopping-list): merge case-variant ingredients into one item Oct 14, 2026
@LeMyst

LeMyst commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the rework. I checked it against the PR's binary. @sugar{100%g} + @Sugar{200%g} with sugar, Sugar or SUGAR = "100%g" in the pantry now gives one sugar 200 g, in every CLI format and from POST /api/shopping_list. Aisle categories, synonyms, mixed and incompatible units, and non-ASCII names (Crème/CRÈME, ß) behave as expected. fmt, clippy, the tests and CI are green, so this looks good to merge.

One small optional addition: docs/shopping-list.md:99, docs/api.md:502 and the generate doc comment (crates/core/src/shopping_list/mod.rs:379) still say "Ingredients with the same name are combined". A few words saying that letter case is ignored and the first spelling is kept would document the new rule.


🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

@drawmeanelephant
drawmeanelephant force-pushed the fix/694-case-variant-ingredients branch from bafa8c7 to 881c966 Compare October 15, 2026 22:44
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Done on 881c966 — all three spots now say the merge ignores letter case and keeps the first spelling: docs/shopping-list.md (with a @sugar + @Sugar example), the generate doc comment, and docs/api.md via the regenerated api_docs.rs text.

sugar and Sugar travelled the pipeline as two ingredients, but the
pantry, the checked set and the aisle all match case-insensitively — so
upstream's categorize showed only the last variant, and each variant was
charged the same pantry stock. Merge them in generate and the server's
merged() under the first spelling seen, before use_common_names and the
pantry subtraction, so the CLI, the web list and the pantry all see one
correct total.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@drawmeanelephant
drawmeanelephant force-pushed the fix/694-case-variant-ingredients branch from 881c966 to a924455 Compare October 15, 2026 23:42
@LeMyst

LeMyst commented Oct 17, 2026

Copy link
Copy Markdown
Collaborator

Re-checked a924455 after the rebase onto #593's optional ingredients. Merging case variants separately for the required and the optional list, before the pantry step, is the right way round: subtract_pantry_from_optional already compares names case-insensitively, so the two lists still line up.

I ran each case from the CLI and through POST /api/shopping_list, with sugar = { quantity = "100%g" } in the pantry:

Case Result
@sugar{100%g} + @Sugar{200%g} one sugar 200 g (300 g with --ignore-pantry), also in JSON
@Sugar{50%g} recipe listed first, then the one above one Sugar 250 g: first spelling kept, pantry subtracted once
required @sugar{100%g} + optional @?Sugar{50%g}, optional chosen required fully covered by the pantry; optional Sugar 50 g kept whole, as on main
@?sugar{20%g} + @?SUGAR{30%g}, both chosen merged to 50 g and covered by the pantry, so nothing to buy

fmt, clippy and cargo test --workspace (1755 passed) are clean locally. CI is green, and the PR still merges cleanly with today's main. Looks good to merge.

One cosmetic thing, already on main and not from this PR: pantry_items skips duplicates case-sensitively (src/server/handlers/shopping_list.rs:151). So the required/optional case above returns ["sugar", "Sugar"].


🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Thanks for the re-check — good to hear the required/optional split lines up with subtract_pantry_from_optional's own case-insensitive compare.

On pantry_items: right, that's pre-existing — it contains-matches the raw strings, so after the merge a sugar + Sugar pair reports ["sugar", "Sugar"] as covered. It's a two-line fix (eq_ignore_ascii_case on the dedup) and a small test — happy to put it in a quick follow-up PR rather than grow this one post-approval, unless you'd rather have it folded in here.

@LeMyst
LeMyst merged commit ab74ace into cooklang:main Oct 17, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shopping list silently drops ingredients whose names differ only by case

2 participants