"Fossies" - the Fresh Open Source Software Archive

Member "fd-8.1.1/src/exec/input.rs" (25 May 2020, 2888 Bytes) of package /linux/privat/fd-8.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Rust source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "input.rs": 8.0.0_vs_8.1.0.

    1 use std::ffi::{OsStr, OsString};
    2 use std::path::{Path, PathBuf};
    3 
    4 use crate::filesystem::strip_current_dir;
    5 
    6 /// Removes the parent component of the path
    7 pub fn basename(path: &Path) -> &OsStr {
    8     path.file_name().unwrap_or_else(|| path.as_os_str())
    9 }
   10 
   11 /// Removes the extension from the path
   12 pub fn remove_extension(path: &Path) -> OsString {
   13     let dirname = dirname(path);
   14     let stem = path.file_stem().unwrap_or_else(|| path.as_os_str());
   15 
   16     let path = PathBuf::from(dirname).join(stem);
   17 
   18     strip_current_dir(&path).to_owned().into_os_string()
   19 }
   20 
   21 /// Removes the basename from the path.
   22 pub 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_else(|| path.as_os_str().to_owned())
   32 }
   33 
   34 #[cfg(test)]
   35 mod path_tests {
   36     use super::*;
   37     use std::path::MAIN_SEPARATOR;
   38 
   39     fn correct(input: &str) -> String {
   40         input.replace('/', &MAIN_SEPARATOR.to_string())
   41     }
   42 
   43     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     }
   55 
   56     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  ""             =>  ""
   62 
   63         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"
   68 
   69         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     }
   74 
   75     #[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     }
   81 
   82     #[test]
   83     #[cfg(not(windows))]
   84     fn dirname_root() {
   85         assert_eq!(dirname(&PathBuf::from("/")), OsString::from("/"));
   86     }
   87 }