From 958466818b416dbdafeaeac6c25393d13b74f9fc Mon Sep 17 00:00:00 2001 From: figsoda Date: Tue, 8 Nov 2022 16:30:16 -0500 Subject: [PATCH] apply clippy suggestions --- src/endpoints/create.rs | 4 ++-- src/endpoints/edit.rs | 21 +++++++++------------ src/endpoints/info.rs | 2 +- src/endpoints/pasta.rs | 20 ++++++++++---------- src/endpoints/qr.rs | 4 ++-- src/endpoints/remove.rs | 4 ++-- src/endpoints/static_resources.rs | 2 +- src/pasta.rs | 6 +++--- src/util/animalnumbers.rs | 8 ++++---- src/util/dbio.rs | 2 +- src/util/hashids.rs | 2 +- src/util/syntaxhighlighter.rs | 4 ++-- 12 files changed, 38 insertions(+), 41 deletions(-) diff --git a/src/endpoints/create.rs b/src/endpoints/create.rs index 715007e..d2cb5c3 100644 --- a/src/endpoints/create.rs +++ b/src/endpoints/create.rs @@ -122,7 +122,7 @@ pub async fn create( while let Some(chunk) = field.try_next().await? { content.push_str(std::str::from_utf8(&chunk).unwrap().to_string().as_str()); } - if content.len() > 0 { + if !content.is_empty() { new_pasta.content = content; new_pasta.pasta_type = if is_valid_url(new_pasta.content.as_str()) { @@ -152,7 +152,7 @@ pub async fn create( None => continue, }; - let mut file = match PastaFile::from_unsanitized(&path) { + let mut file = match PastaFile::from_unsanitized(path) { Ok(f) => f, Err(e) => { warn!("Unsafe file name: {e:?}"); diff --git a/src/endpoints/edit.rs b/src/endpoints/edit.rs index b361aec..53d2707 100644 --- a/src/endpoints/edit.rs +++ b/src/endpoints/edit.rs @@ -22,9 +22,9 @@ pub async fn get_edit(data: web::Data, id: web::Path) -> HttpR let mut pastas = data.pastas.lock().unwrap(); let id = if ARGS.hash_ids { - hashid_to_u64(&*id).unwrap_or(0) + hashid_to_u64(&id).unwrap_or(0) } else { - to_u64(&*id.into_inner()).unwrap_or(0) + to_u64(&id.into_inner()).unwrap_or(0) }; remove_expired(&mut pastas); @@ -38,7 +38,7 @@ pub async fn get_edit(data: web::Data, id: web::Path) -> HttpR } return HttpResponse::Ok().content_type("text/html").body( EditTemplate { - pasta: &pasta, + pasta, args: &ARGS, } .render() @@ -65,9 +65,9 @@ pub async fn post_edit( } let id = if ARGS.hash_ids { - hashid_to_u64(&*id).unwrap_or(0) + hashid_to_u64(&id).unwrap_or(0) } else { - to_u64(&*id.into_inner()).unwrap_or(0) + to_u64(&id.into_inner()).unwrap_or(0) }; let mut pastas = data.pastas.lock().unwrap(); @@ -77,20 +77,17 @@ pub async fn post_edit( let mut new_content = String::from(""); while let Some(mut field) = payload.try_next().await? { - match field.name() { - "content" => { - while let Some(chunk) = field.try_next().await? { - new_content = std::str::from_utf8(&chunk).unwrap().to_string(); - } + if field.name() == "content" { + while let Some(chunk) = field.try_next().await? { + new_content = std::str::from_utf8(&chunk).unwrap().to_string(); } - _ => {} } } for (i, pasta) in pastas.iter().enumerate() { if pasta.id == id { if pasta.editable { - pastas[i].content.replace_range(.., &*new_content); + pastas[i].content.replace_range(.., &new_content); save_to_file(&pastas); return Ok(HttpResponse::Found() diff --git a/src/endpoints/info.rs b/src/endpoints/info.rs index b2d8cd2..775a04a 100644 --- a/src/endpoints/info.rs +++ b/src/endpoints/info.rs @@ -17,7 +17,7 @@ struct Info<'a> { #[get("/info")] pub async fn info(data: web::Data) -> HttpResponse { // get access to the pasta collection - let mut pastas = data.pastas.lock().unwrap(); + let pastas = data.pastas.lock().unwrap(); // todo status report more sophisticated let mut status = "OK"; diff --git a/src/endpoints/pasta.rs b/src/endpoints/pasta.rs index 208664c..cf31fa3 100644 --- a/src/endpoints/pasta.rs +++ b/src/endpoints/pasta.rs @@ -6,7 +6,7 @@ use crate::util::animalnumbers::to_u64; use crate::util::hashids::to_u64 as hashid_to_u64; use crate::util::misc::remove_expired; use crate::AppState; -use actix_web::rt::time; + use actix_web::{get, web, HttpResponse}; use askama::Template; use std::time::{SystemTime, UNIX_EPOCH}; @@ -24,9 +24,9 @@ pub async fn getpasta(data: web::Data, id: web::Path) -> HttpR let mut pastas = data.pastas.lock().unwrap(); let id = if ARGS.hash_ids { - hashid_to_u64(&*id).unwrap_or(0) + hashid_to_u64(&id).unwrap_or(0) } else { - to_u64(&*id.into_inner()).unwrap_or(0) + to_u64(&id.into_inner()).unwrap_or(0) }; // remove expired pastas (including this one if needed) @@ -45,7 +45,7 @@ pub async fn getpasta(data: web::Data, id: web::Path) -> HttpR if found { // increment read count - pastas[index].read_count = pastas[index].read_count + 1; + pastas[index].read_count += 1; // save the updated read count save_to_file(&pastas); @@ -88,9 +88,9 @@ pub async fn redirecturl(data: web::Data, id: web::Path) -> Ht let mut pastas = data.pastas.lock().unwrap(); let id = if ARGS.hash_ids { - hashid_to_u64(&*id).unwrap_or(0) + hashid_to_u64(&id).unwrap_or(0) } else { - to_u64(&*id.into_inner()).unwrap_or(0) + to_u64(&id.into_inner()).unwrap_or(0) }; // remove expired pastas (including this one if needed) @@ -110,7 +110,7 @@ pub async fn redirecturl(data: web::Data, id: web::Path) -> Ht if found { // increment read count - pastas[index].read_count = pastas[index].read_count + 1; + pastas[index].read_count += 1; // save the updated read count save_to_file(&pastas); @@ -155,9 +155,9 @@ pub async fn getrawpasta(data: web::Data, id: web::Path) -> St let mut pastas = data.pastas.lock().unwrap(); let id = if ARGS.hash_ids { - hashid_to_u64(&*id).unwrap_or(0) + hashid_to_u64(&id).unwrap_or(0) } else { - to_u64(&*id.into_inner()).unwrap_or(0) + to_u64(&id.into_inner()).unwrap_or(0) }; // remove expired pastas (including this one if needed) @@ -176,7 +176,7 @@ pub async fn getrawpasta(data: web::Data, id: web::Path) -> St if found { // increment read count - pastas[index].read_count = pastas[index].read_count + 1; + pastas[index].read_count += 1; // get current unix time in seconds let timenow: i64 = match SystemTime::now().duration_since(UNIX_EPOCH) { diff --git a/src/endpoints/qr.rs b/src/endpoints/qr.rs index ab4ea32..5d2c86d 100644 --- a/src/endpoints/qr.rs +++ b/src/endpoints/qr.rs @@ -7,8 +7,8 @@ use crate::util::misc::{self, remove_expired}; use crate::AppState; use actix_web::{get, web, HttpResponse}; use askama::Template; -use qrcode_generator::QrCodeEcc; -use std::time::{SystemTime, UNIX_EPOCH}; + + #[derive(Template)] #[template(path = "qr.html", escape = "none")] diff --git a/src/endpoints/remove.rs b/src/endpoints/remove.rs index 0378a00..9a26021 100644 --- a/src/endpoints/remove.rs +++ b/src/endpoints/remove.rs @@ -21,9 +21,9 @@ pub async fn remove(data: web::Data, id: web::Path) -> HttpRes let mut pastas = data.pastas.lock().unwrap(); let id = if ARGS.hash_ids { - hashid_to_u64(&*id).unwrap_or(0) + hashid_to_u64(&id).unwrap_or(0) } else { - to_u64(&*id.into_inner()).unwrap_or(0) + to_u64(&id.into_inner()).unwrap_or(0) }; for (i, pasta) in pastas.iter().enumerate() { diff --git a/src/endpoints/static_resources.rs b/src/endpoints/static_resources.rs index d314416..e82fc6e 100644 --- a/src/endpoints/static_resources.rs +++ b/src/endpoints/static_resources.rs @@ -1,4 +1,4 @@ -use actix_web::{web, App, HttpResponse, HttpServer, Responder}; +use actix_web::{web, HttpResponse, Responder}; use mime_guess::from_path; use rust_embed::RustEmbed; diff --git a/src/pasta.rs b/src/pasta.rs index f20ce7d..04ee45d 100644 --- a/src/pasta.rs +++ b/src/pasta.rs @@ -118,7 +118,7 @@ impl Pasta { }; // it's less than 1 second????? - return String::from("just now"); + String::from("just now") } pub fn last_read_days_ago(&self) -> u16 { @@ -132,7 +132,7 @@ impl Pasta { } as i64; // get seconds since last read and convert it to days - return ((timenow - self.last_read) / 86400) as u16; + ((timenow - self.last_read) / 86400) as u16 } pub fn content_syntax_highlighted(&self) -> String { @@ -144,7 +144,7 @@ impl Pasta { } pub fn content_escaped(&self) -> String { - self.content.replace("`", "\\`").replace("$", "\\$") + self.content.replace('`', "\\`").replace('$', "\\$") } } diff --git a/src/util/animalnumbers.rs b/src/util/animalnumbers.rs index 29e5094..eb003f1 100644 --- a/src/util/animalnumbers.rs +++ b/src/util/animalnumbers.rs @@ -24,7 +24,7 @@ pub fn to_animal_names(mut number: u64) -> String { number -= digit * ANIMAL_NAMES.len().pow(power) as u64; if power > 0 { power -= 1; - } else if power <= 0 || number == 0 { + } else if power == 0 || number == 0 { break; } } @@ -35,12 +35,12 @@ pub fn to_animal_names(mut number: u64) -> String { pub fn to_u64(animal_names: &str) -> Result { let mut result: u64 = 0; - let animals: Vec<&str> = animal_names.split("-").collect(); + let animals: Vec<&str> = animal_names.split('-').collect(); let mut pow = animals.len(); - for i in 0..animals.len() { + for animal in animals { pow -= 1; - let animal_index = ANIMAL_NAMES.iter().position(|&r| r == animals[i]); + let animal_index = ANIMAL_NAMES.iter().position(|&r| r == animal); match animal_index { None => return Err("Failed to convert animal name to u64!"), Some(_) => { diff --git a/src/util/dbio.rs b/src/util/dbio.rs index 12dff06..9bf573d 100644 --- a/src/util/dbio.rs +++ b/src/util/dbio.rs @@ -4,7 +4,7 @@ use std::io::{BufReader, BufWriter}; use crate::Pasta; -static DATABASE_PATH: &'static str = "pasta_data/database.json"; +static DATABASE_PATH: &str = "pasta_data/database.json"; pub fn save_to_file(pasta_data: &Vec) { let mut file = File::create(DATABASE_PATH); diff --git a/src/util/hashids.rs b/src/util/hashids.rs index e9cdb1f..0e53f31 100644 --- a/src/util/hashids.rs +++ b/src/util/hashids.rs @@ -13,6 +13,6 @@ pub fn to_u64(hash_id: &str) -> Result { let ids = HARSH .decode(hash_id) .map_err(|_e| "Failed to decode hash ID")?; - let id = ids.get(0).ok_or("No ID found in hash ID")?; + let id = ids.first().ok_or("No ID found in hash ID")?; Ok(*id) } diff --git a/src/util/syntaxhighlighter.rs b/src/util/syntaxhighlighter.rs index 4b3f2a2..58d2433 100644 --- a/src/util/syntaxhighlighter.rs +++ b/src/util/syntaxhighlighter.rs @@ -11,7 +11,7 @@ pub fn html_highlight(text: &str, extension: &str) -> String { let syntax = ps .find_syntax_by_extension(extension) - .or(Option::from(ps.find_syntax_plain_text())) + .or_else(|| Option::from(ps.find_syntax_plain_text())) .unwrap(); let mut h = HighlightLines::new(syntax, &ts.themes["InspiredGitHub"]); @@ -33,5 +33,5 @@ pub fn html_highlight(text: &str, extension: &str) -> String { highlighted_content2 = highlighted_content2.replace("style=\"color:#183691;\"", "style=\"color:blue;\""); - return highlighted_content2; + highlighted_content2 }