1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-18 20:01:48 +01:00

allow empty pattern

This commit is contained in:
Nikolay Kim 2019-03-03 21:00:58 -08:00
parent 0410f59cf5
commit fb43940824
6 changed files with 40 additions and 44 deletions

View File

@ -2,7 +2,7 @@ use serde::de::{self, Deserializer, Error as DeError, Visitor};
use serde::forward_to_deserialize_any; use serde::forward_to_deserialize_any;
use crate::path::{Path, PathIter}; use crate::path::{Path, PathIter};
use crate::ResourcePath; use crate::Resource;
macro_rules! unsupported_type { macro_rules! unsupported_type {
($trait_fn:ident, $name:expr) => { ($trait_fn:ident, $name:expr) => {
@ -33,17 +33,17 @@ macro_rules! parse_single_value {
} }
} }
pub struct PathDeserializer<'de, T: ResourcePath + 'de> { pub struct PathDeserializer<'de, T: Resource + 'de> {
path: &'de Path<T>, path: &'de Path<T>,
} }
impl<'de, T: ResourcePath + 'de> PathDeserializer<'de, T> { impl<'de, T: Resource + 'de> PathDeserializer<'de, T> {
pub fn new(path: &'de Path<T>) -> Self { pub fn new(path: &'de Path<T>) -> Self {
PathDeserializer { path } PathDeserializer { path }
} }
} }
impl<'de, T: ResourcePath + 'de> Deserializer<'de> for PathDeserializer<'de, T> { impl<'de, T: Resource + 'de> Deserializer<'de> for PathDeserializer<'de, T> {
type Error = de::value::Error; type Error = de::value::Error;
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
@ -206,12 +206,12 @@ impl<'de, T: ResourcePath + 'de> Deserializer<'de> for PathDeserializer<'de, T>
parse_single_value!(deserialize_char, visit_char, "char"); parse_single_value!(deserialize_char, visit_char, "char");
} }
struct ParamsDeserializer<'de, T: ResourcePath> { struct ParamsDeserializer<'de, T: Resource> {
params: PathIter<'de, T>, params: PathIter<'de, T>,
current: Option<(&'de str, &'de str)>, current: Option<(&'de str, &'de str)>,
} }
impl<'de, T: ResourcePath> de::MapAccess<'de> for ParamsDeserializer<'de, T> { impl<'de, T: Resource> de::MapAccess<'de> for ParamsDeserializer<'de, T> {
type Error = de::value::Error; type Error = de::value::Error;
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error> fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
@ -406,11 +406,11 @@ impl<'de> Deserializer<'de> for Value<'de> {
unsupported_type!(deserialize_identifier, "identifier"); unsupported_type!(deserialize_identifier, "identifier");
} }
struct ParamsSeq<'de, T: ResourcePath> { struct ParamsSeq<'de, T: Resource> {
params: PathIter<'de, T>, params: PathIter<'de, T>,
} }
impl<'de, T: ResourcePath> de::SeqAccess<'de> for ParamsSeq<'de, T> { impl<'de, T: Resource> de::SeqAccess<'de> for ParamsSeq<'de, T> {
type Error = de::value::Error; type Error = de::value::Error;
fn next_element_seed<U>(&mut self, seed: U) -> Result<Option<U::Value>, Self::Error> fn next_element_seed<U>(&mut self, seed: U) -> Result<Option<U::Value>, Self::Error>

View File

@ -9,23 +9,23 @@ pub use self::path::Path;
pub use self::resource::ResourceDef; pub use self::resource::ResourceDef;
pub use self::router::{ResourceInfo, Router, RouterBuilder}; pub use self::router::{ResourceInfo, Router, RouterBuilder};
pub trait ResourcePath { pub trait Resource {
fn path(&self) -> &str; fn path(&self) -> &str;
} }
impl ResourcePath for String { impl Resource for String {
fn path(&self) -> &str { fn path(&self) -> &str {
self.as_str() self.as_str()
} }
} }
impl<'a> ResourcePath for &'a str { impl<'a> Resource for &'a str {
fn path(&self) -> &str { fn path(&self) -> &str {
self self
} }
} }
impl<T: AsRef<[u8]>> ResourcePath for string::String<T> { impl<T: AsRef<[u8]>> Resource for string::String<T> {
fn path(&self) -> &str { fn path(&self) -> &str {
&*self &*self
} }
@ -39,10 +39,10 @@ pub use self::url::Url;
#[cfg(feature = "http")] #[cfg(feature = "http")]
mod http_support { mod http_support {
use super::ResourcePath; use super::Resource;
use http::Uri; use http::Uri;
impl ResourcePath for Uri { impl Resource for Uri {
fn path(&self) -> &str { fn path(&self) -> &str {
self.path() self.path()
} }

View File

@ -4,7 +4,7 @@ use std::rc::Rc;
use serde::de; use serde::de;
use crate::de::PathDeserializer; use crate::de::PathDeserializer;
use crate::ResourcePath; use crate::Resource;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub(crate) enum PathItem { pub(crate) enum PathItem {
@ -42,7 +42,7 @@ impl<T: Clone> Clone for Path<T> {
} }
} }
impl<T: ResourcePath> Path<T> { impl<T: Resource> Path<T> {
pub fn new(path: T) -> Path<T> { pub fn new(path: T) -> Path<T> {
Path { Path {
path, path,
@ -165,7 +165,7 @@ pub struct PathIter<'a, T> {
params: &'a Path<T>, params: &'a Path<T>,
} }
impl<'a, T: ResourcePath> Iterator for PathIter<'a, T> { impl<'a, T: Resource> Iterator for PathIter<'a, T> {
type Item = (&'a str, &'a str); type Item = (&'a str, &'a str);
#[inline] #[inline]
@ -183,7 +183,7 @@ impl<'a, T: ResourcePath> Iterator for PathIter<'a, T> {
} }
} }
impl<'a, T: ResourcePath> Index<&'a str> for Path<T> { impl<'a, T: Resource> Index<&'a str> for Path<T> {
type Output = str; type Output = str;
fn index(&self, name: &'a str) -> &str { fn index(&self, name: &'a str) -> &str {
@ -192,7 +192,7 @@ impl<'a, T: ResourcePath> Index<&'a str> for Path<T> {
} }
} }
impl<T: ResourcePath> Index<usize> for Path<T> { impl<T: Resource> Index<usize> for Path<T> {
type Output = str; type Output = str;
fn index(&self, idx: usize) -> &str { fn index(&self, idx: usize) -> &str {

View File

@ -5,7 +5,7 @@ use std::rc::Rc;
use regex::{escape, Regex}; use regex::{escape, Regex};
use crate::path::{Path, PathItem}; use crate::path::{Path, PathItem};
use crate::ResourcePath; use crate::Resource;
const MAX_DYNAMIC_SEGMENTS: usize = 16; const MAX_DYNAMIC_SEGMENTS: usize = 16;
@ -119,7 +119,7 @@ impl ResourceDef {
} }
/// Is the given path and parameters a match against this pattern? /// Is the given path and parameters a match against this pattern?
pub fn match_path<T: ResourcePath>(&self, path: &mut Path<T>) -> bool { pub fn match_path<T: Resource>(&self, path: &mut Path<T>) -> bool {
match self.tp { match self.tp {
PatternType::Static(ref s) => { PatternType::Static(ref s) => {
if s == path.path() { if s == path.path() {

View File

@ -3,7 +3,7 @@ use std::rc::Rc;
use crate::path::Path; use crate::path::Path;
use crate::resource::ResourceDef; use crate::resource::ResourceDef;
use crate::ResourcePath; use crate::Resource;
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]
pub(crate) enum ResourceId { pub(crate) enum ResourceId {
@ -41,34 +41,30 @@ impl<T> Router<T> {
} }
} }
pub fn recognize<U: ResourcePath>(&self, path: &mut Path<U>) -> Option<(&T, ResourceInfo)> { pub fn recognize<U: Resource>(&self, path: &mut Path<U>) -> Option<(&T, ResourceInfo)> {
if !path.path().is_empty() { for (idx, resource) in self.rmap.patterns.iter().enumerate() {
for (idx, resource) in self.rmap.patterns.iter().enumerate() { if resource.match_path(path) {
if resource.match_path(path) { let info = ResourceInfo {
let info = ResourceInfo { rmap: self.rmap.clone(),
rmap: self.rmap.clone(), resource: ResourceId::Normal(idx as u16),
resource: ResourceId::Normal(idx as u16), };
}; return Some((&self.resources[idx], info));
return Some((&self.resources[idx], info));
}
} }
} }
None None
} }
pub fn recognize_mut<U: ResourcePath>( pub fn recognize_mut<U: Resource>(
&mut self, &mut self,
path: &mut Path<U>, path: &mut Path<U>,
) -> Option<(&mut T, ResourceInfo)> { ) -> Option<(&mut T, ResourceInfo)> {
if !path.path().is_empty() { for (idx, resource) in self.rmap.patterns.iter().enumerate() {
for (idx, resource) in self.rmap.patterns.iter().enumerate() { if resource.match_path(path) {
if resource.match_path(path) { let info = ResourceInfo {
let info = ResourceInfo { rmap: self.rmap.clone(),
rmap: self.rmap.clone(), resource: ResourceId::Normal(idx as u16),
resource: ResourceId::Normal(idx as u16), };
}; return Some((&mut self.resources[idx], info));
return Some((&mut self.resources[idx], info));
}
} }
} }
None None

View File

@ -1,6 +1,6 @@
use std::rc::Rc; use std::rc::Rc;
use crate::ResourcePath; use crate::Resource;
#[allow(dead_code)] #[allow(dead_code)]
const GEN_DELIMS: &[u8] = b":/?#[]@"; const GEN_DELIMS: &[u8] = b":/?#[]@";
@ -67,7 +67,7 @@ impl Url {
} }
} }
impl ResourcePath for Url { impl Resource for Url {
fn path(&self) -> &str { fn path(&self) -> &str {
self.path() self.path()
} }