From 9570c1cccd25bdf58f6d879e29096d6f0cd6a435 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 15 Jul 2018 15:24:27 +0600 Subject: [PATCH] rename RouteInfo --- src/helpers.rs | 10 +++--- src/httprequest.rs | 34 ++++++++---------- src/lib.rs | 2 +- src/router.rs | 90 +++++++++++++++++++++++----------------------- 4 files changed, 66 insertions(+), 70 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 50a9bcf6a..85247123a 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -92,7 +92,7 @@ impl Handler for NormalizePath { // merge slashes let p = self.re_merge.replace_all(req.path(), "/"); if p.len() != req.path().len() { - if req.route().has_route(p.as_ref()) { + if req.resource().has_route(p.as_ref()) { let p = if !query.is_empty() { p + "?" + query } else { @@ -105,7 +105,7 @@ impl Handler for NormalizePath { // merge slashes and append trailing slash if self.append && !p.ends_with('/') { let p = p.as_ref().to_owned() + "/"; - if req.route().has_route(&p) { + if req.resource().has_route(&p) { let p = if !query.is_empty() { p + "?" + query } else { @@ -120,7 +120,7 @@ impl Handler for NormalizePath { // try to remove trailing slash if p.ends_with('/') { let p = p.as_ref().trim_right_matches('/'); - if req.route().has_route(p) { + if req.resource().has_route(p) { let mut req = HttpResponse::build(self.redirect); return if !query.is_empty() { req.header( @@ -135,7 +135,7 @@ impl Handler for NormalizePath { } else if p.ends_with('/') { // try to remove trailing slash let p = p.as_ref().trim_right_matches('/'); - if req.route().has_route(p) { + if req.resource().has_route(p) { let mut req = HttpResponse::build(self.redirect); return if !query.is_empty() { req.header( @@ -151,7 +151,7 @@ impl Handler for NormalizePath { // append trailing slash if self.append && !req.path().ends_with('/') { let p = req.path().to_owned() + "/"; - if req.route().has_route(&p) { + if req.resource().has_route(&p) { let p = if !query.is_empty() { p + "?" + query } else { diff --git a/src/httprequest.rs b/src/httprequest.rs index a04973381..91ee9eb13 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -20,7 +20,7 @@ use httpresponse::{HttpResponse, HttpResponseBuilder}; use info::ConnectionInfo; use param::Params; use payload::Payload; -use router::{ResourceDef, RouteInfo}; +use router::ResourceInfo; use server::Request; struct Query(HashMap); @@ -30,7 +30,7 @@ struct Cookies(Vec>); pub struct HttpRequest { req: Option, state: Rc, - route: RouteInfo, + resource: ResourceInfo, } impl HttpMessage for HttpRequest { @@ -61,10 +61,12 @@ impl Deref for HttpRequest { impl HttpRequest { #[inline] - pub(crate) fn new(req: Request, state: Rc, route: RouteInfo) -> HttpRequest { + pub(crate) fn new( + req: Request, state: Rc, resource: ResourceInfo, + ) -> HttpRequest { HttpRequest { state, - route, + resource, req: Some(req), } } @@ -75,17 +77,17 @@ impl HttpRequest { HttpRequest { state, req: self.req.as_ref().map(|r| r.clone()), - route: self.route.clone(), + resource: self.resource.clone(), } } #[inline] /// Construct new http request with new RouteInfo. - pub(crate) fn with_route_info(&self, mut route: RouteInfo) -> HttpRequest { - route.merge(&self.route); + pub(crate) fn with_route_info(&self, mut resource: ResourceInfo) -> HttpRequest { + resource.merge(&self.resource); HttpRequest { - route, + resource, req: self.req.as_ref().map(|r| r.clone()), state: self.state.clone(), } @@ -193,7 +195,7 @@ impl HttpRequest { U: IntoIterator, I: AsRef, { - self.route.url_for(&self, name, elements) + self.resource.url_for(&self, name, elements) } /// Generate url for named resource @@ -207,14 +209,8 @@ impl HttpRequest { /// This method returns reference to current `RouteInfo` object. #[inline] - pub fn route(&self) -> &RouteInfo { - &self.route - } - - /// This method returns reference to matched `Resource` object. - #[inline] - pub fn resource(&self) -> Option<&ResourceDef> { - self.route.resource() + pub fn resource(&self) -> &ResourceInfo { + &self.resource } /// Peer socket address @@ -300,7 +296,7 @@ impl HttpRequest { /// access the matched value for that segment. #[inline] pub fn match_info(&self) -> &Params { - &self.route.match_info() + &self.resource.match_info() } /// Check if request requires connection upgrade @@ -331,7 +327,7 @@ impl Clone for HttpRequest { HttpRequest { req: self.req.as_ref().map(|r| r.clone()), state: self.state.clone(), - route: self.route.clone(), + resource: self.resource.clone(), } } } diff --git a/src/lib.rs b/src/lib.rs index d61c94f35..34dcd7182 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -246,7 +246,7 @@ pub mod dev { pub use param::{FromParam, Params}; pub use resource::Resource; pub use route::Route; - pub use router::{ResourceDef, ResourceType, Router}; + pub use router::{ResourceDef, ResourceInfo, ResourceType, Router}; } pub mod http { diff --git a/src/router.rs b/src/router.rs index 8a6a263ab..d93ec9eb6 100644 --- a/src/router.rs +++ b/src/router.rs @@ -18,7 +18,7 @@ use scope::Scope; use server::Request; #[derive(Debug, Copy, Clone, PartialEq)] -pub(crate) enum RouterResource { +pub(crate) enum ResourceId { Default, Normal(u16), } @@ -43,29 +43,29 @@ pub struct Router { default: Option>, } -/// Information about current route +/// Information about current resource #[derive(Clone)] -pub struct RouteInfo { +pub struct ResourceInfo { router: Rc, - resource: RouterResource, + resource: ResourceId, params: Params, } -impl RouteInfo { +impl ResourceInfo { /// Name os the resource #[inline] pub fn name(&self) -> &str { - if let RouterResource::Normal(idx) = self.resource { + if let ResourceId::Normal(idx) = self.resource { self.router.patterns[idx as usize].name() } else { "" } } - /// This method returns reference to matched `Resource` object. + /// This method returns reference to matched `ResourceDef` object. #[inline] - pub fn resource(&self) -> Option<&ResourceDef> { - if let RouterResource::Normal(idx) = self.resource { + pub fn rdef(&self) -> Option<&ResourceDef> { + if let ResourceId::Normal(idx) = self.resource { Some(&self.router.patterns[idx as usize]) } else { None @@ -84,7 +84,7 @@ impl RouteInfo { } #[inline] - pub(crate) fn merge(&mut self, info: &RouteInfo) { + pub(crate) fn merge(&mut self, info: &ResourceInfo) { let mut p = info.params.clone(); p.set_tail(self.params.tail); for item in &self.params.segments { @@ -185,32 +185,32 @@ impl Router { } #[inline] - pub(crate) fn route_info_params(&self, idx: u16, params: Params) -> RouteInfo { - RouteInfo { + pub(crate) fn route_info_params(&self, idx: u16, params: Params) -> ResourceInfo { + ResourceInfo { params, router: self.defs.clone(), - resource: RouterResource::Normal(idx), + resource: ResourceId::Normal(idx), } } #[cfg(test)] - pub(crate) fn route_info(&self, req: &Request, prefix: u16) -> RouteInfo { + pub(crate) fn route_info(&self, req: &Request, prefix: u16) -> ResourceInfo { let mut params = Params::with_url(req.url()); params.set_tail(prefix); - RouteInfo { + ResourceInfo { params, router: self.defs.clone(), - resource: RouterResource::Default, + resource: ResourceId::Default, } } #[cfg(test)] - pub(crate) fn default_route_info(&self) -> RouteInfo { - RouteInfo { + pub(crate) fn default_route_info(&self) -> ResourceInfo { + ResourceInfo { params: Params::new(), router: self.defs.clone(), - resource: RouterResource::Default, + resource: ResourceId::Default, } } @@ -326,9 +326,9 @@ impl Router { /// Handle request pub fn handle(&self, req: &HttpRequest) -> AsyncResult { - let resource = match req.route().resource { - RouterResource::Normal(idx) => &self.resources[idx as usize], - RouterResource::Default => { + let resource = match req.resource().resource { + ResourceId::Normal(idx) => &self.resources[idx as usize], + ResourceId::Default => { if let Some(ref default) = self.default { if let Some(id) = default.get_route_id(req) { return default.handle(id, req); @@ -356,14 +356,14 @@ impl Router { } /// Query for matched resource - pub fn recognize(&self, req: &Request, state: &S, tail: usize) -> RouteInfo { + pub fn recognize(&self, req: &Request, state: &S, tail: usize) -> ResourceInfo { self.match_with_params(req, state, tail, true) } /// Query for matched resource pub(crate) fn match_with_params( &self, req: &Request, state: &S, tail: usize, insert: bool, - ) -> RouteInfo { + ) -> ResourceInfo { if tail <= req.path().len() { 'outer: for (idx, resource) in self.patterns.iter().enumerate() { match resource { @@ -397,10 +397,10 @@ impl Router { } } } - RouteInfo { + ResourceInfo { params: Params::new(), router: self.defs.clone(), - resource: RouterResource::Default, + resource: ResourceId::Default, } } } @@ -806,35 +806,35 @@ mod tests { let req = TestRequest::with_uri("/name").finish(); let info = router.recognize(&req, &(), 0); - assert_eq!(info.resource, RouterResource::Normal(0)); + assert_eq!(info.resource, ResourceId::Normal(0)); assert!(info.match_info().is_empty()); let req = TestRequest::with_uri("/name/value").finish(); let info = router.recognize(&req, &(), 0); - assert_eq!(info.resource, RouterResource::Normal(1)); + assert_eq!(info.resource, ResourceId::Normal(1)); assert_eq!(info.match_info().get("val").unwrap(), "value"); assert_eq!(&info.match_info()["val"], "value"); let req = TestRequest::with_uri("/name/value2/index.html").finish(); let info = router.recognize(&req, &(), 0); - assert_eq!(info.resource, RouterResource::Normal(2)); + assert_eq!(info.resource, ResourceId::Normal(2)); assert_eq!(info.match_info().get("val").unwrap(), "value2"); let req = TestRequest::with_uri("/file/file.gz").finish(); let info = router.recognize(&req, &(), 0); - assert_eq!(info.resource, RouterResource::Normal(3)); + assert_eq!(info.resource, ResourceId::Normal(3)); assert_eq!(info.match_info().get("file").unwrap(), "file"); assert_eq!(info.match_info().get("ext").unwrap(), "gz"); let req = TestRequest::with_uri("/vtest/ttt/index.html").finish(); let info = router.recognize(&req, &(), 0); - assert_eq!(info.resource, RouterResource::Normal(4)); + assert_eq!(info.resource, ResourceId::Normal(4)); assert_eq!(info.match_info().get("val").unwrap(), "test"); assert_eq!(info.match_info().get("val2").unwrap(), "ttt"); let req = TestRequest::with_uri("/v/blah-blah/index.html").finish(); let info = router.recognize(&req, &(), 0); - assert_eq!(info.resource, RouterResource::Normal(5)); + assert_eq!(info.resource, ResourceId::Normal(5)); assert_eq!( info.match_info().get("tail").unwrap(), "blah-blah/index.html" @@ -842,12 +842,12 @@ mod tests { let req = TestRequest::with_uri("/test2/index.html").finish(); let info = router.recognize(&req, &(), 0); - assert_eq!(info.resource, RouterResource::Normal(6)); + assert_eq!(info.resource, ResourceId::Normal(6)); assert_eq!(info.match_info().get("test").unwrap(), "index"); let req = TestRequest::with_uri("/bbb/index.html").finish(); let info = router.recognize(&req, &(), 0); - assert_eq!(info.resource, RouterResource::Normal(7)); + assert_eq!(info.resource, ResourceId::Normal(7)); assert_eq!(info.match_info().get("test").unwrap(), "bbb"); } @@ -859,11 +859,11 @@ mod tests { let req = TestRequest::with_uri("/index.json").finish(); let info = router.recognize(&req, &(), 0); - assert_eq!(info.resource, RouterResource::Normal(0)); + assert_eq!(info.resource, ResourceId::Normal(0)); let req = TestRequest::with_uri("/test.json").finish(); let info = router.recognize(&req, &(), 0); - assert_eq!(info.resource, RouterResource::Normal(1)); + assert_eq!(info.resource, ResourceId::Normal(1)); } #[test] @@ -875,15 +875,15 @@ mod tests { let req = TestRequest::with_uri("/name").finish(); let info = router.recognize(&req, &(), 5); - assert_eq!(info.resource, RouterResource::Default); + assert_eq!(info.resource, ResourceId::Default); let req = TestRequest::with_uri("/test/name").finish(); let info = router.recognize(&req, &(), 5); - assert_eq!(info.resource, RouterResource::Normal(0)); + assert_eq!(info.resource, ResourceId::Normal(0)); let req = TestRequest::with_uri("/test/name/value").finish(); let info = router.recognize(&req, &(), 5); - assert_eq!(info.resource, RouterResource::Normal(1)); + assert_eq!(info.resource, ResourceId::Normal(1)); assert_eq!(info.match_info().get("val").unwrap(), "value"); assert_eq!(&info.match_info()["val"], "value"); @@ -895,19 +895,19 @@ mod tests { let req = TestRequest::with_uri("/name").finish(); let info = router.recognize(&req, &(), 6); - assert_eq!(info.resource, RouterResource::Default); + assert_eq!(info.resource, ResourceId::Default); let req = TestRequest::with_uri("/test2/name").finish(); let info = router.recognize(&req, &(), 6); - assert_eq!(info.resource, RouterResource::Normal(0)); + assert_eq!(info.resource, ResourceId::Normal(0)); let req = TestRequest::with_uri("/test2/name-test").finish(); let info = router.recognize(&req, &(), 6); - assert_eq!(info.resource, RouterResource::Default); + assert_eq!(info.resource, ResourceId::Default); let req = TestRequest::with_uri("/test2/name/ttt").finish(); let info = router.recognize(&req, &(), 6); - assert_eq!(info.resource, RouterResource::Normal(1)); + assert_eq!(info.resource, ResourceId::Normal(1)); assert_eq!(&info.match_info()["val"], "ttt"); } @@ -1005,13 +1005,13 @@ mod tests { let req = TestRequest::with_uri("/index.json").finish(); let info = router.recognize(&req, &(), 0); - assert_eq!(info.resource, RouterResource::Normal(0)); + assert_eq!(info.resource, ResourceId::Normal(0)); assert_eq!(info.name(), "r1"); let req = TestRequest::with_uri("/test.json").finish(); let info = router.recognize(&req, &(), 0); - assert_eq!(info.resource, RouterResource::Normal(1)); + assert_eq!(info.resource, ResourceId::Normal(1)); assert_eq!(info.name(), "r2"); } }