Skip to main content

Module pantry

Module pantry 

Source
Expand description

cook pantry: what is in stock, and changing it.

load reads the configuration Context::pantry points at — a file, or text an editor is holding — and the queries answer questions about it: everything in it (list), what is running out (depleted), what is about to go off (expiring), and which recipes it can already cook (recipes).

plan is the odd one out: it answers “what should I stock?” by looking at the recipe collection alone, and never reads the pantry at all.

add, remove and update change the pantry and write it back. They are the only functions in this crate that write to a file the user owns, so read [write_atomically] and what a write touches before calling them.

§What a write touches

Only the entry asked for. A change is applied to the file as a TOML document, so everything else is left byte for byte as it was: comments, blank lines, indentation, key order, the choice between x = "1%kg" and x = { quantity = "1%kg" }, attributes cooklang does not model, and values that are not strings.

It did not always work this way. Every change used to re-parse the whole file into cooklang’s model, apply itself, and serialise that model back — so anything the model did not carry was gone the first time anything was added, removed or updated, silently, on a file people hand-write. A top-level item written with attributes fared worst: the parser reads

salt = { quantity = "1%kg", expire = "2027-01-01" }

as a section named salt holding items quantity and expire, and the rewrite emitted it as one — destroying the item and inventing two, on a command that had nothing to do with it (https://github.com/cooklang/cookcli/issues/429).

What a write still normalises, because it is what the writer must choose:

  • A new item is written name = "quantity" when that is all it has, and name = { … } when it carries more. An item added to general is always written in the short form, because a top-level inline table would be read back as a section header — the very shape above.
  • An item updated with an attribute it has no room for grows from the short form into a table, keeping the quantity it had.
  • A section emptied by remove is removed, matching what cooklang does with an empty section when it reads the file back.

update refuses, rather than guesses, when an item’s value is neither a quantity nor a set of attributes — a hand-written salt = 3. Attributes cooklang does not model are kept untouched; its own parse still reports them as unknown fields, which is about what cook pantry list can show rather than about anything being lost.

Structs§

AddRequest
An item to add to the pantry.
DepletedRequest
Which items count as running out.
ExpiringItem
A pantry item that is expiring, with the arithmetic already done.
ExpiringRequest
How far ahead to look for expiring items.
IngredientStep
One ingredient to buy, and what buying it achieves.
ListRequest
Which of the pantry to list.
PantryContents
A whole pantry configuration.
PantryItem
One item in the pantry.
PantryPlan
An order to stock a pantry in.
PantrySection
One section of the pantry, with the items written under it.
PartialMatch
A recipe most of whose ingredients are in stock.
PlanRequest
How far to take a pantry plan.
RecipeMatches
What the pantry can cook.
RecipesRequest
How complete a match has to be to be worth reporting.
RemoveRequest
Which item to take out of the pantry.
UpdateRequest
What to change about an item already in the pantry.

Functions§

add
Add an item to the pantry and write it back.
depleted
The items that are low or out of stock, in file order.
expiring
The items expiring within ExpiringRequest::days of today, soonest first.
list
List the pantry, optionally narrowed to one section.
load
Read and parse the pantry configuration Context::pantry names.
plan
Work out which ingredients to stock to cook as much of the collection as possible.
recipes
Work out which recipes under Context::base_path the pantry can cook.
remove
Take an item out of the pantry and write it back.
update
Change an item already in the pantry and write it back.