mirror of
https://github.com/amtoaer/bili-sync.git
synced 2026-09-05 23:49:53 +08:00
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 {
|
impl VideoSource for collection::Model {
|
||||||
fn display_name(&self) -> Cow<'static, str> {
|
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 {
|
fn filter_expr(&self) -> SimpleExpr {
|
||||||
@@ -76,14 +76,14 @@ impl VideoSource for collection::Model {
|
|||||||
CollectionItem {
|
CollectionItem {
|
||||||
sid: self.s_id.to_string(),
|
sid: self.s_id.to_string(),
|
||||||
mid: self.m_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?;
|
let collection_info = collection.get_info().await?;
|
||||||
ensure!(
|
ensure!(
|
||||||
collection_info.sid == self.s_id
|
collection_info.sid == self.s_id
|
||||||
&& collection_info.mid == self.m_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 mismatch: {:?} != {:?}",
|
||||||
collection_info,
|
collection_info,
|
||||||
collection.collection
|
collection.collection
|
||||||
|
|||||||
@@ -10,13 +10,23 @@ use serde_json::Value;
|
|||||||
use crate::bilibili::credential::encoded_query;
|
use crate::bilibili::credential::encoded_query;
|
||||||
use crate::bilibili::{BiliClient, MIXIN_KEY, Validate, VideoInfo};
|
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 {
|
pub enum CollectionType {
|
||||||
Series,
|
Series,
|
||||||
#[default]
|
#[default]
|
||||||
Season,
|
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 {
|
impl From<CollectionType> for i32 {
|
||||||
fn from(v: CollectionType) -> Self {
|
fn from(v: CollectionType) -> Self {
|
||||||
match v {
|
match v {
|
||||||
@@ -26,16 +36,24 @@ impl From<CollectionType> for i32 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<i32> for CollectionType {
|
impl TryFrom<i32> for CollectionType {
|
||||||
fn from(v: i32) -> Self {
|
type Error = anyhow::Error;
|
||||||
|
|
||||||
|
fn try_from(v: i32) -> Result<Self, Self::Error> {
|
||||||
match v {
|
match v {
|
||||||
1 => CollectionType::Series,
|
1 => Ok(CollectionType::Series),
|
||||||
2 => CollectionType::Season,
|
2 => Ok(CollectionType::Season),
|
||||||
_ => panic!("invalid collection type"),
|
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 {
|
impl Display for CollectionType {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
let s = match self {
|
let s = match self {
|
||||||
|
|||||||
Reference in New Issue
Block a user