Skip to content

Commit

Permalink
Save and use router param without wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Jul 29, 2024
1 parent 8184414 commit 18ee97d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
30 changes: 18 additions & 12 deletions crates/core/src/routing/filters/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ impl PathWisp for CharsWisp {
}
if chars.len() == max_width {
state.forward(max_width);
state.params.insert(self.name.clone(), chars.into_iter().collect());
insert_param_to_state(state, &self.name, chars.into_iter().collect());
return true;
}
}
if chars.len() >= self.min_width {
state.forward(chars.len());
state.params.insert(self.name.clone(), chars.into_iter().collect());
insert_param_to_state(state, &self.name, chars.into_iter().collect());
true
} else {
false
Expand All @@ -274,7 +274,7 @@ impl PathWisp for CharsWisp {
}
if chars.len() >= self.min_width {
state.forward(chars.len());
state.params.insert(self.name.clone(), chars.into_iter().collect());
insert_param_to_state(state, &self.name, chars.into_iter().collect());
true
} else {
false
Expand Down Expand Up @@ -403,7 +403,7 @@ impl PathWisp for NamedWisp {
}
if !rest.is_empty() || !self.0.starts_with("*+") {
let rest = rest.to_string();
state.params.insert(self.0.clone(), rest);
insert_param_to_state(state, &self.0, rest);
state.cursor.0 = state.parts.len();
true
} else {
Expand All @@ -416,7 +416,7 @@ impl PathWisp for NamedWisp {
}
let picked = picked.expect("picked should not be `None`").to_owned();
state.forward(picked.len());
state.params.insert(self.0.clone(), picked);
insert_param_to_state(state, &self.0, picked);
true
}
}
Expand Down Expand Up @@ -456,7 +456,7 @@ impl PathWisp for RegexWisp {
if let Some(cap) = cap {
let cap = cap.as_str().to_owned();
state.forward(cap.len());
state.params.insert(self.name.clone(), cap);
insert_param_to_state(state, &self.name, cap);
true
} else {
false
Expand All @@ -472,7 +472,7 @@ impl PathWisp for RegexWisp {
if let Some(cap) = cap {
let cap = cap.as_str().to_owned();
state.forward(cap.len());
state.params.insert(self.name.clone(), cap);
insert_param_to_state(state, &self.name, cap);
true
} else {
false
Expand Down Expand Up @@ -915,6 +915,17 @@ impl PathFilter {
}
}

#[inline]
fn insert_param_to_state(state: &mut PathState, name: &str, value: String) {
if name.starts_with("*+") || name.starts_with("*?") || name.starts_with("**") {
state.params.insert(name[2..].to_owned(), value);
} else if name.starts_with('*') {
state.params.insert(name[1..].to_owned(), value);
} else {
state.params.insert(name.to_owned(), value);
}
}

#[cfg(test)]
mod tests {
use super::PathParser;
Expand All @@ -930,11 +941,6 @@ mod tests {
let segments = PathParser::new("/").parse().unwrap();
assert!(segments.is_empty());
}
#[test]
fn test_parse_rest_without_name() {
let segments = PathParser::new("/hello/<**>").parse().unwrap();
assert_eq!(format!("{:?}", segments), r#"[ConstWisp("hello"), NamedWisp("**")]"#);
}

#[test]
fn test_parse_single_const() {
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
//!
//! #[handler]
//! fn serve_file(req: &mut Request) {
//! let rest_path = req.param::<i64>("**rest_path");
//! let rest_path = req.param::<i64>("rest_path");
//! }
//! ```
//!
Expand Down
2 changes: 1 addition & 1 deletion crates/oapi/src/swagger_ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub(crate) fn redirect_to_dir_url(req_uri: &Uri, res: &mut Response) {
#[async_trait]
impl Handler for SwaggerUi {
async fn handle(&self, req: &mut Request, _depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) {
let path = req.params().get("**").map(|s| &**s).unwrap_or_default();
let path = req.params().last().map(|(_, s)| &**s).unwrap_or_default();
// Redirect to dir url if path is empty and not end with '/'
if path.is_empty() && !req.uri().path().ends_with('/') {
redirect_to_dir_url(req.uri(), res);
Expand Down
4 changes: 2 additions & 2 deletions examples/static-embed-file/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ struct Assets;
async fn main() {
tracing_subscriber::fmt().init();

let router = Router::with_path("<*path>").get(serve_file);
let router = Router::with_path("<**rest>").get(serve_file);

let acceptor = TcpListener::new("0.0.0.0:5800").bind().await;
Server::new(acceptor).serve(router).await;
}

#[handler]
async fn serve_file(req: &mut Request, res: &mut Response) {
let path = req.param::<String>("*path").unwrap();
let path = req.param::<String>("rest").unwrap();
if let Some(file) = Assets::get(&path) {
file.render(req, res);
} else {
Expand Down

0 comments on commit 18ee97d

Please sign in to comment.