Repository navigation
fix(format): refuse quantities cooklang cannot spell - #755
Conversation
LeMyst
left a comment
There was a problem hiding this comment.
Thanks. Refusing instead of writing text that won't parse back is the right direction, but as written this rejects valid recipes.
1. Every fraction is refused
The comment at crates/format/src/cooklang_source.rs:56 says fractions don't parse under Extensions::empty(). They do: 1/2 and 1 1/2 are core quantity grammar, not an extension. Only ranges need RANGE_VALUES. Because :70-75 returns false for any fraction, these now fail with cannot write a quantity of …, although they round-trip fine on main:
~{1/2%hour}(timers are never scaled, so the fraction survives)#pan{1/2}@pepper{=1/2%tsp}(locked, so it is not scaled to a decimal)
The whole cook recipe -f cooklang command fails for those recipes. The closed #738 got this part right: it used n.value() for any number. Keeping this PR's check-everything-before-writing structure with #738's test for what is writable would work.
2. -0 still gets through
-0.0 >= 0.0 is true, so @flour{0%g} at scale -1 passes the check and is written as {-0%g}, which parses back as text. Writing 0 for negative zero closes that.
3. With -o, the file is emptied before the check runs
The "nothing written first" guarantee only holds inside the writer. write_to_output (src/util/mod.rs:98) calls File::create before the formatter runs. So cook recipe dish.cook:-1 -o dish.cook now empties dish.cook and then errors; before, it at least left a (corrupted) recipe. Combined with point 1, cook recipe dish.cook -o dish.cook on an unscaled recipe with a ~{1/2%hour} timer would wipe it. Running the check before opening the output, or writing to a temp file and renaming, avoids that.
4. Possibly simpler: refuse a negative scale up front
A negative scale is nonsense for every output format: human, JSON and Markdown also print -400 g. Rejecting scale < 0 in cookcli_core::parse_recipe, next to the existing finite-scale check that returns InvalidScale, would fix the root cause. The formatter check could then stay as a backstop for overflow to infinity (e.g. a scale of 1e308).
Smaller points
- Ranges: cookcli never produces one, but
cooklang-formatis published for reuse. A consumer parsing withRANGE_VALUESwould be refused output that round-trips for them. Only refusing negative or non-finite endpoints would keep that working. - Inline quantities (
:68): these are written as plain text anyway, so checking them adds nothing. - Tests: they only build
-400,infandNaNby hand. A test that fractions and locked fractions still format would have caught point 1.
This PR merges cleanly with #735, #736 and #737. #737 makes @egg{=1/2%cup} round-trip, which this PR currently refuses, so the two need to agree.
🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.
cdabec7 to
9964d9b
Compare
|
Reworked per review: a negative scale is now refused up front in |
|
Thanks for the rework. I checked it by running the cases from the first review. Fractions in timers, cookware and locked ingredients format again.
Optional: a scale of This now conflicts with #737 in the quantity-writing hunk of 🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst. |
9964d9b to
d1d5ab2
Compare
|
All three on d1d5ab2:
Noted on the #737 conflict — as you say, keep the |
d1d5ab2 to
ec23c44
Compare
A negative scaling factor left quantities like -400 g that every format
prints literally, and cooklang output {-400%g} reparses as text —
silently unscalable. Reject a negative scale in parse_recipe up front,
next to the existing finite-scale check.
As a backstop the cooklang formatter still refuses, before writing any
bytes, a quantity whose value is negative or non-finite; -0.0 is written
as 0 so it does not reparse as text either. Fractions and non-negative
ranges are valid grammar and keep formatting. -o output is now rendered
to a buffer first so a formatter error cannot truncate the target file.
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
ec23c44 to
5a067c2
Compare
|
Thanks, all three points are fixed. I reran the earlier cases on 5a067c2:
This looks good to merge. One optional tidy-up: since #736, numbers are written by 🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst. |
|
Thanks — noted on the tidy-up: the |
Summary
cook recipe dish.cook:-1scaled quantities negative, and{−400%g}reads back as text — a recipe rewritten through--format cooklangsilently lost scalability.parse_recipenow refuses a negative scale up front, next to the existing finite check (InvalidScale).print_cooklangstill refuses — before writing a byte — a quantity whose value is negative or non-finite (io::ErrorKind::InvalidData). Fractions (core grammar) and non-negative ranges (forRANGE_VALUESconsumers) write normally;-0.0is emitted as0so it does not reparse as text either.-ooutput is rendered to a buffer before the file is opened, so a formatter error can no longer truncate the target.Test plan
cook recipe "dish.cook:-1" -f cooklang→Error: scale factor must be a finite non-negative number, but was -1an_unwritable_quantity_is_refused_not_written_as_text— hand-planted-400,inf,NaNrefused before any bytefraction_quantities_still_format—{1/2%cup},#pan{1/2},~{1/2%hour},{=1 1/2%ball}all round-tripnegative_zero_is_written_as_zero;zero_scale_is_accepted_but_negative_is_rejectedCloses #722