From 0f27cfb788ea6935c409b081d736a2a90a4e9825 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Fri, 13 Jan 2023 00:29:42 -0800 Subject: [PATCH] clippy --- src/config.rs | 10 +++++----- src/main.rs | 14 +++++--------- src/routes.rs | 25 ++++++++++--------------- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/src/config.rs b/src/config.rs index 1436101..1c8aed7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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, } -#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)] +#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone)] pub struct RouteGroup { pub name: String, pub description: Option, @@ -35,7 +35,7 @@ pub struct RouteGroup { pub routes: HashMap, } -#[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 { }); } 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}"); } } } diff --git a/src/main.rs b/src/main.rs index 76b9e2f..5d4b28a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) -> HashMap { 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) => { - trace!("Overriding {kw} route from {old_value} to {dest}."); - } + 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."); } } } diff --git a/src/routes.rs b/src/routes.rs index ce4e8f5..a8dcdd3 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -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 => { - debug!("Found empty query, returning no route."); - return RouteResolution::Unresolved; - } - } + 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::>(); @@ -177,13 +174,11 @@ 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 check_route(route, arg_count) { - let args = args.join(" "); - debug!("Using default route {route} with args {args}"); - return RouteResolution::Resolved { route, args }; - } + 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 }; } }