From 2b8f41e9e42121aed95d8d968848f9ecc6cb0c0e Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 5 Jan 2019 22:00:38 -0800 Subject: [PATCH] helper impls for Router --- router/src/lib.rs | 14 +++++++++++++- router/src/path.rs | 25 +++++++++++++++++++++++-- router/src/router.rs | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/router/src/lib.rs b/router/src/lib.rs index c2135055..54a2fc73 100644 --- a/router/src/lib.rs +++ b/router/src/lib.rs @@ -7,7 +7,7 @@ mod router; pub use self::de::PathDeserializer; pub use self::path::Path; pub use self::pattern::Pattern; -pub use self::router::{Router, RouterBuilder}; +pub use self::router::{ResourceInfo, Router, RouterBuilder}; pub trait RequestPath { fn path(&self) -> &str; @@ -30,3 +30,15 @@ impl> RequestPath for string::String { &*self } } + +#[cfg(feature = "http")] +mod http_support { + use super::RequestPath; + use http::Uri; + + impl RequestPath for Uri { + fn path(&self) -> &str { + self.path() + } + } +} diff --git a/router/src/path.rs b/router/src/path.rs index 0087dfdd..ee467af8 100644 --- a/router/src/path.rs +++ b/router/src/path.rs @@ -12,7 +12,7 @@ pub(crate) enum PathItem { /// Resource path match information /// /// If resource path contains variable patterns, `Path` stores them. -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct Path { path: T, pub(crate) skip: u16, @@ -29,6 +29,16 @@ impl Default for Path { } } +impl Clone for Path { + fn clone(&self) -> Self { + Path { + path: self.path.clone(), + skip: self.skip, + segments: self.segments.clone(), + } + } +} + impl Path { pub fn new(path: T) -> Path { Path { @@ -38,6 +48,16 @@ impl Path { } } + /// Get reference to inner path instance + pub fn get_ref(&self) -> &T { + &self.path + } + + /// Get mutable reference to inner path instance + pub fn get_mut(&mut self) -> &mut T { + &mut self.path + } + pub fn path(&self) -> &str { let skip = self.skip as usize; let path = self.path.path(); @@ -68,7 +88,8 @@ impl Path { } } - pub(crate) fn add_static(&mut self, name: &str, value: &'static str) { + #[doc(hidden)] + pub fn add_static(&mut self, name: &str, value: &'static str) { self.segments .push((Rc::new(name.to_string()), PathItem::Static(value))); } diff --git a/router/src/router.rs b/router/src/router.rs index a875da6e..4568e778 100644 --- a/router/src/router.rs +++ b/router/src/router.rs @@ -75,6 +75,24 @@ impl Router { } } +impl<'a, T> IntoIterator for &'a Router { + type Item = &'a T; + type IntoIter = std::slice::Iter<'a, T>; + + fn into_iter(self) -> Self::IntoIter { + self.resources.iter() + } +} + +impl<'a, T> IntoIterator for &'a mut Router { + type Item = &'a mut T; + type IntoIter = std::slice::IterMut<'a, T>; + + fn into_iter(self) -> Self::IntoIter { + self.resources.iter_mut() + } +} + impl ResourceMap { fn register(&mut self, pattern: Pattern) { self.patterns.push(pattern);