mirror of
https://github.com/fafhrd91/actix-net
synced 2025-08-15 12:30:20 +02:00
Compare commits
6 Commits
utils-0.2.
...
service-v0
Author | SHA1 | Date | |
---|---|---|---|
|
3d7daabdd7 | ||
|
32f4718880 | ||
|
b8f9bf4bc8 | ||
|
e354c6df92 | ||
|
a53f06a1a4 | ||
|
9979bfb3ef |
@@ -1,8 +1,15 @@
|
||||
# Changes
|
||||
|
||||
## [0.2.1] - 2019-02-09
|
||||
|
||||
### Changes
|
||||
|
||||
* Drop service response
|
||||
|
||||
|
||||
## [0.2.0] - 2019-02-01
|
||||
|
||||
## Changes
|
||||
### Changes
|
||||
|
||||
* Migrate to actix-service 0.2
|
||||
|
||||
@@ -11,14 +18,14 @@
|
||||
|
||||
## [0.1.3] - 2018-12-21
|
||||
|
||||
## Fixed
|
||||
### Fixed
|
||||
|
||||
* Fix max concurrent connections handling
|
||||
|
||||
|
||||
## [0.1.2] - 2018-12-12
|
||||
|
||||
## Changed
|
||||
### Changed
|
||||
|
||||
* rename ServiceConfig::rt() to ServiceConfig::apply()
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-server"
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix server - General purpose tcp server"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
@@ -33,7 +33,7 @@ ssl = ["openssl", "tokio-openssl"]
|
||||
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
|
||||
|
||||
[dependencies]
|
||||
actix-service = "0.2.0"
|
||||
actix-service = "0.2.1"
|
||||
actix-rt = "0.1.0"
|
||||
|
||||
log = "0.4"
|
||||
|
@@ -23,13 +23,13 @@ pub enum ServerMessage {
|
||||
}
|
||||
|
||||
pub trait StreamServiceFactory: Send + Clone + 'static {
|
||||
type NewService: NewService<Request = TcpStream, Response = ()>;
|
||||
type NewService: NewService<Request = TcpStream>;
|
||||
|
||||
fn create(&self) -> Self::NewService;
|
||||
}
|
||||
|
||||
pub trait ServiceFactory: Send + Clone + 'static {
|
||||
type NewService: NewService<Request = ServerMessage, Response = ()>;
|
||||
type NewService: NewService<Request = ServerMessage>;
|
||||
|
||||
fn create(&self) -> Self::NewService;
|
||||
}
|
||||
@@ -63,7 +63,7 @@ impl<T> StreamService<T> {
|
||||
|
||||
impl<T> Service for StreamService<T>
|
||||
where
|
||||
T: Service<Request = TcpStream, Response = ()>,
|
||||
T: Service<Request = TcpStream>,
|
||||
T::Future: 'static,
|
||||
T::Error: 'static,
|
||||
{
|
||||
@@ -86,7 +86,7 @@ where
|
||||
if let Ok(stream) = stream {
|
||||
spawn(self.service.call(stream).then(move |res| {
|
||||
drop(guard);
|
||||
res.map_err(|_| ())
|
||||
res.map_err(|_| ()).map(|_| ())
|
||||
}));
|
||||
ok(())
|
||||
} else {
|
||||
@@ -110,7 +110,7 @@ impl<T> ServerService<T> {
|
||||
|
||||
impl<T> Service for ServerService<T>
|
||||
where
|
||||
T: Service<Request = ServerMessage, Response = ()>,
|
||||
T: Service<Request = ServerMessage>,
|
||||
T::Future: 'static,
|
||||
T::Error: 'static,
|
||||
{
|
||||
@@ -126,7 +126,7 @@ where
|
||||
fn call(&mut self, (guard, req): (Option<CounterGuard>, ServerMessage)) -> Self::Future {
|
||||
spawn(self.service.call(req).then(move |res| {
|
||||
drop(guard);
|
||||
res.map_err(|_| ())
|
||||
res.map_err(|_| ()).map(|_| ())
|
||||
}));
|
||||
ok(())
|
||||
}
|
||||
@@ -241,7 +241,7 @@ impl InternalServiceFactory for Box<InternalServiceFactory> {
|
||||
impl<F, T> ServiceFactory for F
|
||||
where
|
||||
F: Fn() -> T + Send + Clone + 'static,
|
||||
T: NewService<Request = ServerMessage, Response = ()>,
|
||||
T: NewService<Request = ServerMessage>,
|
||||
{
|
||||
type NewService = T;
|
||||
|
||||
@@ -253,7 +253,7 @@ where
|
||||
impl<F, T> StreamServiceFactory for F
|
||||
where
|
||||
F: Fn() -> T + Send + Clone + 'static,
|
||||
T: NewService<Request = TcpStream, Response = ()>,
|
||||
T: NewService<Request = TcpStream>,
|
||||
{
|
||||
type NewService = T;
|
||||
|
||||
|
@@ -1,5 +1,14 @@
|
||||
# Changes
|
||||
|
||||
## [0.2.2] - 2019-02-19
|
||||
|
||||
### Added
|
||||
|
||||
* Added `NewService` impl for `Rc<S> where S: NewService`
|
||||
|
||||
* Added `NewService` impl for `Arc<S> where S: NewService`
|
||||
|
||||
|
||||
## [0.2.1] - 2019-02-03
|
||||
|
||||
### Changed
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-service"
|
||||
version = "0.2.1"
|
||||
version = "0.2.2"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix Service"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
|
@@ -89,7 +89,6 @@ where
|
||||
/// `ApplyNewService` new service combinator
|
||||
pub struct ApplyNewService<T, S>
|
||||
where
|
||||
// T::InitError: From<S::InitError>,
|
||||
T: NewTransform<S::Service, InitError = S::InitError>,
|
||||
T::Error: From<S::Error>,
|
||||
S: NewService,
|
||||
|
@@ -1,3 +1,6 @@
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures::{Future, IntoFuture, Poll};
|
||||
|
||||
mod and_then;
|
||||
@@ -377,6 +380,38 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> NewService for Rc<S>
|
||||
where
|
||||
S: NewService,
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Service = S::Service;
|
||||
type InitError = S::InitError;
|
||||
type Future = S::Future;
|
||||
|
||||
fn new_service(&self) -> S::Future {
|
||||
self.as_ref().new_service()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> NewService for Arc<S>
|
||||
where
|
||||
S: NewService,
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Service = S::Service;
|
||||
type InitError = S::InitError;
|
||||
type Future = S::Future;
|
||||
|
||||
fn new_service(&self) -> S::Future {
|
||||
self.as_ref().new_service()
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for types that can be converted to a `Service`
|
||||
pub trait IntoService<T>
|
||||
where
|
||||
|
@@ -1,5 +1,14 @@
|
||||
# Changes
|
||||
|
||||
## [0.2.2] - 2019-02-11
|
||||
|
||||
### Added
|
||||
|
||||
* Add `Display` impl for `TimeoutError`
|
||||
|
||||
* Add `Display` impl for `InOrderError`
|
||||
|
||||
|
||||
## [0.2.1] - 2019-02-06
|
||||
|
||||
### Added
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-utils"
|
||||
version = "0.2.1"
|
||||
version = "0.2.2"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix utils - various actix net related services"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
|
@@ -37,6 +37,15 @@ impl<E: fmt::Debug> fmt::Debug for InOrderError<E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: fmt::Display> fmt::Display for InOrderError<E> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
InOrderError::Service(e) => e.fmt(f),
|
||||
InOrderError::Disconnected => write!(f, "InOrder service disconnected"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// InOrder - The service will yield responses as they become available,
|
||||
/// in the order that their originating requests were submitted to the service.
|
||||
pub struct InOrder<S> {
|
||||
|
@@ -41,6 +41,15 @@ impl<E: fmt::Debug> fmt::Debug for TimeoutError<E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: fmt::Display> fmt::Display for TimeoutError<E> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
TimeoutError::Service(e) => e.fmt(f),
|
||||
TimeoutError::Timeout => write!(f, "Service call timeout"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: PartialEq> PartialEq for TimeoutError<E> {
|
||||
fn eq(&self, other: &TimeoutError<E>) -> bool {
|
||||
match self {
|
||||
|
@@ -2,7 +2,7 @@ use serde::de::{self, Deserializer, Error as DeError, Visitor};
|
||||
use serde::forward_to_deserialize_any;
|
||||
|
||||
use crate::path::{Path, PathIter};
|
||||
use crate::RequestPath;
|
||||
use crate::ResourcePath;
|
||||
|
||||
macro_rules! unsupported_type {
|
||||
($trait_fn:ident, $name:expr) => {
|
||||
@@ -33,17 +33,17 @@ macro_rules! parse_single_value {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PathDeserializer<'de, T: RequestPath + 'de> {
|
||||
pub struct PathDeserializer<'de, T: ResourcePath + 'de> {
|
||||
path: &'de Path<T>,
|
||||
}
|
||||
|
||||
impl<'de, T: RequestPath + 'de> PathDeserializer<'de, T> {
|
||||
impl<'de, T: ResourcePath + 'de> PathDeserializer<'de, T> {
|
||||
pub fn new(path: &'de Path<T>) -> Self {
|
||||
PathDeserializer { path }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T: RequestPath + 'de> Deserializer<'de> for PathDeserializer<'de, T> {
|
||||
impl<'de, T: ResourcePath + 'de> Deserializer<'de> for PathDeserializer<'de, T> {
|
||||
type Error = de::value::Error;
|
||||
|
||||
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
@@ -206,12 +206,12 @@ impl<'de, T: RequestPath + 'de> Deserializer<'de> for PathDeserializer<'de, T> {
|
||||
parse_single_value!(deserialize_char, visit_char, "char");
|
||||
}
|
||||
|
||||
struct ParamsDeserializer<'de, T: RequestPath> {
|
||||
struct ParamsDeserializer<'de, T: ResourcePath> {
|
||||
params: PathIter<'de, T>,
|
||||
current: Option<(&'de str, &'de str)>,
|
||||
}
|
||||
|
||||
impl<'de, T: RequestPath> de::MapAccess<'de> for ParamsDeserializer<'de, T> {
|
||||
impl<'de, T: ResourcePath> de::MapAccess<'de> for ParamsDeserializer<'de, T> {
|
||||
type Error = de::value::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");
|
||||
}
|
||||
|
||||
struct ParamsSeq<'de, T: RequestPath> {
|
||||
struct ParamsSeq<'de, T: ResourcePath> {
|
||||
params: PathIter<'de, T>,
|
||||
}
|
||||
|
||||
impl<'de, T: RequestPath> de::SeqAccess<'de> for ParamsSeq<'de, T> {
|
||||
impl<'de, T: ResourcePath> de::SeqAccess<'de> for ParamsSeq<'de, T> {
|
||||
type Error = de::value::Error;
|
||||
|
||||
fn next_element_seed<U>(&mut self, seed: U) -> Result<Option<U::Value>, Self::Error>
|
||||
|
@@ -1,31 +1,31 @@
|
||||
//! Resource path matching library.
|
||||
mod de;
|
||||
mod path;
|
||||
mod pattern;
|
||||
mod resource;
|
||||
mod router;
|
||||
|
||||
pub use self::de::PathDeserializer;
|
||||
pub use self::path::Path;
|
||||
pub use self::pattern::Pattern;
|
||||
pub use self::resource::ResourceDef;
|
||||
pub use self::router::{ResourceInfo, Router, RouterBuilder};
|
||||
|
||||
pub trait RequestPath {
|
||||
pub trait ResourcePath {
|
||||
fn path(&self) -> &str;
|
||||
}
|
||||
|
||||
impl RequestPath for String {
|
||||
impl ResourcePath for String {
|
||||
fn path(&self) -> &str {
|
||||
self.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> RequestPath for &'a str {
|
||||
impl<'a> ResourcePath for &'a str {
|
||||
fn path(&self) -> &str {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AsRef<[u8]>> RequestPath for string::String<T> {
|
||||
impl<T: AsRef<[u8]>> ResourcePath for string::String<T> {
|
||||
fn path(&self) -> &str {
|
||||
&*self
|
||||
}
|
||||
@@ -39,10 +39,10 @@ pub use self::url::Url;
|
||||
|
||||
#[cfg(feature = "http")]
|
||||
mod http_support {
|
||||
use super::RequestPath;
|
||||
use super::ResourcePath;
|
||||
use http::Uri;
|
||||
|
||||
impl RequestPath for Uri {
|
||||
impl ResourcePath for Uri {
|
||||
fn path(&self) -> &str {
|
||||
self.path()
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@ use std::rc::Rc;
|
||||
use serde::de;
|
||||
|
||||
use crate::de::PathDeserializer;
|
||||
use crate::RequestPath;
|
||||
use crate::ResourcePath;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub(crate) enum PathItem {
|
||||
@@ -42,7 +42,7 @@ impl<T: Clone> Clone for Path<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RequestPath> Path<T> {
|
||||
impl<T: ResourcePath> Path<T> {
|
||||
pub fn new(path: T) -> Path<T> {
|
||||
Path {
|
||||
path,
|
||||
@@ -165,7 +165,7 @@ pub struct PathIter<'a, T> {
|
||||
params: &'a Path<T>,
|
||||
}
|
||||
|
||||
impl<'a, T: RequestPath> Iterator for PathIter<'a, T> {
|
||||
impl<'a, T: ResourcePath> Iterator for PathIter<'a, T> {
|
||||
type Item = (&'a str, &'a str);
|
||||
|
||||
#[inline]
|
||||
@@ -183,7 +183,7 @@ impl<'a, T: RequestPath> Iterator for PathIter<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: RequestPath> Index<&'a str> for Path<T> {
|
||||
impl<'a, T: ResourcePath> Index<&'a str> for Path<T> {
|
||||
type Output = str;
|
||||
|
||||
fn index(&self, name: &'a str) -> &str {
|
||||
@@ -192,7 +192,7 @@ impl<'a, T: RequestPath> Index<&'a str> for Path<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RequestPath> Index<usize> for Path<T> {
|
||||
impl<T: ResourcePath> Index<usize> for Path<T> {
|
||||
type Output = str;
|
||||
|
||||
fn index(&self, idx: usize) -> &str {
|
||||
|
@@ -5,20 +5,35 @@ use std::rc::Rc;
|
||||
use regex::{escape, Regex};
|
||||
|
||||
use crate::path::{Path, PathItem};
|
||||
use crate::RequestPath;
|
||||
use crate::ResourcePath;
|
||||
|
||||
const MAX_DYNAMIC_SEGMENTS: usize = 16;
|
||||
|
||||
/// Resource type describes an entry in resources table
|
||||
/// ResourceDef describes an entry in resources table
|
||||
///
|
||||
/// Resource pattern can contain only 16 dynamic segments
|
||||
/// Resource definition can contain only 16 dynamic segments
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Pattern {
|
||||
pub struct ResourceDef {
|
||||
tp: PatternType,
|
||||
rtp: ResourceType,
|
||||
name: String,
|
||||
pattern: String,
|
||||
elements: Vec<PatternElement>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||
/// Resource type
|
||||
pub enum ResourceType {
|
||||
/// Normal resource
|
||||
Normal,
|
||||
/// Resource for application default handler
|
||||
Default,
|
||||
/// External resource
|
||||
External,
|
||||
/// Unknown resource type
|
||||
Unset,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
enum PatternElement {
|
||||
Str(String),
|
||||
@@ -32,12 +47,12 @@ enum PatternType {
|
||||
Dynamic(Regex, Vec<Rc<String>>, usize),
|
||||
}
|
||||
|
||||
impl Pattern {
|
||||
impl ResourceDef {
|
||||
/// Parse path pattern and create new `Pattern` instance.
|
||||
///
|
||||
/// Panics if path pattern is wrong.
|
||||
pub fn new(path: &str) -> Self {
|
||||
Pattern::with_prefix(path, false)
|
||||
ResourceDef::with_prefix(path, false)
|
||||
}
|
||||
|
||||
/// Parse path pattern and create new `Pattern` instance.
|
||||
@@ -46,13 +61,22 @@ impl Pattern {
|
||||
///
|
||||
/// Panics if path regex pattern is wrong.
|
||||
pub fn prefix(path: &str) -> Self {
|
||||
Pattern::with_prefix(path, true)
|
||||
ResourceDef::with_prefix(path, true)
|
||||
}
|
||||
|
||||
/// Construct external resource def
|
||||
///
|
||||
/// Panics if path pattern is malformed.
|
||||
pub fn external(path: &str) -> Self {
|
||||
let mut resource = ResourceDef::with_prefix(path, false);
|
||||
resource.rtp = ResourceType::External;
|
||||
resource
|
||||
}
|
||||
|
||||
/// Parse path pattern and create new `Pattern` instance with custom prefix
|
||||
fn with_prefix(path: &str, for_prefix: bool) -> Self {
|
||||
let path = path.to_owned();
|
||||
let (pattern, elements, is_dynamic, len) = Pattern::parse(&path, for_prefix);
|
||||
let (pattern, elements, is_dynamic, len) = ResourceDef::parse(&path, for_prefix);
|
||||
|
||||
let tp = if is_dynamic {
|
||||
let re = match Regex::new(&pattern) {
|
||||
@@ -71,9 +95,11 @@ impl Pattern {
|
||||
PatternType::Static(pattern.clone())
|
||||
};
|
||||
|
||||
Pattern {
|
||||
ResourceDef {
|
||||
tp,
|
||||
elements,
|
||||
name: String::new(),
|
||||
rtp: ResourceType::Normal,
|
||||
pattern: path.to_owned(),
|
||||
}
|
||||
}
|
||||
@@ -93,7 +119,7 @@ impl Pattern {
|
||||
}
|
||||
|
||||
/// Is the given path and parameters a match against this pattern?
|
||||
pub fn match_path<T: RequestPath>(&self, path: &mut Path<T>) -> bool {
|
||||
pub fn match_path<T: ResourcePath>(&self, path: &mut Path<T>) -> bool {
|
||||
match self.tp {
|
||||
PatternType::Static(ref s) => {
|
||||
if s == path.path() {
|
||||
@@ -261,20 +287,32 @@ impl Pattern {
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Pattern {
|
||||
fn eq(&self, other: &Pattern) -> bool {
|
||||
impl Eq for ResourceDef {}
|
||||
|
||||
impl PartialEq for ResourceDef {
|
||||
fn eq(&self, other: &ResourceDef) -> bool {
|
||||
self.pattern == other.pattern
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Pattern {}
|
||||
|
||||
impl Hash for Pattern {
|
||||
impl Hash for ResourceDef {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.pattern.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for ResourceDef {
|
||||
fn from(path: &'a str) -> ResourceDef {
|
||||
ResourceDef::new(path)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for ResourceDef {
|
||||
fn from(path: String) -> ResourceDef {
|
||||
ResourceDef::new(&path)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -282,29 +320,29 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_parse_static() {
|
||||
let re = Pattern::new("/");
|
||||
let re = ResourceDef::new("/");
|
||||
assert!(re.is_match("/"));
|
||||
assert!(!re.is_match("/a"));
|
||||
|
||||
let re = Pattern::new("/name");
|
||||
let re = ResourceDef::new("/name");
|
||||
assert!(re.is_match("/name"));
|
||||
assert!(!re.is_match("/name1"));
|
||||
assert!(!re.is_match("/name/"));
|
||||
assert!(!re.is_match("/name~"));
|
||||
|
||||
let re = Pattern::new("/name/");
|
||||
let re = ResourceDef::new("/name/");
|
||||
assert!(re.is_match("/name/"));
|
||||
assert!(!re.is_match("/name"));
|
||||
assert!(!re.is_match("/name/gs"));
|
||||
|
||||
let re = Pattern::new("/user/profile");
|
||||
let re = ResourceDef::new("/user/profile");
|
||||
assert!(re.is_match("/user/profile"));
|
||||
assert!(!re.is_match("/user/profile/profile"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_param() {
|
||||
let re = Pattern::new("/user/{id}");
|
||||
let re = ResourceDef::new("/user/{id}");
|
||||
assert!(re.is_match("/user/profile"));
|
||||
assert!(re.is_match("/user/2345"));
|
||||
assert!(!re.is_match("/user/2345/"));
|
||||
@@ -318,7 +356,7 @@ mod tests {
|
||||
assert!(re.match_path(&mut path));
|
||||
assert_eq!(path.get("id").unwrap(), "1245125");
|
||||
|
||||
let re = Pattern::new("/v{version}/resource/{id}");
|
||||
let re = ResourceDef::new("/v{version}/resource/{id}");
|
||||
assert!(re.is_match("/v1/resource/320120"));
|
||||
assert!(!re.is_match("/v/resource/1"));
|
||||
assert!(!re.is_match("/resource"));
|
||||
@@ -328,7 +366,7 @@ mod tests {
|
||||
assert_eq!(path.get("version").unwrap(), "151");
|
||||
assert_eq!(path.get("id").unwrap(), "adahg32");
|
||||
|
||||
let re = Pattern::new("/{id:[[:digit:]]{6}}");
|
||||
let re = ResourceDef::new("/{id:[[:digit:]]{6}}");
|
||||
assert!(re.is_match("/012345"));
|
||||
assert!(!re.is_match("/012"));
|
||||
assert!(!re.is_match("/01234567"));
|
||||
@@ -341,7 +379,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_parse_urlencoded_param() {
|
||||
let re = Pattern::new("/user/{id}/test");
|
||||
let re = ResourceDef::new("/user/{id}/test");
|
||||
|
||||
let mut path = Path::new("/user/2345/test");
|
||||
assert!(re.match_path(&mut path));
|
||||
@@ -359,14 +397,14 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_resource_prefix() {
|
||||
let re = Pattern::prefix("/name");
|
||||
let re = ResourceDef::prefix("/name");
|
||||
assert!(re.is_match("/name"));
|
||||
assert!(re.is_match("/name/"));
|
||||
assert!(re.is_match("/name/test/test"));
|
||||
assert!(re.is_match("/name1"));
|
||||
assert!(re.is_match("/name~"));
|
||||
|
||||
let re = Pattern::prefix("/name/");
|
||||
let re = ResourceDef::prefix("/name/");
|
||||
assert!(re.is_match("/name/"));
|
||||
assert!(re.is_match("/name/gs"));
|
||||
assert!(!re.is_match("/name"));
|
||||
@@ -374,7 +412,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_reousrce_prefix_dynamic() {
|
||||
let re = Pattern::prefix("/{name}/");
|
||||
let re = ResourceDef::prefix("/{name}/");
|
||||
assert!(re.is_match("/name/"));
|
||||
assert!(re.is_match("/name/gs"));
|
||||
assert!(!re.is_match("/name"));
|
@@ -2,8 +2,8 @@ use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::path::Path;
|
||||
use crate::pattern::Pattern;
|
||||
use crate::RequestPath;
|
||||
use crate::resource::ResourceDef;
|
||||
use crate::ResourcePath;
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||
pub(crate) enum ResourceId {
|
||||
@@ -20,15 +20,15 @@ pub struct ResourceInfo {
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub(crate) struct ResourceMap {
|
||||
root: Option<Pattern>,
|
||||
named: HashMap<String, Pattern>,
|
||||
patterns: Vec<Pattern>,
|
||||
root: Option<ResourceDef>,
|
||||
named: HashMap<String, ResourceDef>,
|
||||
patterns: Vec<ResourceDef>,
|
||||
}
|
||||
|
||||
/// Resource router.
|
||||
pub struct Router<T> {
|
||||
rmap: Rc<ResourceMap>,
|
||||
named: HashMap<String, Pattern>,
|
||||
named: HashMap<String, ResourceDef>,
|
||||
resources: Vec<T>,
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ impl<T> Router<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn recognize<U: RequestPath>(&self, path: &mut Path<U>) -> Option<(&T, ResourceInfo)> {
|
||||
pub fn recognize<U: ResourcePath>(&self, path: &mut Path<U>) -> Option<(&T, ResourceInfo)> {
|
||||
if !path.path().is_empty() {
|
||||
for (idx, resource) in self.rmap.patterns.iter().enumerate() {
|
||||
if resource.match_path(path) {
|
||||
@@ -56,7 +56,7 @@ impl<T> Router<T> {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn recognize_mut<U: RequestPath>(
|
||||
pub fn recognize_mut<U: ResourcePath>(
|
||||
&mut self,
|
||||
path: &mut Path<U>,
|
||||
) -> Option<(&mut T, ResourceInfo)> {
|
||||
@@ -94,11 +94,11 @@ impl<'a, T> IntoIterator for &'a mut Router<T> {
|
||||
}
|
||||
|
||||
impl ResourceMap {
|
||||
fn register(&mut self, pattern: Pattern) {
|
||||
fn register(&mut self, pattern: ResourceDef) {
|
||||
self.patterns.push(pattern);
|
||||
}
|
||||
|
||||
fn register_named(&mut self, name: String, pattern: Pattern) {
|
||||
fn register_named(&mut self, name: String, pattern: ResourceDef) {
|
||||
self.patterns.push(pattern.clone());
|
||||
self.named.insert(name, pattern);
|
||||
}
|
||||
@@ -110,21 +110,30 @@ impl ResourceMap {
|
||||
|
||||
pub struct RouterBuilder<T> {
|
||||
rmap: ResourceMap,
|
||||
named: HashMap<String, Pattern>,
|
||||
named: HashMap<String, ResourceDef>,
|
||||
resources: Vec<T>,
|
||||
}
|
||||
|
||||
impl<T> RouterBuilder<T> {
|
||||
/// Register resource for specified path.
|
||||
pub fn path(&mut self, path: &str, resource: T) {
|
||||
self.rmap.register(Pattern::new(path));
|
||||
self.rmap.register(ResourceDef::new(path));
|
||||
self.resources.push(resource);
|
||||
}
|
||||
|
||||
/// Register resource for specified path prefix.
|
||||
pub fn prefix(&mut self, prefix: &str, resource: T) {
|
||||
self.rmap.register(Pattern::prefix(prefix));
|
||||
self.rmap.register(ResourceDef::prefix(prefix));
|
||||
self.resources.push(resource);
|
||||
}
|
||||
|
||||
/// Register resource for ResourceDef
|
||||
pub fn rdef(&mut self, rdef: ResourceDef, resource: T) {
|
||||
self.rmap.register(rdef);
|
||||
self.resources.push(resource);
|
||||
}
|
||||
|
||||
/// Finish configuration and create router instance.
|
||||
pub fn finish(self) -> Router<T> {
|
||||
Router {
|
||||
rmap: Rc::new(self.rmap),
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::RequestPath;
|
||||
use crate::ResourcePath;
|
||||
|
||||
#[allow(dead_code)]
|
||||
const GEN_DELIMS: &[u8] = b":/?#[]@";
|
||||
@@ -67,7 +67,7 @@ impl Url {
|
||||
}
|
||||
}
|
||||
|
||||
impl RequestPath for Url {
|
||||
impl ResourcePath for Url {
|
||||
fn path(&self) -> &str {
|
||||
self.path()
|
||||
}
|
||||
@@ -190,11 +190,11 @@ mod tests {
|
||||
use http::{HttpTryFrom, Uri};
|
||||
|
||||
use super::*;
|
||||
use crate::{Path, Pattern};
|
||||
use crate::{Path, ResourceDef};
|
||||
|
||||
#[test]
|
||||
fn test_parse_url() {
|
||||
let re = Pattern::new("/user/{id}/test");
|
||||
let re = ResourceDef::new("/user/{id}/test");
|
||||
|
||||
let url = Uri::try_from("/user/2345/test").unwrap();
|
||||
let mut path = Path::new(Url::new(url));
|
||||
|
Reference in New Issue
Block a user