Add try_get and document the remaining public-API panics#29
Merged
Conversation
The last road-to-1.0 item: take the public API's panics out of the "looks like sloppiness" category. This one is additive, not breaking. - `L10nLanguageVec::try_get` returns `Option<&L10nBundle>` for the case the type system can't rule out: a language id from outside the loaded set (a header, a query param, user input). `get` stays for known `L10n` variants (the generated `L10n::langneg` always yields one) but now documents its panic precondition and delegates to `try_get`. - The generated single-file `load`/`load_all` kept a bare `.unwrap()`. They are infallible by construction — the `.ftl` is embedded and validated at build time — so they stay non-`Result`, but the panic is now an `expect` that says it's a build-time bug worth reporting, not a runtime condition. The generated message accessors (`msg(..).unwrap()` etc.) are deliberately left as-is: their ids and arg types are generated from the same validated `.ftl`, so a failure there is the same drift invariant already documented on `L10nError`, and per-accessor `expect` strings would only bloat the output. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The last road-to-1.0 item (#4 of 4): get the public API's panics out of the "looks like sloppiness" category. This one is additive, not breaking — no existing call needs to change.
What & why
I sorted every remaining panic by who can trigger it:
L10nLanguageVec::get(impl AsRef<str>)try_get -> Option; keepget, document its preconditionload()/load_all()(single-file).ftlunwrap()→ diagnosticexpect()msg(..).unwrap()accessors.ftlL10nError)Only the first is a genuine footgun:
getaccepts an arbitrary string and panics on a miss. So:try_get(&self, lang) -> Option<&L10nBundle>— the non-panicking path for when the language id comes from outside the loaded set (a rawAccept-Language, a query param, user input).getstays for the common case — you pass anL10nenum variant, and the generatedL10n::langnegalways returns one (it has aDefaultfallback), so the lookup is guaranteed to hit. It now documents the panic precondition and delegates totry_get. Matches the stdlibIndex-panics /get-returns-Optionidiom (names inverted here for back-compat).The generated single-file
load/load_allare infallible by construction — the.ftlis embedded and validated at build time — so they keep their non-Resultsignatures (forcing callers to handle an impossible error would be pure noise; compare the compressedload<D>, which genuinely returnsResultbecause the user's decompressor can fail). The bareunwrap()is now anexpect()that says it's a build-time bug worth reporting.The generated message accessors are deliberately untouched: a failure there is the same generated-code-vs-embedded-
.ftldrift invariant already documented onL10nError, and adding anexpectstring to every accessor would only bloat the output.Verification
cargo fmt --check,clippy --all-targets --features build,langneg -D warnings,RUSTDOCFLAGS=-D warnings cargo doc— all cleantry_getSome/None) + 1 integration (playground) test pass; benches build*_gen.rsfixtures + snapshots and the playgroundsingle_l10n.rs; the entire generated diff is onlyunwrap()→expect(..)onload/load_all(gzip and multi-file outputs correctly unchanged)0.7 cycle
✅ #25 seal
BuildOptions· ✅ #26 sealFtlOutputOptions· ✅ #28 typedL10nError· ✅ this — closes out the 0.7 road-to-1.0 work.🤖 Generated with Claude Code