From 758568771077de10f0a5b7f57d6bf96d4a238993 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Sun, 27 Sep 2020 19:37:24 -0400 Subject: [PATCH] Refactor route resolution --- src/routes.rs | 61 ++++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 37 deletions(-) diff --git a/src/routes.rs b/src/routes.rs index 4058aed..5e848f5 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -148,50 +148,37 @@ fn resolve_hop<'a>( default_route: &Option, ) -> RouteResolution<'a> { let mut split_args = query.split_ascii_whitespace().peekable(); - let command = match split_args.peek() { - Some(command) => command, - None => { - debug!("Found empty query, returning no route."); - return RouteResolution::Unresolved; + let maybe_route = { + match split_args.peek() { + Some(command) => routes.get(*command), + None => { + debug!("Found empty query, returning no route."); + return RouteResolution::Unresolved; + } } }; - match (routes.get(*command), default_route) { - // Found a route - (Some(resolved), _) => { - let args = match split_args.next() { - // Discard the first result, we found the route using the first arg - Some(_) => { - let args = split_args.collect::>().join(" "); - debug!("Resolved {} with args {}", resolved, args); - args - } - None => { - debug!("Resolved {} with no args", resolved); - String::new() - } - }; + if maybe_route.is_some() { + split_args.next(); + } - RouteResolution::Resolved { - route: resolved, - args, - } - } - // Unable to find route, but had a default route - (None, Some(route)) => { - let args = split_args.collect::>().join(" "); + let args = split_args.collect::>().join(" "); + + // Try resolving with a matched command + if let Some(route) = maybe_route { + debug!("Resolved {} with args {}", route, args); + return RouteResolution::Resolved { route, args }; + } + + // Try resolving with the default route, if it exists + if let Some(route) = default_route { + if let Some(route) = routes.get(route) { debug!("Using default route {} with args {}", route, args); - match routes.get(route) { - Some(route) => RouteResolution::Resolved { route, args }, - None => RouteResolution::Unresolved, - } - } - // No default route and no match - (None, None) => { - debug!("Failed to resolve route!"); - RouteResolution::Unresolved + return RouteResolution::Resolved { route, args }; } } + + RouteResolution::Unresolved } /// Runs the executable with the user's input as a single argument. Returns Ok