From c654f7dcdb67042e56bdacd94ce1020c8eeeffe9 Mon Sep 17 00:00:00 2001 From: Michel de Graaf Date: Sun, 29 Jun 2025 17:56:23 +0200 Subject: [PATCH] chore: fix warnings --- src/client.rs | 16 +++---- src/message/message.rs | 6 +-- src/message/message_reader.rs | 2 +- src/message/server/message_factory.rs | 2 +- src/server.rs | 4 +- src/utils/md5.rs | 64 +++++++++++++-------------- src/utils/mod.rs | 1 - src/utils/zlib.rs | 15 +++---- 8 files changed, 51 insertions(+), 59 deletions(-) diff --git a/src/client.rs b/src/client.rs index 5089e9e..2802234 100644 --- a/src/client.rs +++ b/src/client.rs @@ -79,19 +79,19 @@ impl Client { println!("Logging in as {}", self.username); if let Some(server) = &self.server { let result = server.login(&self.username, &self.password); - if result.unwrap() == true { - return Ok(true); + if result.unwrap() { + Ok(true) } else { - return Err(std::io::Error::new( + Err(std::io::Error::new( std::io::ErrorKind::Other, "Error logging in", - )); + )) } } else { - return Err(std::io::Error::new( + Err(std::io::Error::new( std::io::ErrorKind::Other, "Not connected to server", - )); + )) } } @@ -99,8 +99,8 @@ impl Client { println!("Searching for {}", query); if let Some(server) = &self.server { let hash = md5::md5(query); - let token = i32::from_str_radix(&hash[0..5].to_string(), 16).unwrap(); - server.file_search(token, &query); + let token = i32::from_str_radix(&hash[0..5], 16).unwrap(); + server.file_search(token, query); } else { eprintln!("Not connected to server"); } diff --git a/src/message/message.rs b/src/message/message.rs index 8152684..06e5c2c 100644 --- a/src/message/message.rs +++ b/src/message/message.rs @@ -1,4 +1,4 @@ -use std::{fmt::Display, str}; +use std::str; #[derive(Debug, PartialEq, Clone)] pub struct Message { @@ -42,7 +42,7 @@ impl Message { // Print the ASCII part let mut i = 0; for &byte in chunk { - i = i + 1; + i += 1; if byte.is_ascii_graphic() || byte.is_ascii_whitespace() { print!("{}", byte as char); } else { @@ -166,7 +166,7 @@ impl Message { .map(|byte| format!("{:02x}", byte)) .collect::(); self.pointer += size; - return hex_str; + hex_str } pub fn read_int8(&mut self) -> i8 { diff --git a/src/message/message_reader.rs b/src/message/message_reader.rs index e1a7df8..4e4adb1 100644 --- a/src/message/message_reader.rs +++ b/src/message/message_reader.rs @@ -74,7 +74,7 @@ impl MessageReader { } let message_buffer: Vec = self.buffer.drain(..total_size).collect(); - return Ok(Some(Message::new_with_data(message_buffer))); + Ok(Some(Message::new_with_data(message_buffer))) } } diff --git a/src/message/server/message_factory.rs b/src/message/server/message_factory.rs index 2862894..46e5e2b 100644 --- a/src/message/server/message_factory.rs +++ b/src/message/server/message_factory.rs @@ -71,7 +71,7 @@ impl MessageFactory { pub fn build_watch_user(token: &str) -> Message { Message::new() .write_raw_bytes([5, 0, 0, 0, 0].to_vec()) - .write_raw_hex_string(&token) + .write_raw_hex_string(token) .clone() } } diff --git a/src/server.rs b/src/server.rs index e8ce9e9..cb2cbb7 100644 --- a/src/server.rs +++ b/src/server.rs @@ -355,10 +355,10 @@ impl Server { } { - logged_in = context.lock().unwrap().logged_in.clone() + logged_in = context.lock().unwrap().logged_in } - if !logged_in.is_none() { + if logged_in.is_some() { break; } } diff --git a/src/utils/md5.rs b/src/utils/md5.rs index 71277b4..7921eb2 100644 --- a/src/utils/md5.rs +++ b/src/utils/md5.rs @@ -1,23 +1,19 @@ use std::convert::TryInto; -/** -* -* The MD5 Message-Digest Algorithm, written in Rust. -* Implemented by michael dimovich (@mdimovich) -* -* The purpose of this code is to act as a learning resource -* for the md5 message digest algorithm. I wanted to provide -* a straightforward walk-through of the md5 algorithm using -* Rust. -* -* This code was implemented using the original md5 message digest -* RFC, written by R. Rivest in April 1992. -* -* For more information about the md5 algorithm, or the original C -* source code implementation, please reference RFC 1321. -* -* (source: https://datatracker.ietf.org/doc/html/rfc1321) -* -*/ +// The MD5 Message-Digest Algorithm, written in Rust. +// Implemented by michael dimovich (@mdimovich) +// +// The purpose of this code is to act as a learning resource +// for the md5 message digest algorithm. I wanted to provide +// a straightforward walk-through of the md5 algorithm using +// Rust. +// +// This code was implemented using the original md5 message digest +// RFC, written by R. Rivest in April 1992. +// +// For more information about the md5 algorithm, or the original C +// source code implementation, please reference RFC 1321. +// +// (source: https://datatracker.ietf.org/doc/html/rfc1321) /** * This function is specified in the rfc as a way to generate the @@ -29,7 +25,7 @@ fn table_construction_function(i: u32) -> u32 { let x: f64 = i as f64; let sin_eval = x.sin().abs(); // note: 4294967296 == 2^32 - return (4294967296.0 * sin_eval) as u32; + (4294967296.0 * sin_eval) as u32 } /** @@ -78,6 +74,8 @@ fn round_one_operations( table: &Vec, x: &Vec, ) -> [u32; 4] { + let _ = x; + let _ = table; macro_rules! round1 { ( $a:ident, $b:ident, $c:ident, $d:ident, $k:expr, $s:expr, $i: expr ) => { $a = $b.wrapping_add( @@ -109,7 +107,7 @@ fn round_one_operations( round1!(c, d, a, b, 14, 17, 15); round1!(b, c, d, a, 15, 22, 16); - return [a, b, c, d]; + [a, b, c, d] } /** @@ -155,7 +153,7 @@ fn round_two_operations( round2!(c, d, a, b, 7, 14, 31); round2!(b, c, d, a, 12, 20, 32); - return [a, b, c, d]; + [a, b, c, d] } /** @@ -201,7 +199,7 @@ fn round_three_operations( round3!(c, d, a, b, 15, 16, 47); round3!(b, c, d, a, 2, 23, 48); - return [a, b, c, d]; + [a, b, c, d] } /** @@ -247,7 +245,7 @@ fn round_four_operations( round4!(c, d, a, b, 2, 15, 63); round4!(b, c, d, a, 9, 21, 64); - return [a, b, c, d]; + [a, b, c, d] } /** @@ -273,7 +271,7 @@ fn convert_u8_chunk_to_u32(chunk: &mut [u8]) -> Vec { temporary_vec.clear(); } } - return x; + x } fn compute_md5_digest(mut v: Vec) -> String { @@ -344,7 +342,7 @@ fn compute_md5_digest(mut v: Vec) -> String { word_c.swap_bytes(), word_d.swap_bytes() ); - return message_digest; + message_digest } /* @@ -372,11 +370,12 @@ fn bit_padding(input: &str) -> Vec { let length_bits_as_u8_array = split_u64_to_u8_array(bit_length); input_vector.extend(length_bits_as_u8_array); - return input_vector; + input_vector } fn split_u64_to_u8_array(s: u64) -> [u8; 8] { - let u8_array = [ + + [ s as u8, (s >> 8) as u8, (s >> 16) as u8, @@ -385,8 +384,7 @@ fn split_u64_to_u8_array(s: u64) -> [u8; 8] { (s >> 40) as u8, (s >> 48) as u8, (s >> 56) as u8, - ]; - return u8_array; + ] } fn construct_value_table() -> Vec { @@ -395,7 +393,7 @@ fn construct_value_table() -> Vec { for i in 1..=64 { t.push(table_construction_function(i)); } - return t; + t } // this should only work with utf-8 encoding and not full unicode support @@ -403,7 +401,7 @@ fn construct_value_table() -> Vec { fn convert_str_to_vec(input: &str) -> Vec { let mut byte_vec: Vec = Vec::new(); byte_vec.extend(input.as_bytes()); - return byte_vec; + byte_vec } /** @@ -425,7 +423,7 @@ fn convert_str_to_vec(input: &str) -> Vec { */ pub fn md5(input: &str) -> String { let input_vec = bit_padding(input); - return compute_md5_digest(input_vec); + compute_md5_digest(input_vec) } #[test] fn a_correct_hash() { diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 5cc1ba2..4b47c1d 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -2,4 +2,3 @@ pub mod md5; pub mod zlib; pub use md5::*; -pub use zlib::*; diff --git a/src/utils/zlib.rs b/src/utils/zlib.rs index 3816415..58754ce 100644 --- a/src/utils/zlib.rs +++ b/src/utils/zlib.rs @@ -1,7 +1,5 @@ //https://www.rfc-editor.org/rfc/rfc1950 -use flate2::bufread::ZlibDecoder; - #[derive(Debug)] struct ZlibHeader { cm: u8, @@ -32,13 +30,13 @@ pub fn extract_header(data: &Vec) -> Result { if cm != 8 { return Err(format!("ZLIB compression method not supported: {}", cmf)); } - return Ok(ZlibHeader { + Ok(ZlibHeader { cm, cinfo, fcheck, fdict, flevel, - }); + }) } pub fn extract_block_header(data: &Vec) -> BlockHeader { @@ -114,7 +112,7 @@ fn find_symbol(current_code: u32, code_len: u32) -> Option { // Range for symbols 256-279: 0b0000000 to 0b0010111 if (0..=0b0010111).contains(&reversed_code) { // Symbol = base_symbol + (code - base_code) - let symbol = 256 + (reversed_code - 0b0000000); + let symbol = 256 + reversed_code; return Some(symbol); } } @@ -122,7 +120,7 @@ fn find_symbol(current_code: u32, code_len: u32) -> Option { let reversed_code = reverse_bits(current_code, 8); // Range for symbols 0-143: 0b00110000 to 0b10111111 if (0b00110000..=0b10111111).contains(&reversed_code) { - let symbol = 0 + (reversed_code - 0b00110000); + let symbol = reversed_code - 0b00110000; return Some(symbol); } // Range for symbols 280-287: 0b11000000 to 0b11000111 @@ -148,7 +146,7 @@ fn find_symbol(current_code: u32, code_len: u32) -> Option { // If we checked a valid length but the code wasn't in any range for that length. None } -pub fn decode_fixed_huffman_symbol(reader: &mut BitReader) -> Result { +fn decode_fixed_huffman_symbol(reader: &mut BitReader) -> Result { let mut current_code = 0u32; let mut code_len = 0u32; loop { @@ -355,14 +353,11 @@ impl HuffmanTable { } } -// --- New Generic Symbol Decoder --- -/// Decodes one symbol from the stream using the provided Huffman table. fn decode_symbol( reader: &mut BitReader, huffman_table: &HuffmanTable, ) -> Result { let mut code = 0; - let mut len = 0; // The maximum length for DEFLATE codes is 15 bits. for i in 0..15 {