RUFF/src/util.rs

40 lines
1,020 B
Rust
Raw Permalink Normal View History

2021-06-22 16:36:38 +02:00
pub mod mime_serialize {
use mime::Mime;
use serde::{
de::{self, Unexpected, Visitor},
2021-07-26 23:58:04 +02:00
Deserializer,
Serializer,
2021-06-22 16:36:38 +02:00
};
pub fn serialize<S>(data: &Mime, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
2022-01-23 15:16:51 +01:00
serializer.serialize_str(data.as_ref())
2021-06-22 16:36:38 +02:00
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Mime, D::Error>
where
D: Deserializer<'de>,
{
struct Vis;
impl<'de> Visitor<'de> for Vis {
type Value = Mime;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a MIME type")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
v.parse()
.map_err(|_| de::Error::invalid_value(Unexpected::Str(v), &Vis))
}
}
deserializer.deserialize_str(Vis)
}
}