From 51512a19e4fed541b071696fd51fb4cc362efb6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sn=C3=ADda=C5=88ov=C3=BD=20Mistr?= Date: Mon, 11 Dec 2023 16:59:32 +0100 Subject: [PATCH] cargo clippy --- src/db/account.rs | 4 +- src/db/ban.rs | 2 +- src/db/board.rs | 8 ++-- src/db/cache.rs | 10 ++--- src/db/post.rs | 13 +++--- src/error.rs | 10 ++--- src/files.rs | 14 +++--- src/filters.rs | 2 +- src/main.rs | 2 +- src/markup.rs | 50 ++++++++++----------- src/web/actions/create_post.rs | 16 +++---- src/web/actions/mod.rs | 8 ++-- src/web/actions/report_posts.rs | 6 +-- src/web/actions/staff_post_actions.rs | 16 +++---- src/web/actions/user_post_actions.rs | 6 +-- src/web/board.rs | 8 ++-- src/web/board_catalog.rs | 2 +- src/web/index.rs | 2 +- src/web/login.rs | 2 +- src/web/mod.rs | 4 +- src/web/overboard.rs | 8 ++-- src/web/overboard_catalog.rs | 2 +- src/web/staff/account.rs | 2 +- src/web/staff/accounts.rs | 2 +- src/web/staff/actions/add_banners.rs | 4 +- src/web/staff/actions/remove_accounts.rs | 2 +- src/web/staff/actions/remove_bans.rs | 2 +- src/web/staff/actions/remove_boards.rs | 2 +- src/web/staff/actions/update_boards.rs | 2 +- src/web/staff/actions/update_permissions.rs | 24 +++++----- src/web/staff/banners.rs | 2 +- src/web/staff/bans.rs | 2 +- src/web/staff/board_config.rs | 2 +- src/web/staff/boards.rs | 2 +- src/web/staff/permissions.rs | 2 +- src/web/staff/reports.rs | 2 +- src/web/tcx.rs | 11 +++-- src/web/thread.rs | 2 +- 38 files changed, 125 insertions(+), 135 deletions(-) diff --git a/src/db/account.rs b/src/db/account.rs index c6bdf2e..4b000e8 100755 --- a/src/db/account.rs +++ b/src/db/account.rs @@ -18,7 +18,7 @@ impl Account { .await?; ctx.cache() - .json_set(format!("accounts:{}", username), ".", &account) + .json_set(format!("accounts:{username}"), ".", &account) .await?; Ok(account) @@ -27,7 +27,7 @@ impl Account { pub async fn read(ctx: &Ctx, username: String) -> Result, NekrochanError> { let account: Option = ctx .cache() - .json_get(format!("accounts:{}", username), ".") + .json_get(format!("accounts:{username}"), ".") .await?; let account = match account { diff --git a/src/db/ban.rs b/src/db/ban.rs index 0bc1f4c..3ae2a04 100755 --- a/src/db/ban.rs +++ b/src/db/ban.rs @@ -77,7 +77,7 @@ impl Ban { let mut ban_map = HashMap::new(); - for ban in bans.into_iter() { + for ban in bans { let board = ban.board.clone(); ban_map.insert(board, ban); diff --git a/src/db/board.rs b/src/db/board.rs index 9e19396..86dac43 100755 --- a/src/db/board.rs +++ b/src/db/board.rs @@ -81,7 +81,7 @@ impl Board { } pub async fn read(ctx: &Ctx, id: String) -> Result, NekrochanError> { - let board: Option = ctx.cache().json_get(format!("boards:{}", id), ".").await?; + let board: Option = ctx.cache().json_get(format!("boards:{id}"), ".").await?; let board = match board { Some(json) => Some(serde_json::from_str(&json)?), @@ -95,7 +95,7 @@ impl Board { let mut boards = Vec::new(); let ids: Vec = ctx.cache().lrange("board_ids", 0, -1).await?; - for id in ids.into_iter() { + for id in ids { if let Some(board) = Self::read(ctx, id).await? { boards.push(board); } @@ -108,7 +108,7 @@ impl Board { let mut boards = HashMap::new(); let ids: Vec = ctx.cache().lrange("board_ids", 0, -1).await?; - for id in ids.into_iter() { + for id in ids { if let Some(board) = Self::read(ctx, id.clone()).await? { boards.insert(id, board); } @@ -210,7 +210,7 @@ impl Board { pub fn random_banner(&self) -> Option { self.banners .choose(&mut thread_rng()) - .map(|banner| banner.to_owned()) + .map(std::clone::Clone::clone) } pub fn thread_captcha(&self) -> Option<(String, String)> { diff --git a/src/db/cache.rs b/src/db/cache.rs index 3a1f96f..1500f06 100644 --- a/src/db/cache.rs +++ b/src/db/cache.rs @@ -13,7 +13,7 @@ pub async fn init_cache(ctx: &Ctx) -> Result<(), Error> { .fetch_all(ctx.db()) .await?; - for account in accounts.iter() { + for account in &accounts { ctx.cache() .json_set(format!("accounts:{}", account.username), ".", &account) .await?; @@ -21,7 +21,7 @@ pub async fn init_cache(ctx: &Ctx) -> Result<(), Error> { let boards: Vec = query_as("SELECT * FROM boards").fetch_all(ctx.db()).await?; - for board in boards.iter() { + for board in &boards { ctx.cache() .json_set(format!("boards:{}", board.id), ".", board) .await?; @@ -39,7 +39,7 @@ pub async fn init_cache(ctx: &Ctx) -> Result<(), Error> { ctx.cache().set("total_threads", 0).await?; - for board in boards.iter() { + for board in &boards { let (thread_count,): (i64,) = query_as(&format!( "SELECT COUNT(id) FROM posts_{} WHERE thread IS NULL", board.id @@ -53,10 +53,10 @@ pub async fn init_cache(ctx: &Ctx) -> Result<(), Error> { .await?; } - for board in boards.iter() { + for board in &boards { let posts = Post::read_all(ctx, board.id.clone()).await?; - for post in posts.into_iter() { + for post in posts { let ip_key = format!("by_ip:{}", post.ip); let content_key = format!("by_content:{}", digest(post.content_nomarkup)); diff --git a/src/db/post.rs b/src/db/post.rs index efe87ba..a521b0c 100755 --- a/src/db/post.rs +++ b/src/db/post.rs @@ -72,7 +72,7 @@ impl Post { .await?; } - let ip_key = format!("by_ip:{}", ip); + let ip_key = format!("by_ip:{ip}"); let content_key = format!("by_content:{}", digest(post.content_nomarkup.as_bytes())); let member = format!("{}/{}", board.id, post.id); let score = post.created.timestamp_micros(); @@ -143,10 +143,9 @@ impl Post { pub async fn read_board_catalog(ctx: &Ctx, board: String) -> Result, NekrochanError> { let posts = query_as(&format!( - r#"SELECT * FROM posts_{} + r#"SELECT * FROM posts_{board} WHERE thread IS NULL - ORDER BY sticky DESC, bumped DESC"#, - board + ORDER BY sticky DESC, bumped DESC"# )) .fetch_all(ctx.db()) .await?; @@ -234,7 +233,7 @@ impl Post { } pub async fn read_all(ctx: &Ctx, board: String) -> Result, NekrochanError> { - let posts = query_as(&format!("SELECT * FROM posts_{}", board)) + let posts = query_as(&format!("SELECT * FROM posts_{board}")) .fetch_all(ctx.db()) .await?; @@ -333,7 +332,7 @@ impl Post { .fetch_all(ctx.db()) .await?; - for post in to_be_deleted.iter() { + for post in &to_be_deleted { let id = post.id; let url = post.post_url(); @@ -443,7 +442,7 @@ async fn delete_old_threads(ctx: &Ctx, board: &Board) -> Result<(), NekrochanErr .fetch_all(ctx.db()) .await?; - for thread in old_threads.iter() { + for thread in &old_threads { thread.delete(ctx).await?; } diff --git a/src/error.rs b/src/error.rs index a87dd04..2826d49 100755 --- a/src/error.rs +++ b/src/error.rs @@ -148,12 +148,12 @@ impl From for NekrochanError { None => false, }; - if !overboard_err { - error!("{e:#?}"); - - Self::InternalError - } else { + if overboard_err { Self::OverboardError + } else { + error!("{e:#?}"); + + Self::InternalError } } } diff --git a/src/files.rs b/src/files.rs index fe5e5ef..2242dcf 100755 --- a/src/files.rs +++ b/src/files.rs @@ -164,9 +164,8 @@ async fn process_image( )); } - let thumb_name = match thumb_name { - Some(thumb_name) => thumb_name, - None => return Ok((width, height)), + let Some(thumb_name) = thumb_name else { + return Ok((width, height)); }; let thumb_w = if width > cfg.files.thumb_size { @@ -255,9 +254,8 @@ async fn process_video( )); } - let thumb_name = match thumb_name { - Some(thumb_name) => thumb_name, - None => return Ok((width, height)), + let Some(thumb_name) = thumb_name else { + return Ok((width, height)); }; let thumb_size = cfg.files.thumb_size; @@ -302,14 +300,14 @@ pub async fn cleanup_files(ctx: &Ctx) -> Result<(), Error> { let boards = Board::read_all(ctx).await?; - for board in boards.into_iter() { + for board in boards { for file in board.banners.0 { keep.insert(format!("{}.{}", file.timestamp, file.format)); } let posts = Post::read_all(ctx, board.id.clone()).await?; - for post in posts.into_iter() { + for post in posts { for file in post.files.0 { keep.insert(format!("{}.{}", file.timestamp, file.format)); diff --git a/src/filters.rs b/src/filters.rs index fc7771d..97081c6 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -119,7 +119,7 @@ pub fn add_yous( format!( "{}{}", quote, - if yous.contains(&format!("{}/{}", board, id)) { + if yous.contains(&format!("{board}/{id}")) { " (Ty)" } else { "" diff --git a/src/main.rs b/src/main.rs index 257e2bb..42ed7e9 100755 --- a/src/main.rs +++ b/src/main.rs @@ -147,7 +147,7 @@ where error_message, }; - let res = template_response(template)?; + let res = template_response(&template)?; let res = ServiceResponse::new(req, res).map_into_right_body(); Ok(ErrorHandlerResponse::Response(res)) diff --git a/src/markup.rs b/src/markup.rs index 64b8660..afefdcd 100644 --- a/src/markup.rs +++ b/src/markup.rs @@ -35,9 +35,8 @@ pub fn parse_name( anon_name: &str, name: &str, ) -> Result<(String, Option, Option), NekrochanError> { - let captures = match NAME_REGEX.captures(name)? { - Some(captures) => captures, - None => return Ok((anon_name.to_owned(), None, None)), + let Some(captures) = NAME_REGEX.captures(name)? else { + return Ok((anon_name.to_owned(), None, None)); }; let name = match captures.get(1) { @@ -77,21 +76,13 @@ pub fn parse_name( return Ok((name, tripcode, None)); } - fn capcode_fallback(owner: bool) -> Option { - if owner { - Some("Admin".into()) - } else { - Some("Uklízeč".into()) - } - } - let capcode = match captures.get(5) { Some(_) => match captures.get(6) { Some(capcode) => { let capcode: String = capcode.as_str().trim().into(); if capcode.is_empty() { - capcode_fallback(perms.owner()) + Some(capcode_fallback(perms.owner())) } else { if capcode.len() > 32 { return Err(NekrochanError::CapcodeFormatError); @@ -100,7 +91,7 @@ pub fn parse_name( Some(capcode) } } - None => capcode_fallback(perms.owner()), + None => Some(capcode_fallback(perms.owner())), }, None => None, }; @@ -108,6 +99,14 @@ pub fn parse_name( Ok((name, tripcode, capcode)) } +fn capcode_fallback(owner: bool) -> String { + if owner { + "Admin".into() + } else { + "Uklízeč".into() + } +} + pub async fn markup( ctx: &Ctx, board: &String, @@ -119,15 +118,15 @@ pub async fn markup( let text = QUOTE_REGEX.replace_all(&text, |captures: &Captures| { let id_raw = &captures[1]; - let id = match id_raw.parse() { - Ok(id) => id, - Err(_) => return format!(">>{id_raw}"), + + let Ok(id) = id_raw.parse() else { + return format!(">>{id_raw}"); }; let post = quoted_posts.get(&id); - match post { - Some(post) => format!( + if let Some(post) = post { + format!( ">>{}{}", post.post_url(), post.id, @@ -136,8 +135,9 @@ pub async fn markup( } else { "" } - ), - None => format!(">>{id}"), + ) + } else { + format!(">>{id}") } }); @@ -184,10 +184,7 @@ async fn get_quoted_posts( for quote in QUOTE_REGEX.captures_iter(text) { let id_raw = "e.unwrap()[1]; - let id = match id_raw.parse() { - Ok(id) => id, - Err(_) => continue, - }; + let Ok(id) = id_raw.parse() else { continue }; quoted_ids.push(id); } @@ -198,13 +195,12 @@ async fn get_quoted_posts( let in_list = quoted_ids .iter() - .map(|id| id.to_string()) + .map(std::string::ToString::to_string) .collect::>() .join(","); let quoted_posts = query_as(&format!( - "SELECT * FROM posts_{} WHERE id IN ({})", - board, in_list + "SELECT * FROM posts_{board} WHERE id IN ({in_list})" )) .fetch_all(ctx.db()) .await? diff --git a/src/web/actions/create_post.rs b/src/web/actions/create_post.rs index 3d8f7c4..a0c819a 100644 --- a/src/web/actions/create_post.rs +++ b/src/web/actions/create_post.rs @@ -133,7 +133,9 @@ pub async fn create_post( let email_raw = form.email.trim(); - let email = if !email_raw.is_empty() { + let email = if email_raw.is_empty() { + None + } else { if email_raw.len() > 256 { return Err(NekrochanError::EmailFormatError); } @@ -147,8 +149,6 @@ pub async fn create_post( } Some(email_raw.into()) - } else { - None }; let content_nomarkup = form.content.0.trim().to_owned(); @@ -177,7 +177,7 @@ pub async fn create_post( let mut files = Vec::new(); - for file in form.files.into_iter() { + for file in form.files { if file.size == 0 { continue; } @@ -219,10 +219,10 @@ pub async fn create_post( ) .await?; - let ts = thread - .as_ref() - .map(|thread| thread.created.timestamp_micros()) - .unwrap_or_else(|| post.created.timestamp_micros()); + let ts = thread.as_ref().map_or_else( + || post.created.timestamp_micros(), + |thread| thread.created.timestamp_micros(), + ); let hash_input = format!("{}:{}:{}", ip, ts, ctx.cfg.secrets.user_id); let user_hash = digest(hash_input); diff --git a/src/web/actions/mod.rs b/src/web/actions/mod.rs index a6a58d5..f5a27e6 100644 --- a/src/web/actions/mod.rs +++ b/src/web/actions/mod.rs @@ -4,8 +4,8 @@ use super::tcx::TemplateCtx; use crate::{ctx::Ctx, db::models::Post}; pub mod create_post; -pub mod staff_post_actions; pub mod report_posts; +pub mod staff_post_actions; pub mod user_post_actions; #[derive(Template)] @@ -18,8 +18,8 @@ pub struct ActionTemplate { pub async fn get_posts_from_ids(ctx: &Ctx, ids: Vec) -> Vec { let mut posts = Vec::new(); - for id in ids.into_iter() { - if let Some((board, id)) = parse_id(id) { + for id in ids { + if let Some((board, id)) = parse_id(&id) { if let Ok(Some(post)) = Post::read(ctx, board, id).await { posts.push(post); } @@ -29,7 +29,7 @@ pub async fn get_posts_from_ids(ctx: &Ctx, ids: Vec) -> Vec { posts } -fn parse_id(id: String) -> Option<(String, i32)> { +fn parse_id(id: &str) -> Option<(String, i32)> { let (board, id) = id.split_once('/')?; let board = board.to_owned(); let id = id.parse().ok()?; diff --git a/src/web/actions/report_posts.rs b/src/web/actions/report_posts.rs index db4a2dc..a962dc3 100644 --- a/src/web/actions/report_posts.rs +++ b/src/web/actions/report_posts.rs @@ -46,7 +46,7 @@ pub async fn report_posts( let reason = form.report_reason.trim(); - for post in posts.iter() { + for post in &posts { let board = &boards[&post.board]; if bans.contains_key(&Some(board.id.clone())) @@ -60,7 +60,7 @@ pub async fn report_posts( writeln!( &mut response, "[Chyba] {}", - NekrochanError::BoardLockError(board.id.to_owned()) + NekrochanError::BoardLockError(board.id.clone()) ) .ok(); @@ -102,5 +102,5 @@ pub async fn report_posts( let template = ActionTemplate { tcx, response }; - template_response(template) + template_response(&template) } diff --git a/src/web/actions/staff_post_actions.rs b/src/web/actions/staff_post_actions.rs index e8f9105..2e01e43 100644 --- a/src/web/actions/staff_post_actions.rs +++ b/src/web/actions/staff_post_actions.rs @@ -55,7 +55,7 @@ pub async fn staff_post_actions( let mut locks_toggled = 0; let mut bans_issued = 0; - for post in posts.iter() { + for post in &posts { if (form.remove_posts.is_some() || form.remove_files.is_some() || form.toggle_spoiler.is_some() @@ -100,7 +100,7 @@ pub async fn staff_post_actions( let mut already_banned = HashSet::new(); - for post in posts.into_iter() { + for post in posts { if let (Some(_), Some(ban_reason), Some(ban_duration), Some(ban_range)) = ( form.ban_user.clone(), form.ban_reason.clone(), @@ -126,14 +126,12 @@ pub async fn staff_post_actions( let prefix = if post.ip.is_ipv4() { match ban_range.as_str() { - "ip" => 32, "lan" => 24, "isp" => 16, _ => 32, } } else { match ban_range.as_str() { - "ip" => 128, "lan" => 48, "isp" => 24, _ => 128, @@ -144,10 +142,10 @@ pub async fn staff_post_actions( let reason = ban_reason.trim().into(); let appealable = form.unappealable_ban.is_none(); - let expires = if ban_duration != 0 { - Some(Utc::now() + Duration::days(ban_duration)) - } else { + let expires = if ban_duration == 0 { None + } else { + Some(Utc::now() + Duration::days(ban_duration)) }; Ban::create(&ctx, account, board, ip_range, reason, appealable, expires).await?; @@ -156,7 +154,7 @@ pub async fn staff_post_actions( "{}\n\n==(UŽIVATEL BYL ZA TENTO PŘÍSPĚVEK ZABANOVÁN)==", post.content_nomarkup ); - + let content = format!( "{}\n\n(UŽIVATEL BYL ZA TENTO PŘÍSPĚVEK ZABANOVÁN)", post.content @@ -215,5 +213,5 @@ pub async fn staff_post_actions( let template = ActionTemplate { tcx, response }; - template_response(template) + template_response(&template) } diff --git a/src/web/actions/user_post_actions.rs b/src/web/actions/user_post_actions.rs index 83ef9e2..f2d92a6 100644 --- a/src/web/actions/user_post_actions.rs +++ b/src/web/actions/user_post_actions.rs @@ -52,7 +52,7 @@ pub async fn user_post_actions( let mut files_removed = 0; let mut spoilers_toggled = 0; - for post in posts.iter() { + for post in &posts { let board = &boards[&post.board]; if bans.contains_key(&Some(board.id.clone())) @@ -66,7 +66,7 @@ pub async fn user_post_actions( writeln!( &mut response, "[Chyba] {}", - NekrochanError::BoardLockError(board.id.to_owned()) + NekrochanError::BoardLockError(board.id.clone()) ) .ok(); @@ -131,5 +131,5 @@ pub async fn user_post_actions( let template = ActionTemplate { tcx, response }; - template_response(template) + template_response(&template) } diff --git a/src/web/board.rs b/src/web/board.rs index 0d5d60c..d047743 100755 --- a/src/web/board.rs +++ b/src/web/board.rs @@ -49,17 +49,17 @@ pub async fn board( .cache() .get(format!("board_threads:{}", board.id)) .await?; - let page = query.map(|q| q.page).unwrap_or(1); + let page = query.map_or(1, |q| q.page); let pages = paginate(board.config.0.page_size, count); check_page(page, pages, board.config.0.page_count)?; let mut threads = Vec::new(); - for thread in Post::read_board_page(&ctx, &board, page).await?.into_iter() { + for thread in Post::read_board_page(&ctx, &board, page).await? { let replies = thread.read_replies(&ctx).await?; - threads.push((thread, replies)) + threads.push((thread, replies)); } let template = BoardTemplate { @@ -70,5 +70,5 @@ pub async fn board( pages, }; - template_response(template) + template_response(&template) } diff --git a/src/web/board_catalog.rs b/src/web/board_catalog.rs index 1f3fb22..c901697 100644 --- a/src/web/board_catalog.rs +++ b/src/web/board_catalog.rs @@ -42,5 +42,5 @@ pub async fn board_catalog( threads, }; - template_response(template) + template_response(&template) } diff --git a/src/web/index.rs b/src/web/index.rs index bda31ec..e3339d2 100755 --- a/src/web/index.rs +++ b/src/web/index.rs @@ -20,5 +20,5 @@ pub async fn index(ctx: Data, req: HttpRequest) -> Result, req: HttpRequest) -> Result Result { let tcx = TemplateCtx::new(ctx, req).await?; - template_response(BannedTemplate { tcx, ban }) + template_response(&BannedTemplate { tcx, ban }) } -pub fn template_response(template: T) -> Result +pub fn template_response(template: &T) -> Result where T: Template, { diff --git a/src/web/overboard.rs b/src/web/overboard.rs index 7bb37ae..edf9e5d 100644 --- a/src/web/overboard.rs +++ b/src/web/overboard.rs @@ -42,17 +42,17 @@ pub async fn overboard( let tcx = TemplateCtx::new(&ctx, &req).await?; let count = ctx.cache().get("total_threads").await?; - let page = query.map(|q| q.page).unwrap_or(1); + let page = query.map_or(1, |q| q.page); let pages = paginate(15, count); check_page(page, pages, None)?; let mut threads = Vec::new(); - for thread in Post::read_overboard_page(&ctx, page).await?.into_iter() { + for thread in Post::read_overboard_page(&ctx, page).await? { let replies = thread.read_replies(&ctx).await?; - threads.push((thread, replies)) + threads.push((thread, replies)); } let template = OverboardTemplate { @@ -63,5 +63,5 @@ pub async fn overboard( pages, }; - template_response(template) + template_response(&template) } diff --git a/src/web/overboard_catalog.rs b/src/web/overboard_catalog.rs index 5758167..3cfde6e 100644 --- a/src/web/overboard_catalog.rs +++ b/src/web/overboard_catalog.rs @@ -26,5 +26,5 @@ pub async fn overboard_catalog( let template = OverboardCatalogTemplate { tcx, threads }; - template_response(template) + template_response(&template) } diff --git a/src/web/staff/account.rs b/src/web/staff/account.rs index 0924bea..fcf4133 100755 --- a/src/web/staff/account.rs +++ b/src/web/staff/account.rs @@ -24,5 +24,5 @@ pub async fn account(ctx: Data, req: HttpRequest) -> Result, req: HttpRequest) -> Result, req: HttpRequest) -> Result, req: HttpRequest) -> Result, req: HttpRequest) -> Result Result { - let cfg = ctx.cfg.to_owned(); + let cfg = ctx.cfg.clone(); let boards = ctx.cache().lrange("board_ids", 0, -1).await?; let account = account_from_auth_opt(ctx, req).await?; @@ -103,11 +103,10 @@ pub fn ip_from_req(req: &HttpRequest) -> Result<(IpAddr, String), NekrochanError // .map_err(|_| NekrochanError::HeaderError("X-Real-IP"))? // .parse::()?; - let country = req - .headers() - .get("X-Country-Code") - .map(|hdr| hdr.to_str().unwrap_or("xx").to_ascii_lowercase()) - .unwrap_or_else(|| "xx".into()); + let country = req.headers().get("X-Country-Code").map_or_else( + || "xx".into(), + |hdr| hdr.to_str().unwrap_or("xx").to_ascii_lowercase(), + ); Ok((ip, country)) } diff --git a/src/web/thread.rs b/src/web/thread.rs index 5647fe9..c1533c9 100644 --- a/src/web/thread.rs +++ b/src/web/thread.rs @@ -55,5 +55,5 @@ pub async fn thread( replies, }; - template_response(template) + template_response(&template) }