Improve 'uri!' macro internal code.

This commit is contained in:
Sergio Benitez 2017-09-10 02:16:51 -07:00
parent a9dd52a4fb
commit a291c9b42e
1 changed files with 5 additions and 8 deletions

View File

@ -61,10 +61,9 @@ fn extract_exprs<'a>(
Err(diag) Err(diag)
} }
Validation::Named(missing, extra, dup) => { Validation::Named(missing, extra, dup) => {
let ps = p!("parameter", internal.fn_args.len()); let e = &format!("invalid parameters for `{}` route uri", route_name);
let e = &format!("invalid {} for `{}` route uri", ps, route_name);
let mut diag = ecx.struct_span_err(internal.uri_params.args_span(), e); let mut diag = ecx.struct_span_err(internal.uri_params.args_span(), e);
diag.note(&format!("expected {}: {}", ps, internal.fn_args_str())); diag.note(&format!("uri parameters are: {}", internal.fn_args_str()));
fn join<S: Display, T: Iterator<Item = S>>(iter: T) -> (&'static str, String) { fn join<S: Display, T: Iterator<Item = S>>(iter: T) -> (&'static str, String) {
let items: Vec<_> = iter.map(|i| format!("`{}`", i)).collect(); let items: Vec<_> = iter.map(|i| format!("`{}`", i)).collect();
@ -107,6 +106,7 @@ pub fn uri_internal(
// Generate the statements to typecheck each parameter. First, the mount. // Generate the statements to typecheck each parameter. First, the mount.
let mut argument_stmts = vec![]; let mut argument_stmts = vec![];
let mut format_assign_tokens = vec![]; let mut format_assign_tokens = vec![];
let mut fmt_string = internal.uri.node.replace('<', "{").replace('>', "}");
if let Some(mount_point) = internal.uri_params.mount_point { if let Some(mount_point) = internal.uri_params.mount_point {
// TODO: Should all expressions, not just string literals, be allowed? // TODO: Should all expressions, not just string literals, be allowed?
// let as_ref = ecx.expr_method_call(span, expr, Ident::from_str("as_ref"), v![]); // let as_ref = ecx.expr_method_call(span, expr, Ident::from_str("as_ref"), v![]);
@ -120,8 +120,7 @@ pub fn uri_internal(
)); ));
format_assign_tokens.push(quote_tokens!(ecx, mount = mount,)); format_assign_tokens.push(quote_tokens!(ecx, mount = mount,));
} else { fmt_string = "{mount}".to_string() + &fmt_string;
format_assign_tokens.push(quote_tokens!(ecx, mount = "/",));
} }
// Now the user's parameters. // Now the user's parameters.
@ -136,10 +135,8 @@ pub fn uri_internal(
)); ));
} }
let user_path = internal.uri.node.replace('<', "{").replace('>', "}");
let route_uri = "{mount}".to_string() + &user_path;
MacEager::expr(quote_expr!(ecx, { MacEager::expr(quote_expr!(ecx, {
$argument_stmts $argument_stmts
::rocket::http::uri::URI::from(format!($route_uri, $format_assign_tokens)) ::rocket::http::uri::URI::from(format!($fmt_string, $format_assign_tokens))
})) }))
} }