use std::cmp::Ord;
use std::collections::{BTreeMap, HashMap};
use anyhow::Result;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::MultiMap;
pub fn to_json<T: Serialize>(obj: &T) -> String {
serde_json::to_string_pretty(obj).unwrap()
}
pub fn to_json_terse<T: Serialize>(obj: &T) -> String {
serde_json::to_string(obj).unwrap()
}
pub fn from_json<T: DeserializeOwned>(raw: &[u8]) -> Result<T> {
serde_json::from_slice(raw).map_err(|err| err.into())
}
pub fn from_json_reader<R: std::io::Read, T: DeserializeOwned>(reader: R) -> Result<T> {
serde_json::from_reader(reader).map_err(|err| err.into())
}
pub fn from_binary<T: DeserializeOwned>(raw: &[u8]) -> Result<T> {
bincode::deserialize(raw).map_err(|err| err.into())
}
pub fn from_binary_reader<R: std::io::Read, T: DeserializeOwned>(reader: R) -> Result<T> {
bincode::deserialize_from(reader).map_err(|err| err.into())
}
pub fn serialized_size_bytes<T: Serialize>(obj: &T) -> usize {
bincode::serialized_size(obj).unwrap() as usize
}
pub fn to_binary<T: Serialize>(obj: &T) -> Vec<u8> {
bincode::serialize(obj).unwrap()
}
pub fn serialize_btreemap<S: Serializer, K: Serialize, V: Serialize>(
map: &BTreeMap<K, V>,
s: S,
) -> Result<S::Ok, S::Error> {
map.iter().collect::<Vec<(_, _)>>().serialize(s)
}
pub fn deserialize_btreemap<
'de,
D: Deserializer<'de>,
K: Deserialize<'de> + Ord,
V: Deserialize<'de>,
>(
d: D,
) -> Result<BTreeMap<K, V>, D::Error> {
let vec = <Vec<(K, V)>>::deserialize(d)?;
let mut map = BTreeMap::new();
for (k, v) in vec {
map.insert(k, v);
}
Ok(map)
}
pub fn serialize_hashmap<S: Serializer, K: Serialize + Ord, V: Serialize>(
map: &HashMap<K, V>,
s: S,
) -> Result<S::Ok, S::Error> {
let mut list: Vec<(&K, &V)> = map.iter().collect();
list.sort_by_key(|(k, _)| *k);
list.serialize(s)
}
pub fn deserialize_hashmap<
'de,
D: Deserializer<'de>,
K: Deserialize<'de> + std::hash::Hash + Eq,
V: Deserialize<'de>,
>(
d: D,
) -> Result<HashMap<K, V>, D::Error> {
let vec = <Vec<(K, V)>>::deserialize(d)?;
let mut map = HashMap::new();
for (k, v) in vec {
map.insert(k, v);
}
Ok(map)
}
pub fn serialize_multimap<
S: Serializer,
K: Serialize + Eq + Ord + Clone,
V: Serialize + Eq + Ord + Clone,
>(
map: &MultiMap<K, V>,
s: S,
) -> Result<S::Ok, S::Error> {
map.borrow().iter().collect::<Vec<(_, _)>>().serialize(s)
}
pub fn deserialize_multimap<
'de,
D: Deserializer<'de>,
K: Deserialize<'de> + Eq + Ord + Clone,
V: Deserialize<'de> + Eq + Ord + Clone,
>(
d: D,
) -> Result<MultiMap<K, V>, D::Error> {
let vec = <Vec<(K, Vec<V>)>>::deserialize(d)?;
let mut map = MultiMap::new();
for (key, values) in vec {
for value in values {
map.insert(key.clone(), value);
}
}
Ok(map)
}
pub fn serialize_usize<S: Serializer>(x: &usize, s: S) -> Result<S::Ok, S::Error> {
if let Ok(x) = u32::try_from(*x) {
x.serialize(s)
} else {
Err(serde::ser::Error::custom(format!("{} can't fit in u32", x)))
}
}
pub fn deserialize_usize<'de, D: Deserializer<'de>>(d: D) -> Result<usize, D::Error> {
let x = <u32>::deserialize(d)?;
Ok(x as usize)
}