Skip to content

Proposal 0021: alternative units and dual quantities - #157

Open
dubadub wants to merge 2 commits into
mainfrom
proposal/0021-alternative-units
Open

dubadub wants to merge 2 commits into
mainfrom
proposal/0021-alternative-units

Conversation

@dubadub

@dubadub dubadub commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Allow several equivalent amounts inside an ingredient's braces, separated by |: @flour{2%cups|260%g}. The first is the primary (as the author measured it), the others are author-supplied equivalents. All scale by the same factor.
  • Parsed model keeps quantity/units as the primary and adds an alternatives list, so existing consumers keep working.
  • Extend the unit system metadata key from Proposal: add unit system metadata #148 with a list form that names each position: unit system: [us customary, metric]. Apps use it to label a unit toggle and to resolve units spelled the same in several systems (cup, pint).
  • Defines selection, scaling, aggregation (one system at a time, fall back to primary), recipe references, shopping lists and pantry matching. Cookware and timer amounts are unchanged.

Motivation

Baking and UK/US recipes are routinely published with two measurements ("2 cups (260 g) flour"). Today one of them ends up in the note or as plain text and stops scaling. Client conversion (#148) can't convert volume to weight without densities and can't reproduce an author's own rounded figures. This proposal complements #148 rather than replacing it.

Addresses discussions #84, #142 and #51, and cooklang/cookcli#432.

Rendered proposal

@tmlmt

tmlmt commented Nov 5, 2026

Copy link
Copy Markdown

I fully agree with the syntax using the | as separator, but the rest is tricky.

  1. I would stick to specifying the syntax and how to deal with ambiguous units (i.e. relation with the proposal Proposal: add unit system metadata #148) but not more that. For instance:

    • Specifying the parsed model is out of scope for the spec in my opinion, although it is obviously something you need to consider for the cooklang-rs parser. By the way, note on this one: it will become challenging if/when you will want to include alternative ingredients into the spec.
    • The proposed Aggregation behavior proposed is also opinionated as it destroys information in the process. In my parser for instance, I sum both us/imperial quantities and metric quantities separately.
  2. Specifying the unit system may require refinement. I would suggest to define a preference for ambiguous units instead of a positional array. Consider for instance the following:

Add @tomato{1%can|400%g} and @water{1%cup|236%mL} and stir

  1. I have trouble understanding the Grammar block written the way it is, but it should also allow for more than 1 alternative e.g. @banana{1%large|1%cup|236%mL}(cut in small pieces)

- the three examples of a pipe between components now write the sigil
  on every option, the form proposed in 0025 (#162)
- no change to what this proposal defines: its pipe is inside the braces
@Plommonsorbet

Copy link
Copy Markdown

The need is real. Today @flour{2%cups}(260 g) "does not scale, not machine-readable", and a client cannot know that this author's cup of flour is 260 g. I would not do this, and I think a smaller mechanism covers what it is for.

The line I would draw: what the recipe text means belongs to cooklang, and so does data that tools have to share in order to agree. Aggregating amounts, rounding them, choosing which unit to show, subtracting a pantry: none of that says anything about cooklang. That second half is what alternatives are mostly for.

Numbered so they are easy to reply to.

Why I would not do this

  1. The right unit depends on the job, and the proposal cannot see the job. Most people do not care whether a recipe says a small, medium or large onion, until the recipe says one and the fridge holds the other. On the shopping list nobody knows what the shop has, so with alternatives @onion{1%large|1.5%medium} has to list both. A client rule is general instead: the list shows one and flips to the other on demand once you are standing in the shop, and it does that for every onion, not only the ones whose author wrote both. A baker who writes 2 dl of flour wants dl in the recipe, grams off the stock, and grams on the shopping list, because nobody goes out to buy 42 dl of flour. Three jobs, two units, one ingredient, and both metric. The proposal selects an alternative by the reader's system and aggregates "in one system at a time", so it cannot tell the three apart, and it would put 42 dl on the shopping list.

  2. So a choice that is the reader's becomes a guess the implementation makes, per ingredient and per job. Which alternative fits which job is something a person decides once, for themselves, consistently, and a client can hold that rule. Written into the braces it is something every implementation has to work out, and the proposal already needs two rules to try: the reader's system for selection and aggregation, and for the pantry "whichever alternative shares a unit, or a convertible unit". An implementation that would rather let the user choose than guess does not escape it either: it now has a layered decision, the author's alternatives, the user's rule and the job, reconciled per ingredient, where a client rule on its own is one layer. The guessing is what makes the useful things harder, for a marginal gain.

  3. Aggregation destroys the choice the alternatives exist to keep. tmlmt said this above, that the aggregation rule "destroys information in the process", and I think it goes further than information. @onion{1%large|1.5%medium} and @onion{2%medium} have to become one line. To add them a tool must first collapse the first to one alternative, and whichever it picks the other is gone: 3.5 medium with no way back to large, or 1 large beside 2 medium, which is not a sum. The proposal's rules give the second, since neither large nor medium belongs to a system, so each falls back to its primary and the two are listed separately. A client rule keeps the choice because it is a rule and not a pair: large is 1.5 medium, the sum is 3.5 medium, and it reads as 2.33 large on demand. tmlmt's parser summing each system separately is the other way to keep it, and both are the client deciding, which is the point.

  4. It cannot keep its own invariant while text amounts exist. @salt{a pinch|0.5%ml} at double is a pinch and 1 ml, because text never scales and numbers do, while the alternatives are defined as describing "one quantity of one ingredient". The proposal already rejects this shape when the cause is syntax, "Fixing one alternative but not another would make them disagree, so = in front of a later alternative is a warning and is ignored", and a text value does the same thing. That is not this proposal's doing, it inherits it from text amounts, but nothing inside the proposal can resolve it.

What I would rather do to start with

This is a library feature, not a spec change, on purpose, and tmlmt's first point above says why from the other side: the parsed model is out of scope for the spec. A need met below the spec is one cooklang never has to carry. The template already asks every proposal what applications should do, so this is the right level, not the wrong thread.

  1. A caller-supplied conversion. The client provides one function, and the crate calls it when its own converter cannot do the job:

    fn convert(ingredient: &str, amount: &Quantity, wanted: &Unit) -> Option<Quantity>

    It gets the amount as written, the unit the caller wants, and the ingredient's name so a density or a rule can be looked up. None means no conversion and is today's behaviour. A client with FDC densities, a user with a table of their own, and an app with nothing all use the same interface. ParseOptions already has two of these, recipe_ref_check and metadata_validator, both callbacks the caller supplies for knowledge the crate does not have, so this would be a third of a kind rather than a new kind. It can be tried and withdrawn, which a spec entry cannot.

  2. It reaches cases dual amounts cannot, which is why I want it. @onion{1%large} is how people write recipes, and no spec can say what a large onion is. With a hook I can declare in my own configuration that a large onion is 1.5 medium. That is not precision, a large onion is not exactly one and a half medium ones, but it is consistent, and it is the same sum I would be doing in my head at the supermarket anyway.

  3. Its reach is every recipe, not only the ones whose author wrote both. I would not go back through my own collection adding second amounts, and even if I did it would do nothing for the recipes I did not write. A client rule applies to everything I read, mine and other people's, and it applies consistently where alternatives cannot: two authors' equivalents for the same ingredient, 260 g and 240 g for 2 cups, may each be right for their own recipe, but summed on one shopping list they mean nothing, where one rule across the list does. The author with the spooned-and-levelled cup is real, and that author can still write the weight.

Not a view, just where I am

  1. Density. On Proposal 0028: units on the shopping list and in recipe references #166's table I have no settled view, and I think the topic deserves proper study rather than a quick answer. A table is at least the right shape for a spec, since tools would have to share the data to agree on a total. But my instinct is that a density is an estimate: bakers weigh for a reason. For a pantry that may be fine, since stock-keeping is best effort wherever it is done: every shop with stock software still counts its shelves, because the software's figure is an estimate and the count is the reconciliation. Taking 2 dl of flour off a pantry as roughly 120 g is that practice, not a shortcut. There is a direction to it too: dl to grams for the shopping list loses nothing anyone wanted, while turning a weight the author chose into a volume throws away the precision they were after. That is the sort of thing I would want to work through before saying whether it belongs in the spec. A density plugs into the same hook whichever way that goes, so nothing above depends on the answer.

Either way I would do the callback first. It is small, it does something that cannot be done today, and it does not become redundant if densities or ingredient data do land in cooklang later: the data answers the cases it has rows for, the callback answers the rest. So this is a no to the mechanism, not to the need, and an order of work for the rest. The internals of cooklang-rs first, so a conversion like this has somewhere to live, then the spec follows what works. Same thing I said on #148, and I would rather write it up once than repeat it in every thread.

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.

3 participants