Skip to content

Commit

Permalink
feat(doit): simplify URL_ENCODE handling
Browse files Browse the repository at this point in the history
More maintainable template code, with less redundancy.
  • Loading branch information
Byron committed Mar 17, 2015
1 parent 1fee21d commit d2bf24c
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/mako/lib/mbuild.mako
Original file line number Diff line number Diff line change
Expand Up @@ -532,31 +532,32 @@ else {
}
% endif
## Hanlde URI Tempates
% if replacements:
for &(find_this, param_name) in [${', '.join('("%s", "%s")' % r for r in replacements)}].iter() {
% if URL_ENCODE in special_cases:
let mut replace_with = String::new();
% else:
let mut replace_with: Option<<&str> = None;
% endif
<%
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 {
% if URL_ENCODE in special_cases:
replace_with = value.to_string();
% else:
replace_with = Some(value);
% endif
replace_with = ${replace_assign};
break;
}
}
% if URL_ENCODE in special_cases:
if find_this.as_bytes()[1] == '+' as u8 {
replace_with = percent_encode(replace_with.as_bytes(), FORM_URLENCODED_ENCODE_SET);
}
url = url.replace(find_this, &replace_with);
% else:
url = url.replace(find_this, replace_with.expect("to find substitution value in params"));
% endif
url = url.replace(find_this, ${url_replace_arg});
}
## Remove all used parameters
{
Expand Down

0 comments on commit d2bf24c

Please sign in to comment.