From: Grégory Burri Date: Thu, 8 Aug 2019 14:15:14 +0000 (+0200) Subject: Cleaning, thanks Clippy. X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=96a5780355820bb1a78cb4ec8dff41dd30ef6ceb;p=rup.git Cleaning, thanks Clippy. --- diff --git a/src/crypto.rs b/src/crypto.rs index a7aa5b9..ddf257c 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -27,7 +27,7 @@ pub enum DecryptError { fn decode_key(key: &str) -> Result, KeyError> { match base64::decode(key) { - Ok(k) => if k.len() != 16 { return Err(KeyError::WrongKeyLength) } else { Ok(k) }, + Ok(k) => if k.len() != 16 { Err(KeyError::WrongKeyLength) } else { Ok(k) }, Err(_e) => Err(KeyError::UnableToDecodeBase64Key) } } @@ -37,7 +37,7 @@ fn decode_key(key: &str) -> Result, KeyError> { /// IV: 16 bytes randomized. /// Mode : CBC. pub fn encrypt(key: &str, plain_text: &str) -> Result { - let key_as_bytes = decode_key(key).map_err(|e| EncryptError::KeyError(e))?; + let key_as_bytes = decode_key(key).map_err(EncryptError::KeyError)?; let text_as_bytes = plain_text.as_bytes(); let iv = rand::thread_rng().gen::<[u8; 16]>(); @@ -59,7 +59,7 @@ pub fn encrypt(key: &str, plain_text: &str) -> Result { /// Decrypt the given text with the given key. The key length must be 128 bits encoded in base64. /// Input format: "1" + base_64( + + ) pub fn decrypt(key: &str, cipher_text: &str) -> Result { - let key_as_bytes = decode_key(key).map_err(|e| DecryptError::KeyError(e))?; + let key_as_bytes = decode_key(key).map_err(DecryptError::KeyError)?; // Can't decrypt a message with the wrong version. if !cipher_text.starts_with(consts::CURRENT_MESSAGE_VERSION) { return Err(DecryptError::WrongMessageVersion) } diff --git a/src/main.rs b/src/main.rs index f1e66ee..3056267 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ extern crate percent_encoding; use listenfd::ListenFd; use actix_files as fs; -use actix_web::{web, middleware, App, HttpServer, HttpResponse, Responder, Result, web::Query}; +use actix_web::{web, middleware, App, HttpServer, HttpResponse, Result, web::Query}; use askama::Template; use std::io::prelude::*; @@ -60,14 +60,14 @@ fn get_exe_name() -> String { fn read_key() -> String { let mut key = String::new(); File::open(consts::FILE_KEY) - .expect(&format!("Failed to open key file: {}", consts::FILE_KEY)) + .unwrap_or_else(|_| panic!("Failed to open key file: {}", consts::FILE_KEY)) .read_to_string(&mut key) - .expect(&format!("Failed to read key file: {}", consts::FILE_KEY)); + .unwrap_or_else(|_| panic!("Failed to read key file: {}", consts::FILE_KEY)); String::from( percent_encoding::percent_decode(key.replace('\n', "").as_bytes()) .decode_utf8() - .expect(&format!("Failed to decode key file: {}", consts::FILE_KEY)) + .unwrap_or_else(|_| panic!("Failed to decode key file: {}", consts::FILE_KEY)) ) } @@ -79,7 +79,7 @@ fn main() -> std::io::Result<()> { println!("Starting RUP as web server..."); let config: Config = { - let f = File::open(consts::FILE_CONF).expect(&format!("Failed to open configuration file {}", consts::FILE_CONF)); + let f = File::open(consts::FILE_CONF).unwrap_or_else(|_| panic!("Failed to open configuration file {}", consts::FILE_CONF)); match from_reader(f) { Ok(c) => c, Err(e) => panic!("Failed to load config: {}", e) @@ -112,7 +112,7 @@ fn main() -> std::io::Result<()> { server.run() } -fn process_args(key: &String) -> bool { +fn process_args(key: &str) -> bool { fn print_usage() { println!("Usage:"); println!(" {} [--help] [--encrypt |--decrypt ]", get_exe_name()); @@ -124,7 +124,7 @@ fn process_args(key: &String) -> bool { print_usage(); return true } else if let Some((position_arg_encrypt, _)) = args.iter().find_position(|arg| arg == &"--encrypt") { - match args.iter().nth(position_arg_encrypt + 1) { + match args.get(position_arg_encrypt + 1) { Some(mess_to_encrypt) => { match crypto::encrypt(&key, mess_to_encrypt) { Ok(encrypted_mess) => { @@ -139,7 +139,7 @@ fn process_args(key: &String) -> bool { return true } else if let Some((position_arg_decrypt, _)) = args.iter().find_position(|arg| arg == &"--decrypt") { - match args.iter().nth(position_arg_decrypt + 1) { + match args.get(position_arg_decrypt + 1) { Some(cipher_text) => { let cipher_text_decoded = percent_encoding::percent_decode(cipher_text.as_bytes()).decode_utf8().expect("Unable to decode encoded cipher text"); match crypto::decrypt(&key, &cipher_text_decoded) { @@ -155,5 +155,5 @@ fn process_args(key: &String) -> bool { return true } - return false + false }