Skip to content

fix(parser): suppress spurious type warning on >> numeric metadata - #743

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/713-legacy-metadata-warning
Oct 17, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/713-legacy-metadata-warning

Conversation

@drawmeanelephant

@drawmeanelephant drawmeanelephant commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #713

A >> metadata value always reaches upstream's std checks as a YAML string, so >> servings: 4 warned Unsupported value for key: 'servings' ("expected 'number' but got 'string'").

Adds a parse_options() factory with a metadata_validator that suppresses the std check only for a servings entry whose value parses as u32 — exactly what Metadata::servings() applies. Nothing else is touched: >> servings: many and >> servings: 4 # x still warn (a YAML re-parse would have wrongly accepted the latter, which servings() itself rejects), and the time keys keep their real warnings like "Time overridden". All three recipe-parse entry points (parse_unscaled, doctor validation, parse_unscaled_recipe_from_entry) share the options.

Test plan

  • legacy_numeric_metadata_does_not_warn — no type warning, deprecation warning kept
  • legacy_bad_numeric_metadata_still_warns — many and 4 # x still warn
  • legacy_time_override_warning_is_preserved — time + prep time still warns "Time overridden"
  • cargo test -p cookcli-core green

@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 for looking into this. The spurious warning is real, but this change hides real warnings and accepts values that don't work.

1. It hides the real "Time overridden" warning

The time keys are included at crates/core/src/parser.rs:43, but >> time: 30 never warned in the first place: parse_time falls back to parsing a float. For those keys, calling run_std_checks(false) also skips locations.metadata.insert and the time-override check in cooklang's event consumer. So >> time: 30 together with >> prep time: 10 used to warn "Time overridden" and no longer does.

2. The re-parse is looser than the code that reads the value

parser.rs:47 re-parses with serde_yaml, so >> servings: 4 # x (and front matter servings: "4 # x") now validates without a warning. But Metadata::servings() uses s.parse::<u32>() and returns Text("4 # x"), so scaling by servings silently stops working.

3. The scope is wider than the description

The metadata validator also runs for YAML front matter, so quoted servings: "4" stops warning too. That one is harmless, because servings() accepts it, but the PR text says only >> is affected.

Suggested fix

Restrict the override to StdKey::Servings and check with value_str.parse::<u32>().is_ok(), which matches servings() exactly.

The underlying inconsistency is in cooklang-rs: check_std_entry uses as_u32(), while Metadata::servings() accepts numeric strings. That deserves an issue upstream so this workaround can eventually go. The LSP and cook report don't go through parse_options(), so the editor will keep showing the warning in the meantime.

This conflicts with #742 in all four shared files (adjacent lines). Both PRs patch three copies of the same PARSER.parse call, which suggests one shared helper (BOM strip plus options) used by parse_unscaled, doctor's validate_entry and util::parse_unscaled_recipe_from_entry.


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

@drawmeanelephant
drawmeanelephant force-pushed the fix/713-legacy-metadata-warning branch from 5bddd82 to 03a73d8 Compare October 13, 2026 20:41
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Reworked per review: the validator now touches only StdKey::Servings and checks value_str.parse::<u32>().is_ok(), matching Metadata::servings() exactly — so 4 # x still warns and the time keys are untouched (the "Time overridden" warning is covered by a new test). Noted the upstream inconsistency in check_std_entry vs servings() — worth a cooklang-rs issue as you say.

A >> metadata value reaches the std checks as a YAML string, so
">> servings: 4" warned "Unsupported value for key" even though
Metadata::servings() accepts it. Suppress the std check for a servings
entry only when its value parses to a u32 — the same test servings()
itself applies — so bad values and every other key's warnings (e.g.
"Time overridden") are unaffected.

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/713-legacy-metadata-warning branch from 03a73d8 to 9ea1011 Compare October 13, 2026 20:56
@LeMyst

LeMyst commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the rework. I checked the cases from the first review. >> servings: 4 no longer warns, while 4 # x, four, -1, 2,4 and front matter "4 # x" still do, matching Metadata::servings(). time + prep time (and prep/cook/time) still give "Time overridden". The other std keys behave exactly as on main, in both cook doctor validate and cook recipe. fmt, clippy and the workspace tests pass locally, and CI is green.

One thing left, for whoever merges second: this still conflicts with #742 in all four files, because both add their own options to the same three PARSER.parse calls. Folding the BOM strip and parse_options() into one helper would make that trivial, but it can also wait for a rebase. Otherwise this looks good to merge.


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

@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Noted — the conflict with #742 is all in the same three PARSER.parse call sites, so whoever lands second just keeps both options. If you'd rather I fold the BOM strip and parse_options() into one helper during the rebase, say the word and I'll pick it up.

@LeMyst
LeMyst merged commit 8442b2b into cooklang:main Oct 17, 2026
5 checks passed
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.

Legacy >> servings metadata warns spuriously (expected number, got string)

2 participants