Repository navigation
fix(pantry): refuse quantities and dates the file cannot read back - #753
Conversation
6fae444 to
4c9f938
Compare
LeMyst
left a comment
There was a problem hiding this comment.
Thanks. Validating dates here is good: the check uses the same parse_date as expiring and days_until_expiry, so the accepted formats match exactly. The quantity check has two problems, though.
1. unlim / unlimited are now refused
These are documented, valid values:
- they appear in
seed/config/pantry.conf(lines 1, 11-13); docs/api.md(around lines 873 and 913) describes the field as "any other string such asunlim";- cooklang's pantry subtraction and the server (
src/server/handlers/shopping_list.rs:142) treat them specially.
With this PR, adding an item with quantity unlim returns "the quantity 'unlim' is not a quantity". CI stays green only because the server test that uses " unlim " (src/server/handlers/pantry.rs:587) goes through trimmed(), not through core.
2. The check doesn't match what reads the value
The PR validates against core's QUANTITY regex (crates/core/src/pantry/mod.rs:1331). Its unit group is (.*), so it accepts any string that starts with a digit. But the shopping list and is_low read quantities through cooklang's parse_quantity, which replaces % with a space and runs f64::parse on the first word.
| Value | Accepted here | Readable by parse_quantity |
|---|---|---|
500g |
yes | no: never subtracted (pantry flour = "500g" leaves flour 600 g on the list) |
1/2%cup |
yes | no |
1,5%kg |
yes | no |
.5%kg |
no | yes |
So the kind of value #710 is about can still be stored, and some readable values are refused.
Suggestion: validate with cooklang's own parsing. Build a PantryItem and call parsed_quantity(), as PantryItem::is_low does around mod.rs:129, and let unlim / unlimited through. Then update docs/api.md and the --quantity help in src/pantry.rs (lines 171, 183, 205) to match. If the free-form string promised by the docs should stay, the softer alternative is to store the value and return a warning in the outcome's diagnostics.
Tests
The tests only cover refusals. Cases that must be accepted, such as unlim, 2, 500%g, 500 g and each date format, would catch over-strictness like point 1.
Minor: update now trims its inputs, so --quantity " " becomes "no attributes given". That seems fine.
This PR merges cleanly on top of #748.
🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.
4c9f938 to
766b1e7
Compare
|
Reworked the validation to use the reader itself rather than a parallel regex: the candidate value now goes through |
766b1e7 to
6e3042d
Compare
|
Thanks, this is much better.
The macOS CI failure ( 🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst. |
6e3042d to
a2dbd51
Compare
|
Done on a2dbd51, all four points:
Tests: refusal table now covers |
|
Thanks, this addresses all four points. Checked on a2dbd51:
A few small things are left:
For the record, a pantry file edited by hand with fmt, clippy and 🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst. |
a2dbd51 to
b10f6e7
Compare
`add` and `update` wrote whatever a request carried: `--quantity garbage` landed as `milk = "garbage"`, and a `32/13/2025` expiry the same way. Nothing can read them back — the quantity is never subtracted from a shopping list nor judged against `low`, the date never expires — so they were junk the next read quietly ignored. The attributes a request sets are now checked before the document is opened: `quantity` and `low` against the pattern the file parses them with, `bought` and `expire` against the date spellings `parse_date` accepts. `update` also trims its attributes and drops blanks, the way `add`'s request always has. Nothing is written on a refusal, and the server's pantry writes are covered by the same check since they go through core. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
b10f6e7 to
c464f31
Compare
|
All four on c464f31:
On the |
|
Thanks! Points 1, 2 and 4 are fixed. Checked on c464f31:
One thing is left, on point 3:
Nit: the comment at fmt, clippy and 🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst. |
`load()` warned about a `quantity`/`low` nothing can read back, but the
warning went nowhere: `list`, `depleted`, `expiring`, `recipes` and
`plan` all ended in `into_value()`, and the pantry page kept only
`outcome.value`. A `butter = "0g"` still looked silently stocked.
The read commands now run their outcome through `log_diagnostics`, as
`add`/`remove`/`update` already did, and the pantry page warns them to
the server log. The message quotes the value with `{value:?}` so a
hand-edited newline cannot forge a second warning line. The seed
pantry's `quantity = "some"` would warn every seed user twice, so the
fennel seeds and cinnamon get readable amounts.
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
Done on 50c316d:
|
|
Thanks, point 3 is closed. Checked on 50c316d with
( Two small things, neither blocking:
fmt, clippy and 🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst. |
Closes #710
Summary
cook pantry add dairy milk --quantity garbagestoredmilk = "garbage"and exited 0;--expire 32/13/2025the same, throughupdatetoo. Nothing downstream can read those values: a quantityPantryItem::parsed_quantitycannot parse is never subtracted from ashopping list nor compared to
low, and a dateparse_datedoes notaccept never expires — so they sat in the file as junk every later read
quietly ignored.
Changes
Both in
cookcli-core(pantry::add/pantry::update), so theserver's pantry writes — which go through core — get the same check:
check_attribute_valuesruns before the document is opened.quantityandloware validated by the reader itself: the candidatevalue is handed to
cooklang'sPantryItem::parsed_quantity(), sowhat is accepted is exactly what the file reads back —
500%g,500 g,.5%kg,2— andunlim/unlimitedare allowed as thedocumented "stocked, not measurable" spellings the seed pantry ships
and the server treats specially.
boughtandexpiremust parse asa date in one of the spellings
parse_dateaccepts.updatenow normalizes its attributes the wayadd's request alwayshas — trimmed, blank meaning unset — so
--quantity " 500%g "stores500%grather than a valueunit_ofcould never match.the expire date '32/13/2025' is not a date a pantry file can read back: write it like '2027-01-01'; nothing is written.docs/api.mdand the--quantityhelp now describe the validatedcontract (
VALUE%UNIT, a plain count, orunlim/unlimited).Test plan
add_refuses_an_attribute_nothing_could_read_backcovers all fourattributes and pins the file staying byte-identical.
update_refuses_an_attribute_nothing_could_read_backdoes the samefor update.
add_accepts_the_quantities_and_dates_readers_useacceptsunlim,unlimited,2,500%g,500 g,.5%kgand everyparse_dateformat.
add_accepts_unlimited_as_a_low_thresholdcoverslow.cook pantry add dairy milk --quantity garbage→ exit 1,file unchanged;
--quantity "2%l" --expire 2025-12-01still writesnormally.
cargo test --all-featuresgreen.Generated with Devin