From 218b8bd1275b06f437b8f21f65dd134d3da9c549 Mon Sep 17 00:00:00 2001 From: ForgQi <34411314+ForgQi@users.noreply.github.com> Date: Mon, 15 Dec 2025 00:43:24 +0800 Subject: [PATCH] Refactor code for conciseness and clarity Simplified conditional logic using Rust's let-else syntax, removed unused imports, and improved code readability in extractor.rs, lib.rs, uploader/line.rs, and stream-gears/server.rs. These changes reduce code complexity and improve maintainability. --- crates/biliup/src/downloader/extractor.rs | 2 +- crates/biliup/src/lib.rs | 7 +++---- crates/biliup/src/uploader/line.rs | 17 +++++++---------- crates/stream-gears/src/server.rs | 3 +-- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/crates/biliup/src/downloader/extractor.rs b/crates/biliup/src/downloader/extractor.rs index fce09b4..c86daaa 100644 --- a/crates/biliup/src/downloader/extractor.rs +++ b/crates/biliup/src/downloader/extractor.rs @@ -58,7 +58,7 @@ impl Site { &mut self, fmt_file_name: &str, segment: Segmentable, - hook: Option>, + _hook: Option>, ) -> downloader::error::Result<()> { let fmt_file_name = fmt_file_name.replace("{title}", &self.title); self.client diff --git a/crates/biliup/src/lib.rs b/crates/biliup/src/lib.rs index 02d4be9..8d393e9 100644 --- a/crates/biliup/src/lib.rs +++ b/crates/biliup/src/lib.rs @@ -12,7 +12,7 @@ pub mod uploader; pub use uploader::bilibili; pub use uploader::credential; -pub async fn retry(mut f: F) -> Result +pub async fn retry(f: F) -> Result where F: FnMut() -> Fut, Fut: Future>, @@ -38,11 +38,10 @@ where match f().await { Err(e) if retries > 0 => { // 如果提供了 should_retry 条件,检查是否应该重试 - if let Some(ref predicate) = should_retry { - if !predicate(&e) { + if let Some(ref predicate) = should_retry + && !predicate(&e) { break Err(e); } - } retries -= 1; let jitter_factor = diff --git a/crates/biliup/src/uploader/line.rs b/crates/biliup/src/uploader/line.rs index ec18697..968ffe7 100644 --- a/crates/biliup/src/uploader/line.rs +++ b/crates/biliup/src/uploader/line.rs @@ -12,7 +12,7 @@ use crate::error::Kind::{Custom, RateLimit}; use crate::uploader::bilibili::{BiliBili, Video}; use crate::uploader::line::upos::Upos; use std::time::Instant; -use tracing::{info, warn}; +use tracing::info; pub mod upos; @@ -56,16 +56,15 @@ impl Parcel { } }; - if video.title.is_none() { - if let Some(filename) = self.video_file.filepath.file_stem().and_then(OsStr::to_str) { + if video.title.is_none() + && let Some(filename) = self.video_file.filepath.file_stem().and_then(OsStr::to_str) { // B站限制分P视频标题不能超过80字符,需要截断 video.title = Some(if filename.chars().count() >= 80 { Video::truncate_title(filename, 80) } else { filename.to_string() }); - } - }; + }; Ok(video) } } @@ -163,9 +162,9 @@ impl Line { let response_text = response.text().await?; // 尝试解析JSON错误响应,检测限流错误(code: 601) - if let Ok(error_json) = serde_json::from_str::(&response_text) { - if let Some(code) = error_json.get("code").and_then(|c| c.as_i64()) { - if code == 601 { + if let Ok(error_json) = serde_json::from_str::(&response_text) + && let Some(code) = error_json.get("code").and_then(|c| c.as_i64()) + && code == 601 { let message = error_json .get("message") .and_then(|m| m.as_str()) @@ -174,8 +173,6 @@ impl Line { // 直接返回限流错误,让调用方决定如何处理 return Err(RateLimit { code, message }); } - } - } return Err(Custom(format!( "Failed to pre_upload from {}", diff --git a/crates/stream-gears/src/server.rs b/crates/stream-gears/src/server.rs index fae1f83..078b1d0 100644 --- a/crates/stream-gears/src/server.rs +++ b/crates/stream-gears/src/server.rs @@ -11,7 +11,7 @@ use biliup_cli::server::core::downloader::DanmakuClient; use biliup_cli::server::core::plugin::{DownloadBase, DownloadPlugin, StreamInfoExt, StreamStatus}; use biliup_cli::server::errors::{AppError, AppResult}; use biliup_cli::server::infrastructure::connection_pool::ConnectionManager; -use biliup_cli::server::infrastructure::context::{Context, PluginContext, Worker}; +use biliup_cli::server::infrastructure::context::{PluginContext, Worker}; use biliup_cli::server::infrastructure::models::StreamerInfo; use biliup_cli::server::infrastructure::repositories; use biliup_cli::server::infrastructure::repositories::get_upload_config; @@ -31,7 +31,6 @@ use pythonize::pythonize; use std::collections::HashMap; use std::net::ToSocketAddrs; use std::ops::Deref; -use std::path::PathBuf; use std::sync::{Arc, LazyLock, RwLock}; use time::macros::format_description; use tracing::{debug, info, warn};