fix: 修复 collection_type 反序列化错误 (#403)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<D>(deserializer: D) -> std::result::Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let v = i32::deserialize(deserializer)?;
|
||||
CollectionType::try_from(v).map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CollectionType> for i32 {
|
||||
fn from(v: CollectionType) -> Self {
|
||||
match v {
|
||||
@@ -26,16 +36,24 @@ impl From<CollectionType> for i32 {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i32> for CollectionType {
|
||||
fn from(v: i32) -> Self {
|
||||
impl TryFrom<i32> for CollectionType {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(v: i32) -> Result<Self, Self::Error> {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user