return the route name instead

This commit is contained in:
takashiidobe 2020-06-23 18:54:51 -04:00
parent 168286ac0b
commit 6e53e14348
3 changed files with 15 additions and 10 deletions

View File

@ -4,7 +4,7 @@ use std::{fmt, net};
use actix_http::http::{HeaderMap, Method, Uri, Version}; use actix_http::http::{HeaderMap, Method, Uri, Version};
use actix_http::{Error, Extensions, HttpMessage, Message, Payload, RequestHead}; use actix_http::{Error, Extensions, HttpMessage, Message, Payload, RequestHead};
use actix_router::{Path, Url, ResourceDef}; use actix_router::{Path, Url};
use futures_util::future::{ok, Ready}; use futures_util::future::{ok, Ready};
use tinyvec::TinyVec; use tinyvec::TinyVec;
@ -139,7 +139,7 @@ impl HttpRequest {
/// Checks if a given path matches a route /// Checks if a given path matches a route
#[inline] #[inline]
pub fn match_name(&self) -> Option<&ResourceDef> { pub fn match_name(&self) -> Option<String> {
self.0.rmap.match_name(&self.path()) self.0.rmap.match_name(&self.path())
} }
@ -482,10 +482,10 @@ mod tests {
.header(header::HOST, "www.rust-lang.org") .header(header::HOST, "www.rust-lang.org")
.rmap(rmap) .rmap(rmap)
.to_http_request(); .to_http_request();
let route_name = req.resource_map().match_name("index").unwrap().name(); let route_name = req.0.rmap.match_name("index");
assert_eq!( assert_eq!(
route_name, route_name.unwrap(),
rdef.name() rdef.name().to_owned()
); );
} }
@ -645,8 +645,8 @@ mod tests {
.service(web::resource("/profile").route(web::get().to( .service(web::resource("/profile").route(web::get().to(
move |req: HttpRequest| { move |req: HttpRequest| {
assert_eq!( assert_eq!(
req.match_name(), req.0.rmap.match_name("/user/22/profile"),
Some(&ResourceDef::new("/profile")) Some(ResourceDef::new("/profile").name().to_owned())
); );
HttpResponse::Ok().finish() HttpResponse::Ok().finish()

View File

@ -94,9 +94,14 @@ impl ResourceMap {
} }
/// Returns the full resource pattern matched against a route or None if no match is /// Returns the full resource pattern matched against a route or None if no match is
/// found. /// found.
pub fn match_name(&self, path: &str) -> Option<&ResourceDef> { pub fn match_name(&self, path: &str) -> Option<String> {
let path = if path.is_empty() { "/" } else { path }; let path = if path.is_empty() { "/" } else { path };
self.named.get(path) let route = self.named.get(path);
match route {
Some(route) => Some(route.name().to_string()),
None => None,
}
} }
/// Returns the full resource pattern matched against a path or None if no full match /// Returns the full resource pattern matched against a path or None if no full match

View File

@ -198,7 +198,7 @@ impl ServiceRequest {
/// Counterpart to [`HttpRequest::match_name`](../struct.HttpRequest.html#method.match_name). /// Counterpart to [`HttpRequest::match_name`](../struct.HttpRequest.html#method.match_name).
#[inline] #[inline]
pub fn match_name(&self) -> Option<&ResourceDef> { pub fn match_name(&self) -> Option<String> {
self.0.match_name() self.0.match_name()
} }