-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
Due to Spotify API returning floats instead of uints for some fields of the Artist it cannot be parsed. This commit adds deserializing float values into u32 as a workaround. Signed-off-by: Maciej Torhan <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Workaround for Spotify API bug which causes dome uint fields | ||
// being return as floats | ||
|
||
use serde::{Deserialize, Deserializer}; | ||
|
||
pub fn as_u32<'de, D>(deserializer: D) -> Result<u32, D::Error> | ||
Check warning on line 6 in rspotify-model/src/data_type_patcher.rs
|
||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let float_data: f64 = Deserialize::deserialize(deserializer)?; | ||
|
||
let u32_data = float_data as u32; | ||
|
||
Ok(u32_data) | ||
} | ||
|
||
pub fn as_some_u32<'de, D>(deserializer: D) -> Result<Option<u32>, D::Error> | ||
Check warning on line 17 in rspotify-model/src/data_type_patcher.rs
|
||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let float_data: f64 = Deserialize::deserialize(deserializer)?; | ||
|
||
let u32_data = float_data as u32; | ||
|
||
Ok(Some(u32_data)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
//! Image object | ||
mod data_type_patcher { | ||
include!("data_type_patcher.rs"); | ||
} | ||
|
||
pub use data_type_patcher::as_some_u32; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
/// Image object | ||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)] | ||
pub struct Image { | ||
#[serde(deserialize_with = "as_some_u32")] | ||
pub height: Option<u32>, | ||
pub url: String, | ||
#[serde(deserialize_with = "as_some_u32")] | ||
pub width: Option<u32>, | ||
} |