Skip to main content

better_url/parts/path/entire/segmented/file/
get.rs

1//! Getters.
2
3use crate::prelude::*;
4
5impl FilePath<'_> {
6    /// The segments as [`str`]s.
7    pub fn iter_strs(&self) -> SplitSlashes<'_> {
8        SplitSlashes(self.as_str().strip_prefix('/'))
9    }
10
11    /// The `index`th segment as a [`str`].
12    pub fn get_str(&self, index: isize) -> Option<&str> {
13        self.iter_strs().neg_nth(index)
14    }
15
16    /// The range of segments as a [`str`].
17    pub fn range_str<B: RangeBounds<isize>>(&self, range: B) -> Option<&str> {
18        self.iter_strs().range(range)
19    }
20
21
22
23    /// The [`FilePathSegmentsIter`].
24    pub fn iter(&self) -> FilePathSegmentsIter<'_> {
25        self.into_iter()
26    }
27
28    /// The `index`th [`FilePathSegment`].
29    pub fn get(&self, index: isize) -> Option<FilePathSegment<'_>> {
30        self.iter().neg_nth(index)
31    }
32
33    /// The range of [`FilePathSegments`].
34    /// # Examples
35    /// ```
36    /// use better_url::prelude::*;
37    ///
38    /// let path = FilePath::new("/ab/cd/ef");
39    ///
40    /// assert_eq!(path.range(0..  2).unwrap(), "ab/cd");
41    /// assert_eq!(path.range(0.. -1).unwrap(), "ab/cd");
42    /// assert_eq!(path.range(0..= 2).unwrap(), "ab/cd/ef");
43    /// assert_eq!(path.range(0..=-1).unwrap(), "ab/cd/ef");
44    /// ```
45    pub fn range<B: RangeBounds<isize>>(&self, range: B) -> Option<FilePathSegments<'_>> {
46        self.iter().range(range)
47    }
48}