cli_assert/
utils.rs

1use std::path::{Component, Path, PathBuf};
2
3pub trait PathExt {
4  fn rem(&self, p: impl AsRef<Path>) -> Option<PathBuf>;
5}
6
7impl PathExt for Path {
8  fn rem(&self, p: impl AsRef<Path>) -> Option<PathBuf> {
9    let left: Vec<Component> = self.components().collect();
10    let right: Vec<Component> = p.as_ref().components().collect();
11    let max_length = left.len().min(right.len());
12    for index in (1..=max_length).rev() {
13      if left[left.len() - index..] == right[..index] {
14        let mut path_buf = PathBuf::new();
15        for component in &right[index..] {
16          path_buf.push(component);
17        }
18        return Some(path_buf);
19      }
20    }
21    None
22  }
23}
24
25/// Pauses thread execution for specified number of milliseconds.
26pub fn sleep(millis: u64) {
27  if millis > 0 {
28    std::thread::sleep(std::time::Duration::from_millis(millis));
29  }
30}