aboutsummaryrefslogtreecommitdiff
path: root/src/utilities.rs
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-07-08 04:09:36 +0000
committerFuwn <[email protected]>2025-07-08 04:09:36 +0000
commit47df6e61ba4c63ef690081055ad551bf340d9c19 (patch)
tree16a19278fe248c77a83531cb92d86790d26107ae /src/utilities.rs
parentchore: Suggest stable Rust toolchain (diff)
downloadwindmark-47df6e61ba4c63ef690081055ad551bf340d9c19.tar.xz
windmark-47df6e61ba4c63ef690081055ad551bf340d9c19.zip
refactor(router): Manually implement trailing slash clipper
Diffstat (limited to 'src/utilities.rs')
-rw-r--r--src/utilities.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/utilities.rs b/src/utilities.rs
index 1eb4831..663c26f 100644
--- a/src/utilities.rs
+++ b/src/utilities.rs
@@ -40,3 +40,30 @@ pub fn params_to_hashmap(
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect()
}
+
+/// Normalizes a path by removing all trailing slashes, unless it's the root
+/// path "/".
+///
+/// # Examples
+///
+/// ```rust
+/// assert_eq!(
+/// windmark::utilities::normalize_path_slashes("/foo///"),
+/// "/foo"
+/// );
+/// assert_eq!(windmark::utilities::normalize_path_slashes("/foo/"), "/foo");
+/// assert_eq!(windmark::utilities::normalize_path_slashes("/foo"), "/foo");
+/// assert_eq!(windmark::utilities::normalize_path_slashes("/"), "/");
+/// ```
+#[must_use]
+pub fn normalize_path_slashes(path: &str) -> String {
+ if path == "/" {
+ return "/".to_string();
+ }
+
+ if path.ends_with('/') {
+ path.trim_end_matches('/').to_string()
+ } else {
+ path.to_string()
+ }
+}