1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-27 20:12:58 +01:00

add enum support for path deserializer

This commit is contained in:
Nikolay Kim 2019-01-06 10:26:59 -08:00
parent 474fed4dfe
commit f8e170fdaf

View File

@ -146,12 +146,20 @@ impl<'de, T: RequestPath + 'de> Deserializer<'de> for PathDeserializer<'de, T> {
self, self,
_: &'static str, _: &'static str,
_: &'static [&'static str], _: &'static [&'static str],
_: V, visitor: V,
) -> Result<V::Value, Self::Error> ) -> Result<V::Value, Self::Error>
where where
V: Visitor<'de>, V: Visitor<'de>,
{ {
Err(de::value::Error::custom("unsupported type: enum")) if self.path.len() < 1 {
Err(de::value::Error::custom(
"expeceted at least one parameters",
))
} else {
visitor.visit_enum(ValueEnum {
value: &self.path[0],
})
}
} }
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
@ -564,6 +572,28 @@ mod tests {
assert_eq!(i, 32); assert_eq!(i, 32);
} }
#[test]
fn test_extract_enum() {
let mut router = Router::<()>::build();
router.path("/{val}/", ());
let router = router.finish();
let mut path = Path::new("/val1/");
assert!(router.recognize(&mut path).is_some());
let i: TestEnum = de::Deserialize::deserialize(PathDeserializer::new(&path)).unwrap();
assert_eq!(i, TestEnum::Val1);
let mut router = Router::<()>::build();
router.path("/{val1}/{val2}/", ());
let router = router.finish();
let mut path = Path::new("/val1/val2/");
assert!(router.recognize(&mut path).is_some());
let i: (TestEnum, TestEnum) =
de::Deserialize::deserialize(PathDeserializer::new(&path)).unwrap();
assert_eq!(i, (TestEnum::Val1, TestEnum::Val2));
}
#[test] #[test]
fn test_extract_enum_value() { fn test_extract_enum_value() {
let mut router = Router::<()>::build(); let mut router = Router::<()>::build();