use std::collections::HashMap;
use serde::de::Error as DeError;
use serde::{Deserialize, Deserializer, Serializer};
#[allow(clippy::trivially_copy_pass_by_ref)]
pub(crate) fn bool_to_int<S>(x: &Option<bool>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match x {
Some(x) => serializer.serialize_some(if *x { &1 } else { &0 }),
None => serializer.serialize_none(),
}
}
#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum IntBool {
Int(u32),
Bool(bool),
}
pub(crate) fn parse_intbool<'de, D>(d: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
{
let x = IntBool::deserialize(d)?;
match x {
IntBool::Int(i) => match i {
0 => Ok(false),
1 => Ok(true),
i => Err(DeError::custom(format!(
"Could not deserialize {} as bool",
i
))),
},
IntBool::Bool(b) => Ok(b),
}
}
#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum StringInt {
Int(u32),
String(String),
}
pub(crate) fn parse_stringint<'de, D>(d: D) -> Result<u32, D::Error>
where
D: Deserializer<'de>,
{
let x = StringInt::deserialize(d)?;
match x {
StringInt::Int(i) => Ok(i),
StringInt::String(s) => match s.parse::<u32>() {
Ok(i) => Ok(i),
Err(_) => Err(DeError::custom(format!(
"Could not deserialize {} as u32",
s
))),
},
}
}
pub(crate) fn parse_hashmap_with_null_values<'de, D>(d: D) -> Result<Option<HashMap<String, String>>, D::Error>
where
D: Deserializer<'de>,
{
let x = Option::<HashMap::<String, Option<String>>>::deserialize(d)?;
Ok(x.map(|hash_map| {
hash_map.into_iter().filter_map(|(key, value)| {
if let Some(value) = value {
Some((key, value))
} else {
None
}
}).collect()
}))
}