pub fn truncate(dir_string: &str, length: usize) -> Option<String> {
if length == 0 {
return None;
}
let mut components = dir_string.split('/').collect::<Vec<&str>>();
if components[0].is_empty() {
components.remove(0);
}
if components.len() <= length {
return None;
}
let truncated_components = &components[components.len() - length..];
Some(truncated_components.join("/"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn truncate_smaller_path_than_provided_length() {
let path = "~/starship";
let output = truncate(path, 3);
assert_eq!(output, None)
}
#[test]
fn truncate_same_path_as_provided_length() {
let path = "~/starship/engines";
let output = truncate(path, 3);
assert_eq!(output, None)
}
#[test]
fn truncate_slightly_larger_path_than_provided_length() {
let path = "~/starship/engines/booster";
let output = truncate(path, 3);
assert_eq!(output.as_deref(), Some("starship/engines/booster"))
}
#[test]
fn truncate_larger_path_than_provided_length() {
let path = "~/starship/engines/booster/rocket";
let output = truncate(path, 3);
assert_eq!(output.as_deref(), Some("engines/booster/rocket"));
}
#[test]
fn truncate_same_path_as_provided_length_from_root() {
let path = "/starship/engines/booster";
let output = truncate(path, 3);
assert_eq!(output, None);
}
#[test]
fn truncate_larger_path_than_provided_length_from_root() {
let path = "/starship/engines/booster/rocket";
let output = truncate(path, 3);
assert_eq!(output.as_deref(), Some("engines/booster/rocket"));
}
}