Skip to content
Myr

input.rs

This file

Path

src/exec/input.rs

87 lines · 2.8 KiB · Rust

Last changed by

Fix test on Windowssharkdpbefore adoption95eae00

History of this file

Take it

1use std::ffi::{OsStr, OsString};2use std::path::{Path, PathBuf};34use crate::filesystem::strip_current_dir;56/// Removes the parent component of the path7pub fn basename(path: &Path) -> &OsStr {8    path.file_name().unwrap_or(path.as_os_str())9}1011/// Removes the extension from the path12pub fn remove_extension(path: &Path) -> OsString {13    let dirname = dirname(path);14    let stem = path.file_stem().unwrap_or(path.as_os_str());1516    let path = PathBuf::from(dirname).join(stem);1718    strip_current_dir(&path).to_owned().into_os_string()19}2021/// Removes the basename from the path.22pub fn dirname(path: &Path) -> OsString {23    path.parent()24        .map(|p| {25            if p == OsStr::new("") {26                OsString::from(".")27            } else {28                p.as_os_str().to_owned()29            }30        })31        .unwrap_or(path.as_os_str().to_owned())32}3334#[cfg(test)]35mod path_tests {36    use super::*;37    use std::path::MAIN_SEPARATOR;3839    fn correct(input: &str) -> String {40        input.replace('/', &MAIN_SEPARATOR.to_string())41    }4243    macro_rules! func_tests {44        ($($name:ident: $func:ident for $input:expr => $output:expr)+) => {45            $(46                #[test]47                fn $name() {48                    let input_path = PathBuf::from(&correct($input));49                    let output_string = OsString::from(correct($output));50                    assert_eq!($func(&input_path), output_string);51                }52            )+53        }54    }5556    func_tests! {57        remove_ext_simple:  remove_extension  for  "foo.txt"      =>  "foo"58        remove_ext_dir:     remove_extension  for  "dir/foo.txt"  =>  "dir/foo"59        hidden:             remove_extension  for  ".foo"         =>  ".foo"60        remove_ext_utf8:    remove_extension  for  "💖.txt"       =>  "💖"61        remove_ext_empty:   remove_extension  for  ""             =>  ""6263        basename_simple:  basename  for  "foo.txt"      =>  "foo.txt"64        basename_dir:     basename  for  "dir/foo.txt"  =>  "foo.txt"65        basename_empty:   basename  for  ""             =>  ""66        basename_utf8_0:  basename  for  "💖/foo.txt"   =>  "foo.txt"67        basename_utf8_1:  basename  for  "dir/💖.txt"   =>  "💖.txt"6869        dirname_simple:  dirname  for  "foo.txt"      =>  "."70        dirname_dir:     dirname  for  "dir/foo.txt"  =>  "dir"71        dirname_utf8_0:  dirname  for  "💖/foo.txt"   =>  "💖"72        dirname_utf8_1:  dirname  for  "dir/💖.txt"   =>  "dir"73    }7475    #[test]76    #[cfg(windows)]77    fn dirname_root() {78        assert_eq!(dirname(&PathBuf::from("C:")), OsString::from("C:"));79        assert_eq!(dirname(&PathBuf::from("\\")), OsString::from("\\"));80    }8182    #[test]83    #[cfg(not(windows))]84    fn dirname_root() {85        assert_eq!(dirname(&PathBuf::from("/")), OsString::from("/"));86    }87}