fix(pantry): convert compatible units before subtracting from the shopping list - #104
Open
gillesdami wants to merge 1 commit into
Open
fix(pantry): convert compatible units before subtracting from the shopping list#104gillesdami wants to merge 1 commit into
gillesdami wants to merge 1 commit into
Conversation
…pping list
A recipe quantity is fitted to its own magnitude before it reaches
`subtract_pantry`, so `@semolina{1500%g}` arrives as 1.5 kg while the pantry
still says `2000%g`. The two were compared as strings, so the pantry was skipped
and the full amount stayed on the shopping list.
Writing the pantry in the larger unit does not help, because the fitting works
both ways: `0.25%kg` arrives as 250 g and misses a pantry kept in kg. An
ingredient used both above and below 1000 g/ml could therefore not be tracked at
all — a couscous needing 1.5 kg of semolina and a tabbouleh needing 250 g have no
pantry unit in common.
Convert the pantry quantity into the unit the recipe was parsed with, using the
converter already in scope, and keep the warning for units that measure
different things: 500 g of oil against a litre on the shelf is still a mismatch,
since there is no density to convert with.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
An ingredient in the pantry is skipped entirely when its recipe quantity is
reported in a different unit from the one the pantry is written in, even when the
two units are the same kind of measure. The full amount stays on the shopping
list and only a
tracing::warn!on stderr says why — the HTTP API and the web UIshow a list that is quietly too long.
This is not an edge case the author can avoid, because the unit in the file is
not the unit that reaches the pantry: a quantity is fitted to its own magnitude
first.
Writing the pantry in the larger unit does not fix it, because the fitting works
in both directions. So an ingredient used both above and below 1000 g/ml — a
couscous needing 1.5 kg of semolina and a tabbouleh needing 250 g of the same
semolina — has no pantry unit that works for both, and cannot be tracked at
all.
Reproduce
2.1 kg of semolina to buy with 2 kg already in the cupboard. Aggregation across
recipes is perfect (1500 g + 600 g = 2.1 kg); it is only the pantry that is
dropped.
POST /api/shopping_listbehaves the same way, without even the warning.The fix
IngredientList::subtract_pantrycomparedreq_unit == pantry_unitas strings.It now converts the pantry quantity into the unit the recipe was parsed with,
using the
Converterthat is already in scope on the next line, and fallsthrough to the existing mismatch branch only when the conversion fails.
Units that measure different things are still a mismatch. 500 g of oil
against
oil = "1%l"in the pantry keeps the full 500 g and still warns, becausethere is no density to convert with — the existing
test_subtract_pantry_unit_mismatchcovers exactly that and passes unchanged.Tests
Two added next to the existing ones in
ingredient_list::pantry_tests:test_subtract_pantry_converts_compatible_units— 1500 g needed against1%kgon the shelf leaves 0.5 kg, and 200 ml against1%lleaves nothing.test_subtract_pantry_when_the_pantry_holds_the_smaller_unit— the direction ahousehold actually writes: grams on the shelf, a recipe big enough to be
reported in kilograms.
cargo test --all-featurespasses excepttests/serde.rs, which failsidentically on an unmodified checkout of
main(Modifiers(REF)vsModifiers(0x0)) — pre-existing and unrelated.cargo fmt --checkandcargo clippyare clean for the touched file; the diffs they report inbindings/are pre-existing onmaintoo.Also verified against a real 33-recipe collection: the patched and unpatched
shopping lists are byte-identical there, since nothing in it crossed a unit
boundary. Only the broken cases change.
Note
src/pantry.rs:160comparescurrent_unit == threshold_unitthe same way, so apantry with
quantity = "2%kg"andlow = "500%g"will not report as depleted.Left alone to keep this change to one thing; happy to follow up.
Disclosure
This patch was written with AI assistance (Claude Code). It is not a speculative
cleanup: I hit this bug in my own kitchen collection through
CookCLI, on a couscous recipe needing
1.5 kg of semolina while 2 kg sat in the pantry. Every claim above is measured
against the shipped
ghcr.io/cooklang/cookcliimage and against a local build ofthis branch, and I have read and understood every line of the change.