Refactor route resolution

This commit is contained in:
Edward Shen 2020-09-27 19:37:24 -04:00
parent 7fdf451470
commit 7585687710
Signed by: edward
GPG key ID: 19182661E818369F

View file

@ -148,51 +148,38 @@ fn resolve_hop<'a>(
default_route: &Option<String>, default_route: &Option<String>,
) -> RouteResolution<'a> { ) -> RouteResolution<'a> {
let mut split_args = query.split_ascii_whitespace().peekable(); let mut split_args = query.split_ascii_whitespace().peekable();
let command = match split_args.peek() { let maybe_route = {
Some(command) => command, match split_args.peek() {
Some(command) => routes.get(*command),
None => { None => {
debug!("Found empty query, returning no route."); debug!("Found empty query, returning no route.");
return RouteResolution::Unresolved; 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::<Vec<&str>>().join(" ");
debug!("Resolved {} with args {}", resolved, args);
args
}
None => {
debug!("Resolved {} with no args", resolved);
String::new()
} }
}; };
RouteResolution::Resolved { if maybe_route.is_some() {
route: resolved, split_args.next();
args,
} }
let args = split_args.collect::<Vec<_>>().join(" ");
// Try resolving with a matched command
if let Some(route) = maybe_route {
debug!("Resolved {} with args {}", route, args);
return RouteResolution::Resolved { route, args };
} }
// Unable to find route, but had a default route
(None, Some(route)) => { // Try resolving with the default route, if it exists
let args = split_args.collect::<Vec<&str>>().join(" "); if let Some(route) = default_route {
if let Some(route) = routes.get(route) {
debug!("Using default route {} with args {}", route, args); debug!("Using default route {} with args {}", route, args);
match routes.get(route) { return RouteResolution::Resolved { route, args };
Some(route) => RouteResolution::Resolved { route, args },
None => RouteResolution::Unresolved,
} }
} }
// No default route and no match
(None, None) => {
debug!("Failed to resolve route!");
RouteResolution::Unresolved RouteResolution::Unresolved
} }
}
}
/// Runs the executable with the user's input as a single argument. Returns Ok /// Runs the executable with the user's input as a single argument. Returns Ok
/// so long as the executable was successfully executed. Returns an Error if the /// so long as the executable was successfully executed. Returns an Error if the