cleanup of various clippy lints, bad practices and simplificaiton of some code

Signed-off-by: Jade <obsidianical@gmail.com>
This commit is contained in:
Schrottkatze 2023-02-17 22:14:43 +01:00
parent fa67edc8c5
commit 528a7b6899
11 changed files with 34 additions and 45 deletions

View file

@ -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 {

View file

@ -19,7 +19,7 @@ struct EditTemplate<'a> {
#[get("/edit/{id}")]
pub async fn get_edit(data: web::Data<AppState>, id: web::Path<String>) -> 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);

View file

@ -9,15 +9,15 @@ use askama::Template;
struct Info<'a> {
args: &'a Args,
pastas: &'a Vec<Pasta>,
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<AppState>) -> 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<AppState>) -> 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(),

View file

@ -21,7 +21,7 @@ struct PastaTemplate<'a> {
#[get("/pasta/{id}")]
pub async fn getpasta(data: web::Data<AppState>, id: web::Path<String>) -> 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<AppState>, id: web::Path<String>) -> HttpR
#[get("/url/{id}")]
pub async fn redirecturl(data: web::Data<AppState>, id: web::Path<String>) -> 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<AppState>, id: web::Path<String>) -> Ht
#[get("/raw/{id}")]
pub async fn getrawpasta(data: web::Data<AppState>, id: web::Path<String>) -> 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)

View file

@ -21,7 +21,7 @@ pub async fn list(data: web::Data<AppState>) -> HttpResponse {
.finish();
}
let mut pastas = data.pastas.lock().unwrap();
let mut pastas = data.pastas.lock().await;
remove_expired(&mut pastas);

View file

@ -19,7 +19,7 @@ struct QRTemplate<'a> {
#[get("/qr/{id}")]
pub async fn getqr(data: web::Data<AppState>, id: web::Path<String>) -> 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)

View file

@ -18,7 +18,7 @@ pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> 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)

View file

@ -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:?}");
}
};

View file

@ -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?????

View file

@ -14,11 +14,11 @@ pub fn save_to_file(pasta_data: &Vec<Pasta>) {
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<Pasta>) {
&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<Vec<Pasta>> {
Ok(data)
}
Err(_) => {
log::info!("Database file {} not found!", DATABASE_PATH);
log::info!("Database file {DATABASE_PATH} not found!");
save_to_file(&Vec::<Pasta>::new());
log::info!("Database file {} created.", DATABASE_PATH);
log::info!("Database file {DATABASE_PATH} created.");
load_from_file()
}
}

View file

@ -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!("<code-line>{}</code-line>\n", line);
highlighted_content2 += &*format!("<code-line>{line}</code-line>\n");
}
// Rewrite colours to ones that are compatible with water.css and both light/dark modes