Skip to content

feat: add query parameter support to the reverse! macro - #642

Open
ChrisJr404 wants to merge 1 commit into
cot-rs:masterfrom
ChrisJr404:reverse-query-params
Open

feat: add query parameter support to the reverse! macro#642
ChrisJr404 wants to merge 1 commit into
cot-rs:masterfrom
ChrisJr404:reverse-query-params

Conversation

@ChrisJr404

Copy link
Copy Markdown

Related issue or discussion

Closes #430

Description

Adds query parameter support to reverse!. Path params are still passed right after the view name, and query params can now be added after a ;, using the same key = value syntax:

reverse!(request, "article", id = 5; page = 2, sort = "new")?
// -> "/articles/5?page=2&sort=new"

They get appended as a percent-encoded query string (built with form_urlencoded, which we already depend on). Values only need to implement ToString, just like path params do. When no query params are given the URL is returned unchanged, so existing reverse! calls behave exactly as before.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Refactor / cleanup
  • Performance improvement
  • Other (describe above)

Checklist

  • I've read the contributing guide
  • Tests pass locally (just test-all)
  • Code passes clippy (just clippy)
  • Code is properly formatted (cargo fmt)
  • New tests added (regression test for bugs, coverage for new features)
  • Documentation (both code and site) updated (if applicable)

Path parameters are passed right after the view name, and query
parameters can now be added after a semicolon. They get appended to the
generated URL as a percent-encoded query string, so you don't have to
build it by hand anymore.
@github-actions github-actions Bot added the C-lib Crate: cot (main library crate) label Aug 25, 2026

@ElijahAhianyo ElijahAhianyo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the contribution @ChrisJr404! Aside the comment on replicating this for the reverse_redirect! macro, this looks good to me.

Comment thread cot/src/router.rs
#[macro_export]
macro_rules! reverse {
($request:expr, $view_name:literal $(, $($key:ident = $value:expr),*)?) => {{
($request:expr, $view_name:literal

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

would be nice to have this for reverse_redirect! as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-lib Crate: cot (main library crate)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Query param helper

2 participants