feat(templated_uri): support Option<T> fields for RFC 6570 undefined …#408
feat(templated_uri): support Option<T> fields for RFC 6570 undefined …#408kate-shine wants to merge 1 commit intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds RFC 6570 “undefined variable” semantics to templated_uri templates by allowing Option<T> fields in #[templated] structs, so None omits the variable (and its prefix/separator) while Some(_) renders normally. This is implemented by regenerating render() and RedactedDisplay to walk parsed template parts and track “first emitted value” per expansion group.
Changes:
- Extend macro codegen to treat
Option<T>as RFC 6570 undefined values, with correct prefix/separator placement across expansion operators. - Add behavioral tests and a trybuild compile-fail test to validate omission behavior and trait-bound error spans.
- Update crate documentation and changelogs to describe the new
Option<T>feature.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/templated_uri_macros_impl/src/struct_template.rs | Main macro codegen changes: emit render() / RedactedDisplay by iterating template parts with optional-aware group handling. |
| crates/templated_uri_macros_impl/src/lib.rs | Updates inline snapshot tests to match the new codegen shape and adds a snapshot covering an optional field. |
| crates/templated_uri/tests/templated_uri.rs | Adds a large suite of behavioral tests validating Option<T> omission and separator/prefix handling. |
| crates/templated_uri/tests/compile_tests.rs | Registers the new trybuild compile-fail test. |
| crates/templated_uri/tests/ui/option_string_in_restricted_position.rs | New UI test ensuring Option<T> preserves inner-type span on Escape trait-bound errors. |
| crates/templated_uri/tests/ui/option_string_in_restricted_position.stderr | Expected stderr output for the new UI test. |
| crates/templated_uri/src/macros.rs | Documents Option<T> as RFC 6570 undefined values in #[templated] usage docs. |
| crates/templated_uri/src/lib.rs | Adds crate-level docs for Option<T> undefined semantics with examples. |
| crates/templated_uri/src/uri.rs | Minor doc/example formatting and import ordering. |
| crates/templated_uri/src/base_uri.rs | Minor doc/example formatting adjustments. |
| crates/templated_uri/src/path_and_query_template.rs | Minor doc/example import ordering and formatting. |
| crates/templated_uri/src/_documentation/recipes.rs | Minor doc import placement/formatting. |
| crates/templated_uri/examples/classified_templating.rs | Import reorganization for clarity/formatting. |
| crates/templated_uri/CHANGELOG.md | Adds an Unreleased feature entry for Option<T> support. |
| crates/templated_uri/README.md | Regenerated README to include the new Option<T> documentation section. |
| crates/templated_uri_macros_impl/CHANGELOG.md | Adds an Unreleased feature entry for Option<T> support. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
aa3319a to
d77c8c1
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #408 +/- ##
========================================
- Coverage 100.0% 99.9% -0.1%
========================================
Files 227 227
Lines 16740 17059 +319
========================================
+ Hits 16740 17055 +315
- Misses 0 4 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…values
Per RFC 6570 §2.3, template variables may be undefined. Allow Option<T>
fields in `#[templated]` structs to model this: `None` causes the
variable and its associated prefix or separator to be omitted from the
rendered URI, while `Some(value)` (including empty strings) renders
normally.
Both `render()` and the `RedactedDisplay` impl are regenerated to
walk the parsed template parts with `__first` tracking so that prefix
and separator placement stays correct across all RFC 6570 operators
(`{x}`, `{+x}`, `{/x,y}`, `{?x,y}`, `{&x}`, `{;x,y}`).
Includes 18 new behavioural tests covering each operator family plus a
trybuild compile-fail test ensuring `Option<T>` still propagates the
inner-type span on `Escape` trait-bound errors.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
d77c8c1 to
988dddc
Compare
| quote! { | ||
| use ::std::fmt::Write as _; | ||
| let mut __out = ::std::string::String::new(); | ||
| #(#statements)* | ||
| __out |
…values
Per RFC 6570 §2.3, template variables may be undefined. Allow Option fields in
#[templated]structs to model this:Nonecauses the variable and its associated prefix or separator to be omitted from the rendered URI, whileSome(value)(including empty strings) renders normally.Both
render()and theRedactedDisplayimpl are regenerated to walk the parsed template parts with__firsttracking so that prefix and separator placement stays correct across all RFC 6570 operators ({x},{+x},{/x,y},{?x,y},{&x},{;x,y}).Includes 18 new behavioural tests covering each operator family plus a trybuild compile-fail test ensuring
Option<T>still propagates the inner-type span onEscapetrait-bound errors.