Skip to main content

better_url/parts/path/entire/segmented/special_not_file/
remove.rs

1//! Removers.
2
3use crate::prelude::*;
4
5impl SpecialNotFilePath<'_> {
6    /// Remove the last segment if it exists.
7    /// # Errors
8    /// If there's only one segment, returns the error [`CantBeEmpty`].
9    #[expect(clippy::missing_panics_doc, reason = "Can't happen.")]
10    pub fn pop(&mut self) -> Result<bool, SetPathError> {
11        match self.0.memrchr(b'/').expect("???") {
12            0 => Err(CantBeEmpty)?,
13            x => unsafe {self.0.truncate_unchecked(x)}
14        }
15
16        Ok(true)
17    }
18
19    /// Remove the last segment if it exists and is empty.
20    /// # Errors
21    /// If there is only one segment and it's empty, returns the error [`CantBeNone`].
22    pub fn pop_if_empty(&mut self) -> Result<bool, SetPathError> {
23        Ok(match self.as_str().as_bytes() {
24            [        b'/'] => Err(CantBeEmpty)?,
25            [x @ .., b'/'] => {
26                unsafe {
27                    self.0.truncate_unchecked(x.len())
28                }
29
30                true
31            },
32            _ => false
33        })
34    }
35
36    /// Remove the `index`th segment if it exists.
37    /// # Errors
38    /// If there is only one segment, returns the error [`CantBeEmpty`].
39    pub fn remove(&mut self, index: isize) -> Result<bool, SetPathError> {
40        Ok(match self.get_str(index) {
41            Some(temp) => {
42                let mut range = self.as_str().my_substr_range(temp);
43                range.start -= 1;
44
45                if range.len() == self.len() {
46                    Err(CantBeEmpty)?;
47                }
48
49                self.0.replace_range(range, "");
50
51                true
52            },
53            None => false
54        })
55    }
56}