use crate::*;
impl Default for Body {
fn default() -> Self {
Self::Text(EMPTY_STR)
}
}
impl Display for Body {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Text(text) => write!(f, "{}", text.to_string()),
Self::Json(json) => {
let hash_map: HashMap<_, _> = json
.iter()
.map(|entry| (*entry.key(), *entry.value()))
.collect();
write!(
f,
"{}",
serde_json::to_string(&hash_map).unwrap_or_else(|_| String::from("{}"))
)
}
Self::Binary(binary) => write!(f, "{:?}", binary),
}
}
}
impl Serialize for Body {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Self::Text(text) => text.serialize(serializer),
Self::Json(json) => {
let hash_map: HashMap<_, _> = json
.iter()
.map(|entry| (*entry.key(), *entry.value()))
.collect();
hash_map.serialize(serializer)
}
Self::Binary(binary) => binary.serialize(serializer),
}
}
}