apply clippy suggestions

This commit is contained in:
figsoda 2022-11-08 16:30:16 -05:00
parent f41c2eb66b
commit 958466818b
12 changed files with 38 additions and 41 deletions

View file

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

View file

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

View file

@ -17,7 +17,7 @@ struct Info<'a> {
#[get("/info")]
pub async fn info(data: web::Data<AppState>) -> 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";

View file

@ -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<AppState>, id: web::Path<String>) -> 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<AppState>, id: web::Path<String>) -> 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<AppState>, id: web::Path<String>) -> 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<AppState>, id: web::Path<String>) -> 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<AppState>, id: web::Path<String>) -> 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<AppState>, id: web::Path<String>) -> 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) {

View file

@ -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")]

View file

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

View file

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