From 6e4af47bda9747d566e96234b2293bf3c5a7b70e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=80=E1=B4=8D=E1=B4=9B=E1=B4=8F=E1=B4=80=E1=B4=87?= =?UTF-8?q?=CA=80?= Date: Wed, 23 Jul 2025 22:46:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20collection=5Ftype?= =?UTF-8?q?=20=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96=E9=94=99=E8=AF=AF=20(#4?= =?UTF-8?q?03)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/bili_sync/src/adapter/collection.rs | 6 ++--- crates/bili_sync/src/bilibili/collection.rs | 30 ++++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/crates/bili_sync/src/adapter/collection.rs b/crates/bili_sync/src/adapter/collection.rs index 26d232d..2ffdf5d 100644 --- a/crates/bili_sync/src/adapter/collection.rs +++ b/crates/bili_sync/src/adapter/collection.rs @@ -16,7 +16,7 @@ use crate::bilibili::{BiliClient, Collection, CollectionItem, CollectionType, Vi impl VideoSource for collection::Model { fn display_name(&self) -> Cow<'static, str> { - format!("{}「{}」", CollectionType::from(self.r#type), self.name).into() + format!("{}「{}」", CollectionType::from_expected(self.r#type), self.name).into() } fn filter_expr(&self) -> SimpleExpr { @@ -76,14 +76,14 @@ impl VideoSource for collection::Model { CollectionItem { sid: self.s_id.to_string(), mid: self.m_id.to_string(), - collection_type: CollectionType::from(self.r#type), + collection_type: CollectionType::from_expected(self.r#type), }, ); let collection_info = collection.get_info().await?; ensure!( collection_info.sid == self.s_id && collection_info.mid == self.m_id - && collection_info.collection_type == CollectionType::from(self.r#type), + && collection_info.collection_type == CollectionType::from_expected(self.r#type), "collection info mismatch: {:?} != {:?}", collection_info, collection.collection diff --git a/crates/bili_sync/src/bilibili/collection.rs b/crates/bili_sync/src/bilibili/collection.rs index b8e6c93..75301c0 100644 --- a/crates/bili_sync/src/bilibili/collection.rs +++ b/crates/bili_sync/src/bilibili/collection.rs @@ -10,13 +10,23 @@ use serde_json::Value; use crate::bilibili::credential::encoded_query; use crate::bilibili::{BiliClient, MIXIN_KEY, Validate, VideoInfo}; -#[derive(PartialEq, Eq, Hash, Clone, Debug, Deserialize, Default, Copy)] +#[derive(PartialEq, Eq, Hash, Clone, Debug, Default, Copy)] pub enum CollectionType { Series, #[default] Season, } +impl<'de> serde::Deserialize<'de> for CollectionType { + fn deserialize(deserializer: D) -> std::result::Result + where + D: serde::Deserializer<'de>, + { + let v = i32::deserialize(deserializer)?; + CollectionType::try_from(v).map_err(serde::de::Error::custom) + } +} + impl From for i32 { fn from(v: CollectionType) -> Self { match v { @@ -26,16 +36,24 @@ impl From for i32 { } } -impl From for CollectionType { - fn from(v: i32) -> Self { +impl TryFrom for CollectionType { + type Error = anyhow::Error; + + fn try_from(v: i32) -> Result { match v { - 1 => CollectionType::Series, - 2 => CollectionType::Season, - _ => panic!("invalid collection type"), + 1 => Ok(CollectionType::Series), + 2 => Ok(CollectionType::Season), + v => Err(anyhow!("got invalid collection type {}", v)), } } } +impl CollectionType { + pub fn from_expected(v: i32) -> Self { + Self::try_from(v).expect("invalid collection type") + } +} + impl Display for CollectionType { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let s = match self {