Refactor route resolution
This commit is contained in:
parent
7fdf451470
commit
7585687710
1 changed files with 24 additions and 37 deletions
|
@ -148,50 +148,37 @@ 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() {
|
||||||
None => {
|
Some(command) => routes.get(*command),
|
||||||
debug!("Found empty query, returning no route.");
|
None => {
|
||||||
return RouteResolution::Unresolved;
|
debug!("Found empty query, returning no route.");
|
||||||
|
return RouteResolution::Unresolved;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match (routes.get(*command), default_route) {
|
if maybe_route.is_some() {
|
||||||
// Found a route
|
split_args.next();
|
||||||
(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 {
|
let args = split_args.collect::<Vec<_>>().join(" ");
|
||||||
route: resolved,
|
|
||||||
args,
|
// Try resolving with a matched command
|
||||||
}
|
if let Some(route) = maybe_route {
|
||||||
}
|
debug!("Resolved {} with args {}", route, args);
|
||||||
// Unable to find route, but had a default route
|
return RouteResolution::Resolved { route, args };
|
||||||
(None, Some(route)) => {
|
}
|
||||||
let args = split_args.collect::<Vec<&str>>().join(" ");
|
|
||||||
|
// 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);
|
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
|
||||||
|
|
Loading…
Reference in a new issue