From aaceb700447fce812554eb7417cf561561817858 Mon Sep 17 00:00:00 2001 From: Darius Auding <Darius.auding@gmx.de> Date: Sat, 24 Jun 2023 13:49:47 +0200 Subject: [PATCH] Fixing all `cargo clippy` warnings / making code better --- core/http/src/handlers/handler.rs | 10 +++++----- core/http/src/handling/file_handlers.rs | 6 +++--- core/http/src/handling/request/datatypes.rs | 2 +- core/http/src/handling/request/form_utils.rs | 14 +++++++------- core/http/src/handling/request/request_impl.rs | 13 +++++-------- core/http/src/handling/request/request_mime.rs | 15 +++++++-------- core/http/src/handling/routes.rs | 10 +++++----- core/http/src/setup.rs | 14 ++++++-------- 8 files changed, 39 insertions(+), 45 deletions(-) diff --git a/core/http/src/handlers/handler.rs b/core/http/src/handlers/handler.rs index 67d5117..99f8300 100644 --- a/core/http/src/handlers/handler.rs +++ b/core/http/src/handlers/handler.rs @@ -17,7 +17,7 @@ pub async fn handle_connection(mut stream: TcpStream, mountpoints: Vec<MountPoin let mut http_request: Vec<String> = Vec::with_capacity(10); loop { let mut buffer = String::new(); - if let Err(_) = buf_reader.read_line(&mut buffer).await { + if buf_reader.read_line(&mut buffer).await.is_err() { eprintln!("\x1b[31mAborting due to invalid UTF-8 in request header\x1b[0m"); return; } @@ -35,8 +35,8 @@ pub async fn handle_connection(mut stream: TcpStream, mountpoints: Vec<MountPoin }; let mut request = Request { - uri: if let Some(uri) = &request_status_line.split(" ").nth(1) { - *uri + uri: if let Some(uri) = &request_status_line.split(' ').nth(1) { + uri } else { eprintln!("\x1b[31mAborting due to invalid status line\x1b[0m"); return; @@ -45,7 +45,7 @@ pub async fn handle_connection(mut stream: TcpStream, mountpoints: Vec<MountPoin headers: http_request, mime_type: None, method: if let Some(method) = request_status_line - .split(" ") + .split(' ') .next() { if let Ok(ok) = method.parse() { ok @@ -90,7 +90,7 @@ pub async fn handle_connection(mut stream: TcpStream, mountpoints: Vec<MountPoin 0 }; if length != 0 { - request.mime_type = Request::extract_mime_from_vec(&mut request.headers); + request.mime_type = Request::extract_mime_from_vec(&request.headers); let mut buffer = vec![0u8; MAX_HTTP_MESSAGE_SIZE.into()]; let read = buf_reader.read(&mut buffer).await.unwrap(); if read != length { diff --git a/core/http/src/handling/file_handlers.rs b/core/http/src/handling/file_handlers.rs index 9e34054..8ca0019 100644 --- a/core/http/src/handling/file_handlers.rs +++ b/core/http/src/handling/file_handlers.rs @@ -18,7 +18,7 @@ impl ResponseBody for NamedFile { } fn get_mime(&self) -> Mime { - self.content_type.clone() + self.content_type } fn get_len(&self) -> usize { @@ -48,8 +48,8 @@ fn proove_path(path: PathBuf) -> PathBuf { PathBuf::from( path.to_str() .unwrap() - .split("/") - .filter(|&val| val != ".." && val != "") + .split('/') + .filter(|&val| val != ".." && !val.is_empty()) .collect::<Vec<&str>>() .join("/"), ) diff --git a/core/http/src/handling/request/datatypes.rs b/core/http/src/handling/request/datatypes.rs index 6b72da9..45f7b7f 100644 --- a/core/http/src/handling/request/datatypes.rs +++ b/core/http/src/handling/request/datatypes.rs @@ -13,7 +13,7 @@ pub trait FromRequest: Send { impl FromRequest for Vec<u8> { fn get_data(&self) -> &Self { - &self + self } fn set_data(&mut self, data: &Self) { diff --git a/core/http/src/handling/request/form_utils.rs b/core/http/src/handling/request/form_utils.rs index fef8a4f..4163710 100644 --- a/core/http/src/handling/request/form_utils.rs +++ b/core/http/src/handling/request/form_utils.rs @@ -19,8 +19,8 @@ impl Request<'_> { }; let data = data .1 - .split("&") - .map(|kvp| kvp.split_once("=")) + .split('&') + .map(|kvp| kvp.split_once('=')) .collect::<Vec<Option<(&str, &str)>>>(); let mut values: HashMap<&str, &str> = HashMap::new(); @@ -41,7 +41,7 @@ impl Request<'_> { error: ParseErrors::NoData, }) }; - response.insert((*key).into(), entry); + response.insert(*key, entry); } Ok(response) } @@ -99,7 +99,7 @@ impl Request<'_> { .unwrap() .trim_matches('"'); let mut temp_bound = "--".to_string(); - temp_bound.push_str(&format!("{boundary}")); + temp_bound.push_str(boundary); let end_boundary = format!("{temp_bound}--\r").as_bytes().to_owned(); temp_bound.push('\r'); let boundary = temp_bound.as_bytes(); @@ -123,8 +123,8 @@ impl Request<'_> { let mut current_key: Option<String> = None; let mut ignore_line = 0; for part in data.split(|byte| byte == &b'\n') { - if part == &[b'\r'] { - if let Some(_) = current_key { + if part == [b'\r'] { + if current_key.is_some() { if ignore_line >= TWO_NEWLINES { current_part.push(&[b'\n']); continue; @@ -178,7 +178,7 @@ impl Request<'_> { current_key = Some(mkey.to_owned()); } continue; - } else if let Some(_) = ¤t_key { + } else if current_key.is_some() { current_part.push(part); } } diff --git a/core/http/src/handling/request/request_impl.rs b/core/http/src/handling/request/request_impl.rs index 747cd23..2d2f7cb 100644 --- a/core/http/src/handling/request/request_impl.rs +++ b/core/http/src/handling/request/request_impl.rs @@ -4,15 +4,12 @@ use super::Request; impl Request<'_> { pub fn can_have_body(&self) -> bool { - match self.method { - Method::Post | Method::Put | Method::Patch | Method::Delete => true, - _ => false, - } + matches!( + self.method, + Method::Post | Method::Put | Method::Patch | Method::Delete + ) } pub fn mandatory_body(&self) -> bool { - match self.method { - Method::Post | Method::Put | Method::Patch => true, - _ => false, - } + matches!(self.method, Method::Post | Method::Put | Method::Patch) } } diff --git a/core/http/src/handling/request/request_mime.rs b/core/http/src/handling/request/request_mime.rs index 4fb9f3f..94ebc4c 100644 --- a/core/http/src/handling/request/request_mime.rs +++ b/core/http/src/handling/request/request_mime.rs @@ -3,7 +3,7 @@ use crate::utils::mime::mime_enum::Mime; use super::datatypes::Request; impl Request<'_> { - pub fn extract_mime_from_vec(headers: &Vec<String>) -> Option<Mime> { + pub fn extract_mime_from_vec(headers: &[String]) -> Option<Mime> { let Some(content_type_header) = headers .iter() .find(|header| header.starts_with("Content-Type: ")) else { @@ -14,23 +14,22 @@ impl Request<'_> { .unwrap() .to_string(); - let mime; - match content_type_string.split_once(';') { + let mime = match content_type_string.split_once(';') { Some(sub) => { - mime = if let Ok(a) = sub.0.trim().parse() { + if let Ok(a) = sub.0.trim().parse() { a } else { return None; - }; + } } None => { - mime = if let Ok(a) = content_type_string.trim().parse() { + if let Ok(a) = content_type_string.trim().parse() { a } else { return None; - }; + } } - } + }; Some(mime) } } diff --git a/core/http/src/handling/routes.rs b/core/http/src/handling/routes.rs index 9fd3f77..a85261a 100644 --- a/core/http/src/handling/routes.rs +++ b/core/http/src/handling/routes.rs @@ -39,19 +39,19 @@ impl Route<'_> { } } pub fn compare_uri(&self, uri: Uri) -> bool { - let mut iter_comp_str = uri.split("/"); - for true_str in self.uri.split("/") { + let mut iter_comp_str = uri.split('/'); + for true_str in self.uri.split('/') { let comp_str = if let Some(str) = iter_comp_str.next() { str } else { return false; }; - if (true_str.starts_with("<") && true_str.ends_with("..>")) - || (comp_str.starts_with(true_str) && comp_str.contains("?")) + if (true_str.starts_with('<') && true_str.ends_with("..>")) + || (comp_str.starts_with(true_str) && comp_str.contains('?')) { return true; } - if true_str.starts_with("<") && true_str.ends_with(">") { + if true_str.starts_with('<') && true_str.ends_with('>') { continue; } if true_str != comp_str { diff --git a/core/http/src/setup.rs b/core/http/src/setup.rs index 30e2bf7..417cf3d 100644 --- a/core/http/src/setup.rs +++ b/core/http/src/setup.rs @@ -21,9 +21,8 @@ pub struct Config { impl<'a> Config { fn check_mountpoint_taken(&self, to_insert: Uri) -> bool { if let Some(to_check) = &self.mountpoints { - let len = to_check.len(); - for i in 0..len { - if to_check[i].mountpoint == to_insert { + for i in to_check.iter() { + if i.mountpoint == to_insert { return true; // Found a duplicate &str } } @@ -38,10 +37,9 @@ impl<'a> Config { routes.sort_by(|a, b| a.rank.cmp(&b.rank)); let mut mount_message = format!(" >> \x1b[35m{}\x1b[0m\n", mountpoint); for (index, route) in routes.iter().enumerate() { - let indent_sign; - match index { - i if i == routes.len() - 1 => indent_sign = "└─", - _ => indent_sign = "├─", + let indent_sign = match index { + i if i == routes.len() - 1 => "└─", + _ => "├─", }; mount_message += &format!( @@ -114,7 +112,7 @@ pub async fn build(ip: &str) -> Config { } else { panic!("\x1b[31mCould't bind Listener to address\x1b[0m"); }; - let ip = ip.splitn(2, ":").collect::<Vec<&str>>(); + let ip = ip.splitn(2, ':').collect::<Vec<&str>>(); if ip.len() != 2 { panic!("Invalid IP Address"); } -- GitLab