From 7dd5fb59db9fa4991f3359c0dfc9be8a0dec31ee Mon Sep 17 00:00:00 2001 From: Mayuri Kini Date: Tue, 29 Oct 2024 00:01:29 -0700 Subject: [PATCH 1/2] Add absolute filepath support --- src/server.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/server.rs b/src/server.rs index d100352..adb53da 100644 --- a/src/server.rs +++ b/src/server.rs @@ -288,10 +288,11 @@ enum RequestType { } pub fn convert_file_path(filename: &str) -> PathBuf { + let formatted_filename = filename.trim_start_matches(|c| c == '/' || c == '\\').to_string(); let normalized_filename = if MAIN_SEPARATOR == '\\' { - filename.replace('/', "\\") + formatted_filename.replace('/', "\\") } else { - filename.replace('\\', "/") + formatted_filename.replace('\\', "/") }; PathBuf::from(normalized_filename) @@ -401,13 +402,13 @@ mod tests { correct_path.push("test.file"); assert_eq!(path, correct_path); - let path = convert_file_path("test\\test.file"); + let path = convert_file_path("\\test\\test.file"); let mut correct_path = PathBuf::new(); correct_path.push("test"); correct_path.push("test.file"); assert_eq!(path, correct_path); - let path = convert_file_path("test/test/test.file"); + let path = convert_file_path("/test/test/test.file"); let mut correct_path = PathBuf::new(); correct_path.push("test"); correct_path.push("test"); From bdefab8ca864c47b688688b0dddb96192214884f Mon Sep 17 00:00:00 2001 From: Mayuri Kini Date: Tue, 29 Oct 2024 10:39:21 -0700 Subject: [PATCH 2/2] Fixing tests --- src/server.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/server.rs b/src/server.rs index adb53da..8c8716a 100644 --- a/src/server.rs +++ b/src/server.rs @@ -402,13 +402,23 @@ mod tests { correct_path.push("test.file"); assert_eq!(path, correct_path); - let path = convert_file_path("\\test\\test.file"); + let path = convert_file_path("\\test.file"); + let mut correct_path = PathBuf::new(); + correct_path.push("test.file"); + assert_eq!(path, correct_path); + + let path = convert_file_path("/test.file"); + let mut correct_path = PathBuf::new(); + correct_path.push("test.file"); + assert_eq!(path, correct_path); + + let path = convert_file_path("test\\test.file"); let mut correct_path = PathBuf::new(); correct_path.push("test"); correct_path.push("test.file"); assert_eq!(path, correct_path); - let path = convert_file_path("/test/test/test.file"); + let path = convert_file_path("test/test/test.file"); let mut correct_path = PathBuf::new(); correct_path.push("test"); correct_path.push("test");