use crate::helpers::http::PercentDecoded;
const EXCLUDED_SEGMENTS: [&str; 1] = [""];
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RequestPathSegments {
segments: Vec<PercentDecoded>,
}
pub(crate) fn split_path_segments<'a>(path: &'a str) -> impl Iterator<Item = &'a str> {
path.split('/').filter(|s| !EXCLUDED_SEGMENTS.contains(s))
}
impl RequestPathSegments {
pub(crate) fn new(path: &str) -> Self {
let segments = split_path_segments(path)
.filter_map(PercentDecoded::new)
.collect();
RequestPathSegments { segments }
}
pub(crate) fn subsegments(&self, offset: usize) -> Self {
RequestPathSegments {
segments: self.segments.split_at(offset).1.to_vec(),
}
}
pub(crate) fn segments(&self) -> &Vec<PercentDecoded> {
&self.segments
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn request_path_segments_tests() {
let rps = RequestPathSegments::new("/some/path/to//my/handler");
assert_eq!(
rps.segments.iter().map(AsRef::as_ref).collect::<Vec<_>>(),
vec!["some", "path", "to", "my", "handler"]
);
}
}