Skip to content

Commit d2bf24c

Browse files
committed
feat(doit): simplify URL_ENCODE handling
More maintainable template code, with less redundancy.
1 parent 1fee21d commit d2bf24c

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

src/mako/lib/mbuild.mako

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -532,31 +532,32 @@ else {
532532
}
533533
% endif
534534
535+
## Hanlde URI Tempates
535536
% if replacements:
536537
for &(find_this, param_name) in [${', '.join('("%s", "%s")' % r for r in replacements)}].iter() {
537-
% if URL_ENCODE in special_cases:
538-
let mut replace_with = String::new();
539-
% else:
540-
let mut replace_with: Option<<&str> = None;
541-
% endif
538+
<%
539+
replace_init = ': Option<&str> = None'
540+
replace_assign = 'Some(value)'
541+
url_replace_arg = 'replace_with.expect("to find substitution value in params")'
542+
if URL_ENCODE in special_cases:
543+
replace_init = ' = String::new()'
544+
replace_assign = 'value.to_string()'
545+
url_replace_arg = '&replace_with'
546+
# end handle url encoding
547+
%>\
548+
let mut replace_with${replace_init};
542549
for &(name, ref value) in params.iter() {
543550
if name == param_name {
544-
% if URL_ENCODE in special_cases:
545-
replace_with = value.to_string();
546-
% else:
547-
replace_with = Some(value);
548-
% endif
551+
replace_with = ${replace_assign};
549552
break;
550553
}
551554
}
552555
% if URL_ENCODE in special_cases:
553556
if find_this.as_bytes()[1] == '+' as u8 {
554557
replace_with = percent_encode(replace_with.as_bytes(), FORM_URLENCODED_ENCODE_SET);
555558
}
556-
url = url.replace(find_this, &replace_with);
557-
% else:
558-
url = url.replace(find_this, replace_with.expect("to find substitution value in params"));
559559
% endif
560+
url = url.replace(find_this, ${url_replace_arg});
560561
}
561562
## Remove all used parameters
562563
{

0 commit comments

Comments
 (0)