This commit is contained in:
Edward Shen 2023-01-13 00:29:42 -08:00
parent 768f944b36
commit 0f27cfb788
Signed by: edward
GPG key ID: 19182661E818369F
3 changed files with 20 additions and 29 deletions

View file

@ -18,7 +18,7 @@ const LARGE_FILE_SIZE_THRESHOLD: u64 = 100_000_000;
#[cfg(test)]
const LARGE_FILE_SIZE_THRESHOLD: u64 = 1_000_000;
#[derive(Deserialize, Debug, PartialEq)]
#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct Config {
pub bind_address: String,
pub public_address: String,
@ -26,7 +26,7 @@ pub struct Config {
pub groups: Vec<RouteGroup>,
}
#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone)]
pub struct RouteGroup {
pub name: String,
pub description: Option<String>,
@ -35,7 +35,7 @@ pub struct RouteGroup {
pub routes: HashMap<String, Route>,
}
#[derive(Debug, PartialEq, Clone, Serialize)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize)]
pub struct Route {
pub route_type: RouteType,
pub path: String,
@ -211,7 +211,7 @@ fn get_route_type(path: &str) -> RouteType {
/// There exists two route types: an external path (e.g. a URL) or an internal
/// path (to a file).
#[derive(Debug, PartialEq, Clone, Serialize)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize)]
pub enum RouteType {
External,
Internal,
@ -288,7 +288,7 @@ pub fn get_config_data() -> Result<FileData, BunBunError> {
});
}
Err(e) => {
debug!("Tried to open a new file at '{location:?}' but failed due to error: {e}",)
debug!("Tried to open a new file at '{location:?}' but failed due to error: {e}");
}
}
}

View file

@ -57,10 +57,7 @@ async fn main() -> Result<()> {
.with(env_filter)
.init();
let conf_data = match opts.config {
Some(file_name) => load_custom_file(file_name),
None => get_config_data(),
}?;
let conf_data = opts.config.map_or_else(get_config_data, load_custom_file)?;
let conf = load_file(conf_data.file.try_clone()?, opts.large_config)?;
let state = Arc::from(ArcSwap::from_pointee(State {
@ -100,11 +97,10 @@ fn cache_routes(groups: Vec<RouteGroup>) -> HashMap<String, Route> {
for group in groups {
for (kw, dest) in group.routes {
// This function isn't called often enough to not be a performance issue.
match mapping.insert(kw.clone(), dest.clone()) {
None => trace!("Inserting {kw} into mapping."),
Some(old_value) => {
if let Some(old_value) = mapping.insert(kw.clone(), dest.clone()) {
trace!("Overriding {kw} route from {old_value} to {dest}.");
}
} else {
trace!("Inserting {kw} into mapping.");
}
}
}

View file

@ -153,14 +153,11 @@ fn resolve_hop<'a>(
default_route: Option<&str>,
) -> RouteResolution<'a> {
let mut split_args = query.split_ascii_whitespace().peekable();
let maybe_route = {
match split_args.peek() {
Some(command) => routes.get(*command),
None => {
let maybe_route = if let Some(command) = split_args.peek() {
routes.get(*command)
} else {
debug!("Found empty query, returning no route.");
return RouteResolution::Unresolved;
}
}
};
let args = split_args.collect::<Vec<_>>();
@ -177,15 +174,13 @@ fn resolve_hop<'a>(
}
// Try resolving with the default route, if it exists
if let Some(route) = default_route {
if let Some(route) = routes.get(route) {
if let Some(route) = default_route.and_then(|route| routes.get(route)) {
if check_route(route, arg_count) {
let args = args.join(" ");
debug!("Using default route {route} with args {args}");
return RouteResolution::Resolved { route, args };
}
}
}
RouteResolution::Unresolved
}