From 2cab6a16ed2619db5ebf5c300fa572bdb3948099 Mon Sep 17 00:00:00 2001 From: theMackabu Date: Sat, 9 Dec 2023 20:39:35 -0800 Subject: [PATCH] v0.2.3: support path extensions --- Cargo.lock | 2 +- Cargo.toml | 2 +- Maidfile.toml | 2 +- app.routes | 6 +++--- src/main.rs | 15 ++++++++++++--- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 32e72f3..4aed173 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1302,7 +1302,7 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "script" -version = "0.2.2" +version = "0.2.3" dependencies = [ "actix-web", "fancy-regex", diff --git a/Cargo.toml b/Cargo.toml index 3370512..547fe23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "script" -version = "0.2.2" +version = "0.2.3" edition = "2021" license = "MIT" repository = "https://lab.themackabu.dev/self/script" diff --git a/Maidfile.toml b/Maidfile.toml index dc0c723..8a02deb 100644 --- a/Maidfile.toml +++ b/Maidfile.toml @@ -1,6 +1,6 @@ [project] name = "script" -version = "0.2.2" +version = "0.2.3" [tasks] clean = { script = ["rm -rf bin", "mkdir bin"] } diff --git a/app.routes b/app.routes index 5719f4b..f5d3cfd 100644 --- a/app.routes +++ b/app.routes @@ -11,12 +11,12 @@ example() { html(http::get("https://example.org").body) } -#[route("/example/{id}")] +#[route("/example/{id}.txt")] example(id) { text("base: " + id) } -#[route("/example/{id}/test.json")] +#[route("/example/{id}/test")] example_test(id) { text("sub: " + id) } @@ -75,5 +75,5 @@ test/loadfile() { } 404 { - text("404 page\n\n") + text("404 page") } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9c71d1b..110c7b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,13 +72,14 @@ fn match_route(route_template: &str, placeholders: &[&str], url: &str) -> Option for (route_segment, url_segment) in route_segments.iter().zip(url_segments.iter()) { if let Some(placeholder_value) = match_segment(route_segment, url_segment, placeholders) { - matched_placeholders.push(placeholder_value); + if !placeholder_value.is_empty() { + matched_placeholders.push(placeholder_value); + } } else { return None; } } - matched_placeholders.retain(|x| x != ""); Some(matched_placeholders) } @@ -93,7 +94,13 @@ fn match_segment(route_segment: &str, url_segment: &str, placeholders: &[&str]) } else if route_segment == url_segment { Some("".to_string()) } else { - None + let route_parts: Vec<&str> = route_segment.split('.').collect(); + let url_parts: Vec<&str> = url_segment.split('.').collect(); + if route_parts.len() == url_parts.len() && route_parts.last() == url_parts.last() { + match_segment(route_parts[0], url_parts[0], placeholders) + } else { + None + } } } @@ -427,6 +434,8 @@ async fn handler(url: Path, req: HttpRequest) -> impl Responder { } }; + let status_code = ternary!(has_wildcard, status_code, StatusCode::NOT_FOUND); + println!("{}: {} (status={}, type={})", req.method(), req.uri(), status_code, content_type); return HttpResponse::build(status_code).content_type(content_type).body(body); } -- GitLab