mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
simplify RouteRecognizer reuse
This commit is contained in:
parent
bea8e4825d
commit
d9b89ccdac
21
src/dev.rs
21
src/dev.rs
@ -7,26 +7,9 @@
|
|||||||
//! # #![allow(unused_imports)]
|
//! # #![allow(unused_imports)]
|
||||||
//! use actix_web::dev::*;
|
//! use actix_web::dev::*;
|
||||||
//! ```
|
//! ```
|
||||||
pub use ws;
|
|
||||||
pub use httpcodes;
|
|
||||||
pub use error::ParseError;
|
|
||||||
pub use application::{Application, ApplicationBuilder, Middleware};
|
|
||||||
pub use httprequest::HttpRequest;
|
|
||||||
pub use httpresponse::{Body, HttpResponse, HttpResponseBuilder};
|
|
||||||
pub use payload::{Payload, PayloadItem, PayloadError};
|
|
||||||
pub use resource::{Reply, Resource};
|
|
||||||
pub use route::{Route, RouteFactory, RouteHandler};
|
|
||||||
pub use recognizer::Params;
|
|
||||||
pub use logger::Logger;
|
|
||||||
pub use server::HttpServer;
|
|
||||||
pub use context::HttpContext;
|
|
||||||
pub use staticfiles::StaticFiles;
|
|
||||||
|
|
||||||
// re-exports
|
pub use super::*;
|
||||||
pub use http::{Method, StatusCode};
|
|
||||||
pub use cookie::{Cookie, CookieBuilder};
|
|
||||||
pub use cookie::{ParseError as CookieParseError};
|
|
||||||
pub use http_range::{HttpRange, HttpRangeParseError};
|
|
||||||
|
|
||||||
// dev specific
|
// dev specific
|
||||||
pub use task::Task;
|
pub use task::Task;
|
||||||
|
pub use recognizer::RouteRecognizer;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::string::ToString;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use regex::{Regex, RegexSet, Captures};
|
use regex::{Regex, RegexSet, Captures};
|
||||||
@ -11,7 +12,19 @@ pub struct RouteRecognizer<T> {
|
|||||||
routes: Vec<(Pattern, T)>,
|
routes: Vec<(Pattern, T)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Default for RouteRecognizer<T> {
|
||||||
|
|
||||||
|
fn default() -> Self {
|
||||||
|
RouteRecognizer {
|
||||||
|
prefix: 0,
|
||||||
|
patterns: RegexSet::new([""].iter()).unwrap(),
|
||||||
|
routes: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> RouteRecognizer<T> {
|
impl<T> RouteRecognizer<T> {
|
||||||
|
|
||||||
pub fn new(prefix: String, routes: Vec<(String, T)>) -> Self {
|
pub fn new(prefix: String, routes: Vec<(String, T)>) -> Self {
|
||||||
let mut paths = Vec::new();
|
let mut paths = Vec::new();
|
||||||
let mut handlers = Vec::new();
|
let mut handlers = Vec::new();
|
||||||
@ -29,6 +42,22 @@ impl<T> RouteRecognizer<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_routes(&mut self, routes: Vec<(&str, T)>) {
|
||||||
|
let mut paths = Vec::new();
|
||||||
|
let mut handlers = Vec::new();
|
||||||
|
for item in routes {
|
||||||
|
let pat = parse(item.0);
|
||||||
|
handlers.push((Pattern::new(&pat), item.1));
|
||||||
|
paths.push(pat);
|
||||||
|
};
|
||||||
|
self.patterns = RegexSet::new(&paths).unwrap();
|
||||||
|
self.routes = handlers;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_prefix<P: ToString>(&mut self, prefix: P) {
|
||||||
|
self.prefix = prefix.to_string().len() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn recognize(&self, path: &str) -> Option<(Option<Params>, &T)> {
|
pub fn recognize(&self, path: &str) -> Option<(Option<Params>, &T)> {
|
||||||
if let Some(idx) = self.patterns.matches(&path[self.prefix..]).into_iter().next()
|
if let Some(idx) = self.patterns.matches(&path[self.prefix..]).into_iter().next()
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user