From 528a7b6899ed0f468f4a10323a8e1092dc29c760 Mon Sep 17 00:00:00 2001 From: Jade Date: Fri, 17 Feb 2023 22:14:43 +0100 Subject: [PATCH] cleanup of various clippy lints, bad practices and simplificaiton of some code Signed-off-by: Jade --- src/endpoints/create.rs | 15 ++++----------- src/endpoints/edit.rs | 4 ++-- src/endpoints/info.rs | 14 +++++++------- src/endpoints/pasta.rs | 6 +++--- src/endpoints/pastalist.rs | 2 +- src/endpoints/qr.rs | 2 +- src/endpoints/remove.rs | 2 +- src/main.rs | 12 +++--------- src/pasta.rs | 10 ++++++---- src/util/dbio.rs | 10 +++++----- src/util/syntaxhighlighter.rs | 2 +- 11 files changed, 34 insertions(+), 45 deletions(-) diff --git a/src/endpoints/create.rs b/src/endpoints/create.rs index 25fc445..2a627c7 100644 --- a/src/endpoints/create.rs +++ b/src/endpoints/create.rs @@ -37,7 +37,7 @@ pub async fn create( .finish()); } - let mut pastas = data.pastas.lock().unwrap(); + let mut pastas = data.pastas.lock().await; let timenow: i64 = match SystemTime::now().duration_since(UNIX_EPOCH) { Ok(n) => n.as_secs(), @@ -65,18 +65,15 @@ pub async fn create( while let Some(mut field) = payload.try_next().await? { match field.name() { "editable" => { - // while let Some(_chunk) = field.try_next().await? {} new_pasta.editable = true; - continue; } "private" => { - // while let Some(_chunk) = field.try_next().await? {} new_pasta.private = true; - continue; } "expiration" => { while let Some(chunk) = field.try_next().await? { new_pasta.expiration = match std::str::from_utf8(&chunk).unwrap() { + // TODO: customizable times "1min" => timenow + 60, "10min" => timenow + 60 * 10, "1hour" => timenow + 60 * 60, @@ -96,12 +93,12 @@ pub async fn create( } }; } - - continue; } "burn_after" => { while let Some(chunk) = field.try_next().await? { new_pasta.burn_after_reads = match std::str::from_utf8(&chunk).unwrap() { + // TODO: also make customizable + // maybe options in config files, with defaults // give an extra read because the user will be redirected to the pasta page automatically "1" => 2, "10" => 10, @@ -115,8 +112,6 @@ pub async fn create( } }; } - - continue; } "content" => { let mut content = String::from(""); @@ -132,13 +127,11 @@ pub async fn create( String::from("text") }; } - continue; } "syntax-highlight" => { while let Some(chunk) = field.try_next().await? { new_pasta.extension = std::str::from_utf8(&chunk).unwrap().to_string(); } - continue; } "file" => { if ARGS.no_file_upload { diff --git a/src/endpoints/edit.rs b/src/endpoints/edit.rs index 1e0bfff..ba2cfe7 100644 --- a/src/endpoints/edit.rs +++ b/src/endpoints/edit.rs @@ -19,7 +19,7 @@ struct EditTemplate<'a> { #[get("/edit/{id}")] pub async fn get_edit(data: web::Data, id: web::Path) -> HttpResponse { - let mut pastas = data.pastas.lock().unwrap(); + let mut pastas = data.pastas.lock().await; let id = if ARGS.hash_ids { hashid_to_u64(&id).unwrap_or(0) @@ -65,7 +65,7 @@ pub async fn post_edit( to_u64(&id.into_inner()).unwrap_or(0) }; - let mut pastas = data.pastas.lock().unwrap(); + let mut pastas = data.pastas.lock().await; remove_expired(&mut pastas); diff --git a/src/endpoints/info.rs b/src/endpoints/info.rs index 775a04a..9ae4d86 100644 --- a/src/endpoints/info.rs +++ b/src/endpoints/info.rs @@ -9,15 +9,15 @@ use askama::Template; struct Info<'a> { args: &'a Args, pastas: &'a Vec, - status: &'a String, - version_string: &'a String, - message: &'a String, + status: &'a str, + version_string: &'a str, + message: &'a str, } #[get("/info")] pub async fn info(data: web::Data) -> HttpResponse { // get access to the pasta collection - let pastas = data.pastas.lock().unwrap(); + let pastas = data.pastas.lock().await; // todo status report more sophisticated let mut status = "OK"; @@ -32,9 +32,9 @@ pub async fn info(data: web::Data) -> HttpResponse { Info { args: &ARGS, pastas: &pastas, - status: &String::from(status), - version_string: &String::from("1.2.0-20221107"), - message: &String::from(message), + status, + version_string: env!("CARGO_PKG_VERSION"), + message } .render() .unwrap(), diff --git a/src/endpoints/pasta.rs b/src/endpoints/pasta.rs index cf31fa3..d490649 100644 --- a/src/endpoints/pasta.rs +++ b/src/endpoints/pasta.rs @@ -21,7 +21,7 @@ struct PastaTemplate<'a> { #[get("/pasta/{id}")] pub async fn getpasta(data: web::Data, id: web::Path) -> HttpResponse { // get access to the pasta collection - let mut pastas = data.pastas.lock().unwrap(); + let mut pastas = data.pastas.lock().await; let id = if ARGS.hash_ids { hashid_to_u64(&id).unwrap_or(0) @@ -85,7 +85,7 @@ pub async fn getpasta(data: web::Data, id: web::Path) -> HttpR #[get("/url/{id}")] pub async fn redirecturl(data: web::Data, id: web::Path) -> HttpResponse { // get access to the pasta collection - let mut pastas = data.pastas.lock().unwrap(); + let mut pastas = data.pastas.lock().await; let id = if ARGS.hash_ids { hashid_to_u64(&id).unwrap_or(0) @@ -152,7 +152,7 @@ pub async fn redirecturl(data: web::Data, id: web::Path) -> Ht #[get("/raw/{id}")] pub async fn getrawpasta(data: web::Data, id: web::Path) -> String { // get access to the pasta collection - let mut pastas = data.pastas.lock().unwrap(); + let mut pastas = data.pastas.lock().await; let id = if ARGS.hash_ids { hashid_to_u64(&id).unwrap_or(0) diff --git a/src/endpoints/pastalist.rs b/src/endpoints/pastalist.rs index e2536e5..67bdc80 100644 --- a/src/endpoints/pastalist.rs +++ b/src/endpoints/pastalist.rs @@ -21,7 +21,7 @@ pub async fn list(data: web::Data) -> HttpResponse { .finish(); } - let mut pastas = data.pastas.lock().unwrap(); + let mut pastas = data.pastas.lock().await; remove_expired(&mut pastas); diff --git a/src/endpoints/qr.rs b/src/endpoints/qr.rs index c89275e..7cbe6af 100644 --- a/src/endpoints/qr.rs +++ b/src/endpoints/qr.rs @@ -19,7 +19,7 @@ struct QRTemplate<'a> { #[get("/qr/{id}")] pub async fn getqr(data: web::Data, id: web::Path) -> HttpResponse { // get access to the pasta collection - let mut pastas = data.pastas.lock().unwrap(); + let mut pastas = data.pastas.lock().await; let u64_id = if ARGS.hash_ids { hashid_to_u64(&id).unwrap_or(0) diff --git a/src/endpoints/remove.rs b/src/endpoints/remove.rs index 9a26021..936e1e2 100644 --- a/src/endpoints/remove.rs +++ b/src/endpoints/remove.rs @@ -18,7 +18,7 @@ pub async fn remove(data: web::Data, id: web::Path) -> HttpRes .finish(); } - let mut pastas = data.pastas.lock().unwrap(); + let mut pastas = data.pastas.lock().await; let id = if ARGS.hash_ids { hashid_to_u64(&id).unwrap_or(0) diff --git a/src/main.rs b/src/main.rs index cfbcc72..b2c4fef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,10 +11,10 @@ use actix_web::{middleware, web, App, HttpServer}; use actix_web_httpauth::middleware::HttpAuthentication; use chrono::Local; use env_logger::Builder; +use futures::lock::Mutex; use log::LevelFilter; use std::fs; use std::io::Write; -use std::sync::Mutex; pub mod args; pub mod pasta; @@ -68,14 +68,8 @@ async fn main() -> std::io::Result<()> { match fs::create_dir_all("./pasta_data/public") { Ok(dir) => dir, Err(error) => { - log::error!( - "Couldn't create data directory ./pasta_data/public/: {:?}", - error - ); - panic!( - "Couldn't create data directory ./pasta_data/public/: {:?}", - error - ); + log::error!("Couldn't create data directory ./pasta_data/public/: {error:?}"); + panic!("Couldn't create data directory ./pasta_data/public/: {error:?}"); } }; diff --git a/src/pasta.rs b/src/pasta.rs index b7d06f4..428bf8c 100644 --- a/src/pasta.rs +++ b/src/pasta.rs @@ -45,6 +45,8 @@ pub struct Pasta { pub last_read: i64, pub read_count: u64, pub burn_after_reads: u64, + // what types can there be? + // `url`, `text`, pub pasta_type: String, } @@ -96,25 +98,25 @@ impl Pasta { // get seconds since last read and convert it to days let days = ((timenow - self.last_read) / 86400) as u16; if days > 1 { - return format!("{} days ago", days); + return format!("{days} days ago"); }; // it's less than 1 day, let's do hours then let hours = ((timenow - self.last_read) / 3600) as u16; if hours > 1 { - return format!("{} hours ago", hours); + return format!("{hours} hours ago"); }; // it's less than 1 hour, let's do minutes then let minutes = ((timenow - self.last_read) / 60) as u16; if minutes > 1 { - return format!("{} minutes ago", minutes); + return format!("{minutes} minutes ago"); }; // it's less than 1 minute, let's do seconds then let seconds = (timenow - self.last_read) as u16; if seconds > 1 { - return format!("{} seconds ago", seconds); + return format!("{seconds} seconds ago"); }; // it's less than 1 second????? diff --git a/src/util/dbio.rs b/src/util/dbio.rs index 9bf573d..7c6a5e8 100644 --- a/src/util/dbio.rs +++ b/src/util/dbio.rs @@ -14,11 +14,11 @@ pub fn save_to_file(pasta_data: &Vec) { serde_json::to_writer(writer, &pasta_data).expect("Failed to create JSON writer"); } Err(_) => { - log::info!("Database file {} not found!", DATABASE_PATH); + log::info!("Database file {DATABASE_PATH} not found!"); file = File::create(DATABASE_PATH); match file { Ok(_) => { - log::info!("Database file {} created.", DATABASE_PATH); + log::info!("Database file {DATABASE_PATH} created."); save_to_file(pasta_data); } Err(err) => { @@ -27,7 +27,7 @@ pub fn save_to_file(pasta_data: &Vec) { &DATABASE_PATH, &err ); - panic!("Failed to create database file {}: {}!", DATABASE_PATH, err) + panic!("Failed to create database file {DATABASE_PATH}: {err}!") } } } @@ -46,10 +46,10 @@ pub fn load_from_file() -> io::Result> { Ok(data) } Err(_) => { - log::info!("Database file {} not found!", DATABASE_PATH); + log::info!("Database file {DATABASE_PATH} not found!"); save_to_file(&Vec::::new()); - log::info!("Database file {} created.", DATABASE_PATH); + log::info!("Database file {DATABASE_PATH} created."); load_from_file() } } diff --git a/src/util/syntaxhighlighter.rs b/src/util/syntaxhighlighter.rs index 58d2433..bda6474 100644 --- a/src/util/syntaxhighlighter.rs +++ b/src/util/syntaxhighlighter.rs @@ -25,7 +25,7 @@ pub fn html_highlight(text: &str, extension: &str) -> String { let mut highlighted_content2: String = String::from(""); for line in highlighted_content.lines() { - highlighted_content2 += &*format!("{}\n", line); + highlighted_content2 += &*format!("{line}\n"); } // Rewrite colours to ones that are compatible with water.css and both light/dark modes