pub fn truncate(path: &Path, length: usize) -> Option<String>
Expand description
Truncates a path to only have a set number of path components.
Will truncate a path to only show the last length
components in a path.
If a length of 0
is provided, the path will not be truncated.
A value will only be returned if the path has been truncated.
§Arguments
path
- The path to truncate.length
- The number of path components to keep.
§Returns
- An
Option
of the truncated path as a string. If the path was not truncated,None
is returned.
§Example
use serde_yml::utilities::directory::truncate;
use std::path::Path;
let long_path = Path::new("home/user/documents/report/2023/05/27/file.txt");
if let Some(truncated) = truncate(long_path, 3) {
println!("Truncated path: {}", truncated);
} else {
println!("Path was not truncated.");
}