Skip to content

Instantly share code, notes, and snippets.

@DefectingCat
Created January 23, 2024 01:15
Show Gist options
  • Save DefectingCat/782c3558a3a5d6b6d871bc758c5513e6 to your computer and use it in GitHub Desktop.
Save DefectingCat/782c3558a3a5d6b6d871bc758c5513e6 to your computer and use it in GitHub Desktop.
deserialize with custom function
fn de_str<'de, D>(deserializer: D) -> Result<i64, D::Error>
where
D: serde::de::Deserializer<'de>,
{
Ok(match Value::deserialize(deserializer)? {
Value::String(s) => s.parse().map_err(de::Error::custom)?,
Value::Number(num) => num.as_i64().ok_or(de::Error::custom("Invalide number"))?,
_ => return Err(de::Error::custom("wrong type")),
})
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BoothExamplesList {
pub total: i64,
#[serde(deserialize_with = "de_str")]
pub per_page: i64,
pub current_page: i64,
pub last_page: i64,
pub data: Vec<BoothExamplesDatum>,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment