From 648eb1a5ebd50099e90ef26f1f641f2b685175ec Mon Sep 17 00:00:00 2001 From: jeb Date: Sat, 23 Jun 2018 23:31:44 -0600 Subject: [PATCH] Prefix 'uri!' format args to allow ignored parameters. --- core/codegen/src/macros/uri.rs | 4 ++++ core/codegen/src/parser/uri_macro.rs | 2 +- core/codegen/tests/typed-uris.rs | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/core/codegen/src/macros/uri.rs b/core/codegen/src/macros/uri.rs index 349b4c0a..b2217bfa 100644 --- a/core/codegen/src/macros/uri.rs +++ b/core/codegen/src/macros/uri.rs @@ -128,6 +128,10 @@ pub fn uri_internal( // Building <$T as ::rocket::http::uri::FromUriParam<_>>::from_uri_param($e). for (i, &(mut ident, ref ty)) in internal.fn_args.iter().enumerate() { let (span, mut expr) = (exprs[i].span, exprs[i].clone()); + + // Format argument names cannot begin with `_`, but a function parameter + // might, so we prefix each parameter with the letters `fmt`. + ident.name = Symbol::intern(&format!("fmt{}", ident.name)); ident.span = span; // path for call: >::from_uri_param diff --git a/core/codegen/src/parser/uri_macro.rs b/core/codegen/src/parser/uri_macro.rs index 62ef6cb7..44658fa6 100644 --- a/core/codegen/src/parser/uri_macro.rs +++ b/core/codegen/src/parser/uri_macro.rs @@ -255,7 +255,7 @@ impl InternalUriParams { pub fn uri_fmt_string(&self) -> String { self.uri.node - .replace('<', "{") + .replace('<', "{fmt") .replace("..>", "}") .replace('>', "}") } diff --git a/core/codegen/tests/typed-uris.rs b/core/codegen/tests/typed-uris.rs index 461a7afe..3237f32d 100644 --- a/core/codegen/tests/typed-uris.rs +++ b/core/codegen/tests/typed-uris.rs @@ -50,6 +50,9 @@ fn simple2(id: i32, name: String) -> &'static str { "" } #[post("//")] fn simple2_flipped(name: String, id: i32) -> &'static str { "" } +#[post("//<_unused>")] +fn unused_param(used: i32, _unused: i32) -> &'static str { "" } + #[post("/")] fn guard_1(cookies: Cookies, id: i32) -> &'static str { "" } @@ -90,6 +93,7 @@ fn check_simple_unnamed() { assert_uri_eq! { uri!(simple: 100) => "/100", uri!(simple: -23) => "/-23", + uri!(unused_param: 1, 2) => "/1/2", } // The "flipped" test ensures that the order of parameters depends on the @@ -118,6 +122,7 @@ fn check_simple_named() { assert_uri_eq! { uri!(simple: id = 100) => "/100", uri!(simple: id = -23) => "/-23", + uri!(unused_param: used = 1, _unused = 2) => "/1/2", } assert_uri_eq! {