diff options
| author | omagdy7 <omar.professional8777@gmail.com> | 2024-06-05 18:13:08 +0300 |
|---|---|---|
| committer | omagdy7 <omar.professional8777@gmail.com> | 2024-06-05 18:13:08 +0300 |
| commit | ff75fa542a98cf9a79133a81b7716401e717bfd6 (patch) | |
| tree | c9433151c9dc21a16a822d4a8f1f6f7c526b1701 /src/extractor.rs | |
| parent | 4f9d09b58ae81e4e6144b85ce36ba424e319b779 (diff) | |
| download | tiny-server-ff75fa542a98cf9a79133a81b7716401e717bfd6.tar.xz tiny-server-ff75fa542a98cf9a79133a81b7716401e717bfd6.zip | |
codecrafters submit [skip ci]
Diffstat (limited to 'src/extractor.rs')
| -rw-r--r-- | src/extractor.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/extractor.rs b/src/extractor.rs new file mode 100644 index 0000000..edc7b2e --- /dev/null +++ b/src/extractor.rs @@ -0,0 +1,44 @@ +use regex::{escape, Regex}; + +pub fn build_regex_from_path(path_template: &str) -> Regex { + // Escape literal parts of the path to safely convert to regex + let mut regex_string = "^".to_string(); + for component in path_template.split('/') { + if component.starts_with(':') { + // Replace placeholder with regex to capture alphanumeric, underscores, or hyphens + regex_string.push_str("/([a-zA-Z0-9_-]+)"); + } else if !component.is_empty() { + // Escape and add literal components to the regex + regex_string.push('/'); + regex_string.push_str(&escape(component)); + } + } + regex_string.push_str("/?$"); + + // Compile the regex + Regex::new(®ex_string).unwrap() +} + +// pub fn match_path(method: &Method) -> String { +// use Method::*; +// match method { +// Get(route) => { +// match re.captures(route) { +// Some(caps) => { +// println!("Matched route: {}", route); +// // Iterate over the captures to extract the path segments and parameters +// for cap in caps.iter().flatten().skip(1) { +// println!("Segment: {}", cap.as_str()); +// } +// "empty".to_string() +// } +// None => { +// println!("No match for route: {}", route); +// "empty none".to_string() +// } +// } +// } +// Post(_) => todo!(), +// Put(_) => todo!(), +// } +// } |
