From f8e170fdafdaebadc39311d9fdc3cee94e126d2b Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 6 Jan 2019 10:26:59 -0800 Subject: [PATCH] add enum support for path deserializer --- router/src/de.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/router/src/de.rs b/router/src/de.rs index 68562909..89622fd3 100644 --- a/router/src/de.rs +++ b/router/src/de.rs @@ -146,12 +146,20 @@ impl<'de, T: RequestPath + 'de> Deserializer<'de> for PathDeserializer<'de, T> { self, _: &'static str, _: &'static [&'static str], - _: V, + visitor: V, ) -> Result where 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(self, visitor: V) -> Result @@ -564,6 +572,28 @@ mod tests { 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] fn test_extract_enum_value() { let mut router = Router::<()>::build();