diff --git a/actix-router/CHANGES.md b/actix-router/CHANGES.md index 8ce805bf..087d67ac 100644 --- a/actix-router/CHANGES.md +++ b/actix-router/CHANGES.md @@ -1,10 +1,13 @@ # Changes ## Unreleased - 2021-xx-xx + + +## 0.5.0-beta.1 - 2021-07-20 * Fix a bug in multi-patterns where static patterns are interpreted as regex. [#366] * Introduce `ResourceDef::pattern_iter` to get an iterator over all patterns in a multi-pattern resource. [#373] * Fix segment interpolation leaving `Path` in unintended state after matching. [#368] -* Fix `ResourceDef` `PartialEq` implementation. +* Fix `ResourceDef` `PartialEq` implementation. [#373] * Re-work `IntoPatterns` trait, adding a `Patterns` enum. [#372] * Implement `IntoPatterns` for `bytestring::ByteString`. [#372] * Rename `Path::{len => segment_count}` to be more descriptive of it's purpose. [#370] diff --git a/actix-router/Cargo.toml b/actix-router/Cargo.toml index b0bcd9da..2a2ce1cc 100644 --- a/actix-router/Cargo.toml +++ b/actix-router/Cargo.toml @@ -1,12 +1,12 @@ [package] name = "actix-router" -version = "0.4.0" +version = "0.5.0-beta.1" authors = [ "Nikolay Kim ", "Ali MJ Al-Nasrawy ", "Rob Ede ", ] -description = "Resource path matching library" +description = "Resource path matching and router" keywords = ["actix", "router", "routing"] repository = "https://github.com/actix/actix-net.git" license = "MIT OR Apache-2.0" diff --git a/actix-router/src/lib.rs b/actix-router/src/lib.rs index 6082c1a3..463e59e4 100644 --- a/actix-router/src/lib.rs +++ b/actix-router/src/lib.rs @@ -1,4 +1,4 @@ -//! Resource path matching library. +//! Resource path matching and router. #![deny(rust_2018_idioms, nonstandard_style)] #![doc(html_logo_url = "https://actix.rs/img/logo.png")] @@ -49,6 +49,15 @@ pub enum Patterns { List(Vec), } +impl Patterns { + pub fn is_empty(&self) -> bool { + match self { + Patterns::Single(_) => false, + Patterns::List(pats) => pats.is_empty(), + } + } +} + /// Helper trait for type that could be converted to one or more path pattern. pub trait IntoPatterns { fn patterns(&self) -> Patterns; @@ -78,6 +87,12 @@ impl IntoPatterns for bytestring::ByteString { } } +impl IntoPatterns for Patterns { + fn patterns(&self) -> Patterns { + self.clone() + } +} + impl> IntoPatterns for Vec { fn patterns(&self) -> Patterns { let mut patterns = self.iter().map(|v| v.as_ref().to_owned()); diff --git a/actix-router/src/router.rs b/actix-router/src/router.rs index 057f7e17..f5deb858 100644 --- a/actix-router/src/router.rs +++ b/actix-router/src/router.rs @@ -12,7 +12,11 @@ pub struct ResourceInfo { } /// Resource router. -pub struct Router(Vec<(ResourceDef, T, Option)>); +// T is the resource itself +// U is any other data needed for routing like method guards +pub struct Router { + routes: Vec<(ResourceDef, T, Option)>, +} impl Router { pub fn build() -> RouterBuilder { @@ -28,7 +32,7 @@ impl Router { { profile_method!(recognize); - for item in self.0.iter() { + for item in self.routes.iter() { if item.0.capture_match_info(resource.resource_path()) { return Some((&item.1, ResourceId(item.0.id()))); } @@ -44,7 +48,7 @@ impl Router { { profile_method!(recognize_mut); - for item in self.0.iter_mut() { + for item in self.routes.iter_mut() { if item.0.capture_match_info(resource.resource_path()) { return Some((&mut item.1, ResourceId(item.0.id()))); } @@ -61,7 +65,7 @@ impl Router { { profile_method!(recognize_checked); - for item in self.0.iter() { + for item in self.routes.iter() { if item.0.capture_match_info_fn(resource, &check, &item.2) { return Some((&item.1, ResourceId(item.0.id()))); } @@ -82,7 +86,7 @@ impl Router { { profile_method!(recognize_mut_checked); - for item in self.0.iter_mut() { + for item in self.routes.iter_mut() { if item.0.capture_match_info_fn(resource, &check, &item.2) { return Some((&mut item.1, ResourceId(item.0.id()))); } @@ -129,7 +133,9 @@ impl RouterBuilder { /// Finish configuration and create router instance. pub fn finish(self) -> Router { - Router(self.resources) + Router { + routes: self.resources, + } } }