From 2cc48072344715c428c204e98ab991c8133cf4c2 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 13 May 2015 10:11:00 +0200 Subject: [PATCH] fix(API): URL substitution handling Previously we would remove the wrong parameters when attempting to remove only those parameters that have been used in the URL substitution. The code we have now is more idiomatic and appears to be removing the correct parameters. Closes #114 [skip ci] --- src/mako/api/lib/mbuild.mako | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/mako/api/lib/mbuild.mako b/src/mako/api/lib/mbuild.mako index cd754549e6..78cb303f22 100644 --- a/src/mako/api/lib/mbuild.mako +++ b/src/mako/api/lib/mbuild.mako @@ -618,16 +618,16 @@ else { ## Hanlde URI Tempates % if replacements: for &(find_this, param_name) in [${', '.join('("%s", "%s")' % r for r in replacements)}].iter() { - <% - replace_init = ': Option<&str> = None' - replace_assign = 'Some(value)' - url_replace_arg = 'replace_with.expect("to find substitution value in params")' - if URL_ENCODE in special_cases: - replace_init = ' = String::new()' - replace_assign = 'value.to_string()' - url_replace_arg = '&replace_with' - # end handle url encoding - %>\ +<% + replace_init = ': Option<&str> = None' + replace_assign = 'Some(value)' + url_replace_arg = 'replace_with.expect("to find substitution value in params")' + if URL_ENCODE in special_cases: + replace_init = ' = String::new()' + replace_assign = 'value.to_string()' + url_replace_arg = '&replace_with' + # end handle url encoding +%>\ let mut replace_with${replace_init}; for &(name, ref value) in params.iter() { if name == param_name { @@ -645,12 +645,9 @@ else { ## Remove all used parameters { let mut indices_for_removal: Vec = Vec::with_capacity(${len(replacements)}); - for param_name in [${', '.join('"%s"' % r[1] for r in replacements)}].iter() { - for (index, &(ref name, _)) in params.iter().rev().enumerate() { - if name == param_name { - indices_for_removal.push(params.len() - index - 1); - break; - } + for param_name in [${', '.join(reversed(['"%s"' % r[1] for r in replacements]))}].iter() { + if let Some(index) = params.iter().position(|t| &t.0 == param_name) { + indices_for_removal.push(index); } } for &index in indices_for_removal.iter() {