1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-25 00:12:59 +01:00
actix-extras/src/de.rs

451 lines
13 KiB
Rust
Raw Normal View History

2018-04-14 01:02:01 +02:00
use serde::de::{self, Deserializer, Error as DeError, Visitor};
use std::borrow::Cow;
use std::convert::AsRef;
2018-04-14 01:02:01 +02:00
use std::slice::Iter;
2018-03-27 08:10:31 +02:00
2018-03-27 00:58:30 +02:00
use httprequest::HttpRequest;
macro_rules! unsupported_type {
($trait_fn:ident, $name:expr) => {
fn $trait_fn<V>(self, _: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>
{
Err(de::value::Error::custom(concat!("unsupported type: ", $name)))
}
};
}
macro_rules! parse_single_value {
($trait_fn:ident, $visit_fn:ident, $tp:tt) => {
fn $trait_fn<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>
{
if self.req.match_info().len() != 1 {
Err(de::value::Error::custom(
format!("wrong number of parameters: {} expected 1",
self.req.match_info().len()).as_str()))
} else {
let v = self.req.match_info()[0].parse().map_err(
|_| de::value::Error::custom(
format!("can not parse {:?} to a {}",
&self.req.match_info()[0], $tp)))?;
visitor.$visit_fn(v)
}
}
}
}
pub struct PathDeserializer<'de, S: 'de> {
2018-04-14 01:02:01 +02:00
req: &'de HttpRequest<S>,
2018-03-27 00:58:30 +02:00
}
impl<'de, S: 'de> PathDeserializer<'de, S> {
pub fn new(req: &'de HttpRequest<S>) -> Self {
2018-04-29 18:09:08 +02:00
PathDeserializer { req }
}
}
2018-04-14 01:02:01 +02:00
impl<'de, S: 'de> Deserializer<'de> for PathDeserializer<'de, S> {
2018-03-27 00:58:30 +02:00
type Error = de::value::Error;
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
V: Visitor<'de>,
2018-03-27 00:58:30 +02:00
{
2018-04-14 01:02:01 +02:00
visitor.visit_map(ParamsDeserializer {
params: self.req.match_info().iter(),
current: None,
})
2018-03-27 00:58:30 +02:00
}
2018-04-14 01:02:01 +02:00
fn deserialize_struct<V>(
2018-04-29 07:55:47 +02:00
self, _: &'static str, _: &'static [&'static str], visitor: V,
2018-04-14 01:02:01 +02:00
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
2018-03-27 00:58:30 +02:00
{
self.deserialize_map(visitor)
}
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
V: Visitor<'de>,
2018-03-27 00:58:30 +02:00
{
visitor.visit_unit()
}
2018-04-14 01:02:01 +02:00
fn deserialize_unit_struct<V>(
2018-04-29 07:55:47 +02:00
self, _: &'static str, visitor: V,
2018-04-14 01:02:01 +02:00
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
2018-03-27 00:58:30 +02:00
{
self.deserialize_unit(visitor)
}
2018-04-14 01:02:01 +02:00
fn deserialize_newtype_struct<V>(
2018-04-29 07:55:47 +02:00
self, _: &'static str, visitor: V,
2018-04-14 01:02:01 +02:00
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
2018-03-27 00:58:30 +02:00
{
visitor.visit_newtype_struct(self)
}
2018-04-14 01:02:01 +02:00
fn deserialize_tuple<V>(
2018-04-29 07:55:47 +02:00
self, len: usize, visitor: V,
2018-04-14 01:02:01 +02:00
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
2018-03-27 00:58:30 +02:00
{
if self.req.match_info().len() < len {
Err(de::value::Error::custom(
2018-04-14 01:02:01 +02:00
format!(
"wrong number of parameters: {} expected {}",
self.req.match_info().len(),
len
).as_str(),
))
2018-03-27 00:58:30 +02:00
} else {
2018-04-14 01:02:01 +02:00
visitor.visit_seq(ParamsSeq {
params: self.req.match_info().iter(),
})
2018-03-27 00:58:30 +02:00
}
}
2018-04-14 01:02:01 +02:00
fn deserialize_tuple_struct<V>(
2018-04-29 07:55:47 +02:00
self, _: &'static str, len: usize, visitor: V,
2018-04-14 01:02:01 +02:00
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
2018-03-27 00:58:30 +02:00
{
if self.req.match_info().len() < len {
Err(de::value::Error::custom(
2018-04-14 01:02:01 +02:00
format!(
"wrong number of parameters: {} expected {}",
self.req.match_info().len(),
len
).as_str(),
))
} else {
2018-04-14 01:02:01 +02:00
visitor.visit_seq(ParamsSeq {
params: self.req.match_info().iter(),
})
}
2018-03-27 00:58:30 +02:00
}
2018-04-14 01:02:01 +02:00
fn deserialize_enum<V>(
2018-04-29 07:55:47 +02:00
self, _: &'static str, _: &'static [&'static str], _: V,
2018-04-14 01:02:01 +02:00
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
2018-03-27 00:58:30 +02:00
{
Err(de::value::Error::custom("unsupported type: enum"))
}
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
V: Visitor<'de>,
{
if self.req.match_info().len() != 1 {
Err(de::value::Error::custom(
2018-04-14 01:02:01 +02:00
format!(
"wrong number of parameters: {} expected 1",
self.req.match_info().len()
).as_str(),
))
} else {
visitor.visit_str(&self.req.match_info()[0])
}
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
V: Visitor<'de>,
{
2018-04-14 01:02:01 +02:00
visitor.visit_seq(ParamsSeq {
params: self.req.match_info().iter(),
})
}
2018-03-27 00:58:30 +02:00
unsupported_type!(deserialize_any, "'any'");
unsupported_type!(deserialize_bytes, "bytes");
unsupported_type!(deserialize_option, "Option<T>");
unsupported_type!(deserialize_identifier, "identifier");
unsupported_type!(deserialize_ignored_any, "ignored_any");
parse_single_value!(deserialize_bool, visit_bool, "bool");
parse_single_value!(deserialize_i8, visit_i8, "i8");
parse_single_value!(deserialize_i16, visit_i16, "i16");
parse_single_value!(deserialize_i32, visit_i32, "i16");
parse_single_value!(deserialize_i64, visit_i64, "i64");
parse_single_value!(deserialize_u8, visit_u8, "u8");
parse_single_value!(deserialize_u16, visit_u16, "u16");
parse_single_value!(deserialize_u32, visit_u32, "u32");
parse_single_value!(deserialize_u64, visit_u64, "u64");
parse_single_value!(deserialize_f32, visit_f32, "f32");
parse_single_value!(deserialize_f64, visit_f64, "f64");
parse_single_value!(deserialize_string, visit_string, "String");
parse_single_value!(deserialize_byte_buf, visit_string, "String");
parse_single_value!(deserialize_char, visit_char, "char");
2018-03-27 00:58:30 +02:00
}
struct ParamsDeserializer<'de> {
params: Iter<'de, (Cow<'de, str>, Cow<'de, str>)>,
current: Option<(&'de str, &'de str)>,
}
2018-04-14 01:02:01 +02:00
impl<'de> de::MapAccess<'de> for ParamsDeserializer<'de> {
type Error = de::value::Error;
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
2018-04-14 01:02:01 +02:00
where
K: de::DeserializeSeed<'de>,
{
2018-05-17 21:20:20 +02:00
self.current = self
.params
2018-04-29 18:09:08 +02:00
.next()
.map(|&(ref k, ref v)| (k.as_ref(), v.as_ref()));
match self.current {
2018-04-29 18:09:08 +02:00
Some((key, _)) => Ok(Some(seed.deserialize(Key { key })?)),
None => Ok(None),
}
}
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
V: de::DeserializeSeed<'de>,
{
if let Some((_, value)) = self.current.take() {
2018-04-29 18:09:08 +02:00
seed.deserialize(Value { value })
} else {
Err(de::value::Error::custom("unexpected item"))
}
}
}
struct Key<'de> {
key: &'de str,
}
impl<'de> Deserializer<'de> for Key<'de> {
type Error = de::value::Error;
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
V: Visitor<'de>,
{
visitor.visit_str(self.key)
}
fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
V: Visitor<'de>,
{
Err(de::value::Error::custom("Unexpected"))
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum ignored_any
}
}
macro_rules! parse_value {
($trait_fn:ident, $visit_fn:ident, $tp:tt) => {
fn $trait_fn<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>
{
let v = self.value.parse().map_err(
|_| de::value::Error::custom(
format!("can not parse {:?} to a {}", self.value, $tp)))?;
visitor.$visit_fn(v)
}
}
}
struct Value<'de> {
value: &'de str,
}
2018-04-14 01:02:01 +02:00
impl<'de> Deserializer<'de> for Value<'de> {
type Error = de::value::Error;
parse_value!(deserialize_bool, visit_bool, "bool");
parse_value!(deserialize_i8, visit_i8, "i8");
parse_value!(deserialize_i16, visit_i16, "i16");
parse_value!(deserialize_i32, visit_i32, "i16");
parse_value!(deserialize_i64, visit_i64, "i64");
parse_value!(deserialize_u8, visit_u8, "u8");
parse_value!(deserialize_u16, visit_u16, "u16");
parse_value!(deserialize_u32, visit_u32, "u32");
parse_value!(deserialize_u64, visit_u64, "u64");
parse_value!(deserialize_f32, visit_f32, "f32");
parse_value!(deserialize_f64, visit_f64, "f64");
parse_value!(deserialize_string, visit_string, "String");
parse_value!(deserialize_byte_buf, visit_string, "String");
parse_value!(deserialize_char, visit_char, "char");
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
V: Visitor<'de>,
{
visitor.visit_unit()
}
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
V: Visitor<'de>,
{
visitor.visit_unit()
}
fn deserialize_unit_struct<V>(
2018-04-29 07:55:47 +02:00
self, _: &'static str, visitor: V,
2018-04-14 01:02:01 +02:00
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_unit()
}
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
V: Visitor<'de>,
{
visitor.visit_borrowed_bytes(self.value.as_bytes())
}
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
V: Visitor<'de>,
{
visitor.visit_borrowed_str(self.value)
}
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
V: Visitor<'de>,
{
visitor.visit_some(self)
}
2018-04-14 01:02:01 +02:00
fn deserialize_enum<V>(
2018-04-29 07:55:47 +02:00
self, _: &'static str, _: &'static [&'static str], visitor: V,
2018-04-14 01:02:01 +02:00
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
2018-05-17 21:20:20 +02:00
visitor.visit_enum(ValueEnum { value: self.value })
}
2018-04-14 01:02:01 +02:00
fn deserialize_newtype_struct<V>(
2018-04-29 07:55:47 +02:00
self, _: &'static str, visitor: V,
2018-04-14 01:02:01 +02:00
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_newtype_struct(self)
}
fn deserialize_tuple<V>(self, _: usize, _: V) -> Result<V::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
V: Visitor<'de>,
{
Err(de::value::Error::custom("unsupported type: tuple"))
}
2018-04-14 01:02:01 +02:00
fn deserialize_struct<V>(
2018-04-29 07:55:47 +02:00
self, _: &'static str, _: &'static [&'static str], _: V,
2018-04-14 01:02:01 +02:00
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(de::value::Error::custom("unsupported type: struct"))
}
2018-04-14 01:02:01 +02:00
fn deserialize_tuple_struct<V>(
2018-04-29 07:55:47 +02:00
self, _: &'static str, _: usize, _: V,
2018-04-14 01:02:01 +02:00
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
2018-05-17 21:20:20 +02:00
Err(de::value::Error::custom("unsupported type: tuple struct"))
}
unsupported_type!(deserialize_any, "any");
unsupported_type!(deserialize_seq, "seq");
unsupported_type!(deserialize_map, "map");
unsupported_type!(deserialize_identifier, "identifier");
}
struct ParamsSeq<'de> {
params: Iter<'de, (Cow<'de, str>, Cow<'de, str>)>,
}
2018-04-14 01:02:01 +02:00
impl<'de> de::SeqAccess<'de> for ParamsSeq<'de> {
type Error = de::value::Error;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
2018-04-14 01:02:01 +02:00
where
T: de::DeserializeSeed<'de>,
{
match self.params.next() {
2018-04-14 01:02:01 +02:00
Some(item) => Ok(Some(seed.deserialize(Value {
value: item.1.as_ref(),
})?)),
None => Ok(None),
}
}
}
struct ValueEnum<'de> {
value: &'de str,
}
impl<'de> de::EnumAccess<'de> for ValueEnum<'de> {
type Error = de::value::Error;
type Variant = UnitVariant;
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
2018-04-14 01:02:01 +02:00
where
V: de::DeserializeSeed<'de>,
{
2018-05-17 21:20:20 +02:00
Ok((seed.deserialize(Key { key: self.value })?, UnitVariant))
}
}
struct UnitVariant;
impl<'de> de::VariantAccess<'de> for UnitVariant {
type Error = de::value::Error;
fn unit_variant(self) -> Result<(), Self::Error> {
Ok(())
}
fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
T: de::DeserializeSeed<'de>,
{
Err(de::value::Error::custom("not supported"))
}
fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
2018-04-14 01:02:01 +02:00
where
V: Visitor<'de>,
{
Err(de::value::Error::custom("not supported"))
}
2018-04-14 01:02:01 +02:00
fn struct_variant<V>(
2018-04-29 07:55:47 +02:00
self, _: &'static [&'static str], _: V,
2018-04-14 01:02:01 +02:00
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(de::value::Error::custom("not supported"))
}
}