#[derive(Clone, Debug)]
pub struct PathComponents<'a> {
remaining: Option<&'a str>,
}
impl<'a> PathComponents<'a> {
pub(crate) fn new(text: &'a str, absolute: bool, literal: bool) -> Self {
let text = if absolute && !literal {
text.strip_prefix('/').unwrap_or(text)
} else {
text
};
Self {
remaining: (!text.is_empty()).then_some(text),
}
}
}
impl<'a> Iterator for PathComponents<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
let remaining = self.remaining?;
match remaining.split_once('/') {
Some((component, rest)) => {
self.remaining = Some(rest);
Some(component)
}
None => {
self.remaining = None;
Some(remaining)
}
}
}
}