Skip to content

fix(format): refuse quantities cooklang cannot spell - #755

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/722-negative-quantity-roundtrip
Oct 17, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/722-negative-quantity-roundtrip

Conversation

@drawmeanelephant

@drawmeanelephant drawmeanelephant commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • cook recipe dish.cook:-1 scaled quantities negative, and {−400%g} reads back as text — a recipe rewritten through --format cooklang silently lost scalability. parse_recipe now refuses a negative scale up front, next to the existing finite check (InvalidScale).
  • As a backstop, print_cooklang still refuses — before writing a byte — a quantity whose value is negative or non-finite (io::ErrorKind::InvalidData). Fractions (core grammar) and non-negative ranges (for RANGE_VALUES consumers) write normally; -0.0 is emitted as 0 so it does not reparse as text either.
  • -o output 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 -1
  • an_unwritable_quantity_is_refused_not_written_as_text — hand-planted -400, inf, NaN refused before any byte
  • fraction_quantities_still_format — {1/2%cup}, #pan{1/2}, ~{1/2%hour}, {=1 1/2%ball} all round-trip
  • negative_zero_is_written_as_zero; zero_scale_is_accepted_but_negative_is_rejected

Closes #722

@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. 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-format is published for reuse. A consumer parsing with RANGE_VALUES would 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, inf and NaN by 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.

@drawmeanelephant
drawmeanelephant force-pushed the fix/722-negative-quantity-roundtrip branch from cdabec7 to 9964d9b Compare October 13, 2026 20:46
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Reworked per review: a negative scale is now refused up front in parse_recipe next to the finite check. The formatter keeps a before-anything-is-written refusal only for negative/non-finite values — fractions (core grammar) and non-negative ranges (RANGE_VALUES consumers) write normally, -0.0 is emitted as 0, and -o output is rendered to a buffer first so a formatter error can no longer truncate the target file. Inline quantities are out of the preflight since they are plain text anyway.

@LeMyst

LeMyst commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the rework. I checked it by running the cases from the first review. Fractions in timers, cookware and locked ingredients format again. -0 is written as 0. A negative scale is refused for every output format and for shopping-list. A failed -o leaves the file byte-for-byte untouched. Two small things are left:

  1. crates/format/src/cooklang_source.rs:62-65 checks value.is_finite(), but cooklang's Number Display rounds with (n * 1000.0).round() / 1000.0, which overflows for values above about 1.8e305. So cook recipe X.cook:1e308 -f cooklang on a recipe with @salt{1%g} still writes @salt{inf%g}, which reads back as text. Checking (value * 1000.0).is_finite() as well, or checking the text that will be written, closes that.
  2. crates/core/src/recipe.rs:89 still says InvalidScale is returned "if the scale is not finite". It now also covers a negative scale.

Optional: a scale of -0 still gets through (-0.0 < 0.0 is false), so cook recipe X.cook:-0 and cook shopping-list X.cook:-0 print -0 g. Adding scale.is_sign_negative() to the check would tidy that up.

This now conflicts with #737 in the quantity-writing hunk of ComponentFormatter. It is easy to resolve: keep #737's = lock block, then this PR's -0 match. They no longer disagree on behaviour.


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

@drawmeanelephant
drawmeanelephant force-pushed the fix/722-negative-quantity-roundtrip branch from 9964d9b to d1d5ab2 Compare October 15, 2026 22:42
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

All three on d1d5ab2:

  • 1e308 overflow: writable now also checks (value * 1000.0).is_finite(), matching what Number's Display actually does — @salt{1%g} at scale 1e308 is refused instead of writing inf%g. Added to the refused table.
  • InvalidScale doc at recipe.rs: now says negative scales are covered, -0 included.
  • -0 scale: the parse check is now !scale.is_finite() || scale.is_sign_negative(), so cook recipe X.cook:-0 and cook shopping-list X.cook:-0 are refused up front instead of printing -0 g. Test added.

Noted on the #737 conflict — as you say, keep the = lock block then this PR's -0 match.

@drawmeanelephant
drawmeanelephant force-pushed the fix/722-negative-quantity-roundtrip branch from d1d5ab2 to ec23c44 Compare October 17, 2026 12:58
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>
@drawmeanelephant
drawmeanelephant force-pushed the fix/722-negative-quantity-roundtrip branch from ec23c44 to 5a067c2 Compare October 17, 2026 13:01
@LeMyst

LeMyst commented Oct 17, 2026

Copy link
Copy Markdown
Collaborator

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 shortest(), not by Number's Display. So the comment at crates/format/src/cooklang_source.rs:64-66 and the "quantity of inf g" in the error message describe the old writer. The -0 arm at :398 also duplicates what shortest() already does.


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

@LeMyst
LeMyst merged commit 7366e50 into cooklang:main Oct 17, 2026
5 checks passed
@drawmeanelephant

drawmeanelephant commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks — noted on the tidy-up: the shortest() writer makes the comment and the -0 arm stale. Since this is merged I will fold that cleanup into the next formatter-touching PR rather than open one for a comment and a dead branch.

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.

Negative quantities round-trip through cooklang format as non-scalable text

2 participants