From ec066a85dbf667dd0cfe520987c0a4369ffc620e Mon Sep 17 00:00:00 2001 From: amtoaer Date: Sun, 31 Mar 2024 19:29:48 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=8A=A0=E5=85=A5=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/status.rs | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/core/status.rs b/src/core/status.rs index 3278b84..d7f12e3 100644 --- a/src/core/status.rs +++ b/src/core/status.rs @@ -67,6 +67,17 @@ impl Status { } } + /// 根据 mask 设置状态,如果 mask 为 false,则清除对应的状态 + fn set_mask(&mut self, mask: &[bool]) { + assert!(mask.len() < 10, "u32 can only store 10 status"); + for (i, &m) in mask.iter().enumerate() { + if !m { + self.clear(i); + self.set_flag(false); + } + } + } + fn plus_one(&mut self, offset: usize) { self.0 += 1 << (3 * offset); } @@ -75,6 +86,10 @@ impl Status { self.0 |= STATUS_OK << (3 * offset); } + fn clear(&mut self, offset: usize) { + self.0 &= !(STATUS_OK << (3 * offset)); + } + fn get_status(&self, offset: usize) -> u32 { let helper = !0u32; (self.0 & (helper << (offset * 3)) & (helper >> (32 - 3 * offset - 3))) >> (offset * 3) @@ -82,7 +97,7 @@ impl Status { fn display_status(status: u32) -> String { if status < STATUS_MAX_RETRY { - format!("retry {} times", status) + format!("failed {} times", status) } else if status == STATUS_OK { "ok".to_string() } else { @@ -97,7 +112,7 @@ impl From for u32 { } } -/// 从前到后分别表示:视频封面、视频信息、Up 主头像、Up 主信息 +/// 从前到后分别表示:视频封面、视频信息、Up 主头像、Up 主信息、分 P 下载 #[derive(Clone)] pub struct VideoStatus(Status); @@ -106,16 +121,18 @@ impl VideoStatus { Self(Status::new(status)) } + pub fn set_mask(&mut self, clear: &[bool]) { + assert!(clear.len() == 5, "VideoStatus should have 5 status"); + self.0.set_mask(clear) + } + pub fn should_run(&self) -> Vec { - self.0.should_run(4) + self.0.should_run(5) } pub fn update_status(&mut self, result: &[Result<()>]) { - assert!( - result.len() >= 4, - "VideoStatus should have 4 status, more status will be ignored" - ); - self.0.update_status(&result[..4]) + assert!(result.len() == 5, "VideoStatus should have 5 status"); + self.0.update_status(result) } } @@ -123,7 +140,7 @@ impl fmt::Display for VideoStatus { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "Video Cover: {}, Page: {}, Video NFO: {}, Up Avatar: {}, Up NFO: {}", + "Video Cover: {}, Video NFO: {}, Up Avatar: {}, Up NFO: {}, Page Download: {}", Status::display_status(self.0.get_status(0)), Status::display_status(self.0.get_status(1)), Status::display_status(self.0.get_status(2)), @@ -148,6 +165,11 @@ impl PageStatus { Self(Status::new(status)) } + pub fn set_mask(&mut self, clear: &[bool]) { + assert!(clear.len() == 3, "PageStatus should have 3 status"); + self.0.set_mask(clear) + } + pub fn should_run(&self) -> Vec { self.0.should_run(3) }