57 lines
1.3 KiB
Rust
57 lines
1.3 KiB
Rust
use core::fmt::Formatter;
|
|
use serde::{
|
|
de::{Error, Unexpected, Visitor},
|
|
Deserialize,
|
|
Deserializer,
|
|
};
|
|
use std::convert::TryInto;
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
pub struct UserResponse {
|
|
pub status: u16,
|
|
pub user: JensmemesUser,
|
|
}
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
pub struct JensmemesUser {
|
|
pub name: String,
|
|
pub tokenhash: String,
|
|
pub userdir: String,
|
|
pub id: String,
|
|
#[serde(deserialize_with = "deserialize_uploads")]
|
|
pub dayuploads: u32,
|
|
}
|
|
|
|
fn deserialize_uploads<'de, D>(de: D) -> Result<u32, D::Error>
|
|
where
|
|
D: Deserializer<'de>,
|
|
{
|
|
struct Vis;
|
|
|
|
impl<'de> Visitor<'de> for Vis {
|
|
type Value = u32;
|
|
|
|
fn expecting(&self, fmt: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
write!(fmt, "a u32")
|
|
}
|
|
|
|
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
where
|
|
E: Error,
|
|
{
|
|
v.parse()
|
|
.map_err(|_| E::invalid_type(Unexpected::Str(v), &"a u32"))
|
|
}
|
|
|
|
/// implementing u64 instead of 32 because it is used as fallback
|
|
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
|
|
where
|
|
E: Error,
|
|
{
|
|
v.try_into()
|
|
.map_err(|_| E::invalid_type(Unexpected::Unsigned(v), &"a u32"))
|
|
}
|
|
}
|
|
|
|
de.deserialize_str(Vis)
|
|
}
|