Skip to content

Range quantities: scale, add up and time {2-3%cloves} and ~{25-30%minutes} as numbers (spec 0011) #862

Backlog

Description

@dubadub

What

Support range quantities as numbers everywhere in CookCLI: @garlic{2-3%cloves}, @chilli flakes{1/2-1%tsp}, ~{25-30%minutes}. Ranges are now part of the Cooklang spec (proposal 0011, accepted). The en dash and em dash also work as separators ({2–3}).

Tracks #597 (ranges stay unchanged when a recipe is scaled). Close #597 when this lands.

Why

The shared PARSER (crates/core/src/parser.rs:20-21) uses Extensions::empty(), so today {2-3%cloves} parses as Text("2-3"). Ranges don't scale, don't add up on shopping lists and get no countdown in the web UI. The language server uses Extensions::all(), so the editor and the rest of CookCLI disagree.

The cooklang-rs change for this (cooklang/cooklang-rs#169, released in 0.23.0) parses ranges with no extensions. RANGE_VALUES becomes a documented no-op. The same change:

  • reads ranges on .shopping-list free-hand items and ? name{…} selection lines, and writes them back as 1-2%unit;
  • warns on {3-2}, and treats a zero denominator at either end ({1-1/0}) as an error.

Most of CookCLI is already ready: the formatters, serde output, recipe.scale(...) and upstream try_add all handle Value::Range.

What changes on its own after the bump

From reading the code; not run.

  • Scaling: recipe read, the web recipe page, shopping lists and reports all call recipe.scale(...), which scales both ends.
  • JSON/YAML: a range serialises as {"type":"range","value":{"start":…,"end":…}}, distinct from text.
  • -f cooklang: already writes start-end (crates/format/src/cooklang_source.rs:321-330).
  • Shopping list totals: through IngredientList (range + range end to end, number + range on both ends).
  • Web ingredient lists: built server-side through GroupedQuantity::merge (src/web/builders.rs). They inherit range support.

Work

  1. Bump cooklang-rs to the release with ranges in core. feat: range quantities scale and add up (cooklang 0.23.0, spec 0011) #865 (cooklang 0.23.0).
  2. doctor: stop adding Extensions::RANGE_VALUES in textual_timer_warnings, and drop the comments that explain it. feat: range quantities scale and add up (cooklang 0.23.0, spec 0011) #865. docs/doctor.md was still accurate. The parser's reversed-range warning ({3-2}) already shows in cook doctor validate, with its hint.
  3. Display:
    • crates/format/src/number.rs:206-216 format_value renders a - b. The spec says to show the unit once (2–3 cloves), and an en dash is allowed for display.
    • Pick one separator for human, Markdown, LaTeX, Typst and the web (templates/shopping_list.html:644-661 formatCooklangValue).
    • Writers (-f cooklang, .shopping-list) keep the plain -.
  4. Web timers:
    • src/web/builders.rs:355-366 timer_duration_seconds returns None for anything but Number, so a range timer gets no data-seconds and no countdown (templates/recipe.html:405, static/js/timers.js:244).
    • The spec says to show the range and default the countdown to the lower end. Offering the upper end (e.g. "+5 min") is optional.
  5. schema.org: crates/format/src/schema.rs:352-361 averages a range for timeRequired. Decide between the lower end and keeping the average, and fix the comment that says ranges only come from scaling.
  6. Recipe references with a range amount: @./sauce{1-2} is refused as NotANumber (crates/core/src/shopping_list/mod.rs:1162-1184, src/util/recipe_info.rs:98). Decide what it means (lower end? error?).
  7. Menus: src/util/menu.rs:253-282 scaled_quantity re-parses through PARSER, so menu ranges start scaling after the bump. Check the menu page and cooking mode.
  8. .shopping-list round trip (web server):
    • New selection lines are written with upstream Display (optional_selections, crates/core/src/shopping_list/mod.rs:912-918), which rounds each end to 3 decimals. Since feat: range quantities scale and add up (cooklang 0.23.0, spec 0011) #865, @?chilli flakes{1/3-1/2%tsp} is recorded as 0.333-0.5%tsp (it was the text 1/3-1/2%tsp, unscaled). Plain numbers already were (1/3 → 0.333). Prefer Quantity::to_cooklang.
    • Check that add_optional_selections and the free-hand paths (FreeHandItem::spec, store.rs:123; mod.rs:518-546) read ranges back as ranges.
  9. Typed free-hand lines:
    • cooklang-ingredient-parser's Amount is a single number. add_typed (store.rs:406) never merges ranges, and edit_text reduces a range item to its name, so edited_item (crates/core/src/shopping_list/typed.rs:98) keeps the old quantity.
    • Needs range support in the ingredient-parser crate (cooklang-rs) or a documented limit.
  10. Pantry:
    • Recipe matching (crates/core/src/pantry/mod.rs:587-633, :731) compares names only. When it compares amounts, the spec says to compare a range against its upper end.
    • Shopping-list pantry subtraction (upstream subtract_pantry) only subtracts numbers, so a range keeps its full amount. That may need a cooklang-rs change.
  11. cooklang-reports: it re-parsed quantities from text, splitting ranges on ASCII -. Fixed in fix: use parsed quantities instead of re-reading them from text cooklang-reports#29 and Update parser v0.1.1: changed defaults #30, released in 0.11.0: parsed quantities are used as they are. feat: range quantities scale and add up (cooklang 0.23.0, spec 0011) #865 bumps it and fixes the comment in report.rs.

Tests that will change

Done in #865, along with the new range scaling, JSON and shopping list total tests. Still to add: a .shopping-list range round trip and a web range timer test.

  • crates/core/src/doctor.rs:1494-1530: range timers without warnings. Keep the test, remove the reliance on the private flag.
  • crates/core/src/doctor.rs:1612-1628: expects @beans{1-1/0%cans} to be text. Per the spec it is now a parse error.
  • tests/schema_output_test.rs:266-277: expects no timeRequired for ~{10-20%minutes}.
  • crates/format/src/number.rs:360-377: pins the 1/2 - 2 1/4 display.
  • New tests:
    • range scaling in recipe read (every format, including -f cooklang);
    • shopping list range totals (number + range, range + range);
    • .shopping-list range round trip, including a line read with an en dash;
    • range JSON output;
    • a range timer countdown in the web UI (seed/Shared/Pizza Dough.cook:25 has ~{10-20%minutes}).

Related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions