Skip to content

feat(templated_uri): support Option<T> fields for RFC 6570 undefined …#408

Open
kate-shine wants to merge 1 commit intomainfrom
u/kchuranov/templated-uri-option-fields
Open

feat(templated_uri): support Option<T> fields for RFC 6570 undefined …#408
kate-shine wants to merge 1 commit intomainfrom
u/kchuranov/templated-uri-option-fields

Conversation

@kate-shine
Copy link
Copy Markdown
Contributor

…values

Per RFC 6570 §2.3, template variables may be undefined. Allow Option 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.

Copilot AI review requested due to automatic review settings May 7, 2026 17:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crates/templated_uri/tests/ui/option_string_in_restricted_position.rs Outdated
Comment thread crates/templated_uri_macros_impl/src/struct_template.rs Outdated
Comment thread crates/templated_uri/tests/templated_uri.rs
@kate-shine kate-shine force-pushed the u/kchuranov/templated-uri-option-fields branch from aa3319a to d77c8c1 Compare May 7, 2026 18:03
@codecov
Copy link
Copy Markdown

codecov Bot commented May 7, 2026

Codecov Report

❌ Patch coverage is 98.96373% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.9%. Comparing base (9bbe9d7) to head (988dddc).

Files with missing lines Patch % Lines
...s/templated_uri_macros_impl/src/struct_template.rs 97.8% 4 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…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>
Copilot AI review requested due to automatic review settings May 7, 2026 18:26
@kate-shine kate-shine force-pushed the u/kchuranov/templated-uri-option-fields branch from d77c8c1 to 988dddc Compare May 7, 2026 18:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Comment on lines +233 to +237
quote! {
use ::std::fmt::Write as _;
let mut __out = ::std::string::String::new();
#(#statements)*
__out
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