input.rs
This file
Path
src/exec/input.rs
87 lines · 2.8 KiB · Rust
Last changed by
Fix some clippy lints, and formatAlexandru Macoveibefore adoption4f43301
History of this fileTake it
The walk stopped after 200 commits: lines older than 320d7fc point at it.
Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
lines 1–2why, one step back1use std::ffi::{OsStr, OsString};2use std::path::{Path, PathBuf};Sort search results when using -X optionMarcoIenibefore adoption320d7fc
line 3why, one step back3Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
line 4why, one step back4use crate::filesystem::strip_current_dir;Sort search results when using -X optionMarcoIenibefore adoption320d7fc
line 5why, one step back5Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
lines 6–7why, one step back6/// Removes the parent component of the path7pub fn basename(path: &Path) -> &OsStr {Fix some clippy lints, and formatAlexandru Macoveibefore adoption4f43301
line 8why, one step back8 path.file_name().unwrap_or_else(|| path.as_os_str())Sort search results when using -X optionMarcoIenibefore adoption320d7fc
lines 9–11why, one step back9}1011/// Removes the extension from the pathReimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
lines 12–13why, one step back12pub fn remove_extension(path: &Path) -> OsString {13 let dirname = dirname(path);Fix some clippy lints, and formatAlexandru Macoveibefore adoption4f43301
line 14why, one step back14 let stem = path.file_stem().unwrap_or_else(|| path.as_os_str());Sort search results when using -X optionMarcoIenibefore adoption320d7fc
line 15why, one step back15Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
line 16why, one step back16 let path = PathBuf::from(dirname).join(stem);Sort search results when using -X optionMarcoIenibefore adoption320d7fc
line 17why, one step back17Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
line 18why, one step back18 strip_current_dir(&path).to_owned().into_os_string()Sort search results when using -X optionMarcoIenibefore adoption320d7fc
lines 19–21why, one step back19}2021/// Removes the basename from the path.Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
lines 22–30why, one step back22pub 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 })Fix some clippy lints, and formatAlexandru Macoveibefore adoption4f43301
line 31why, one step back31 .unwrap_or_else(|| path.as_os_str().to_owned())Sort search results when using -X optionMarcoIenibefore adoption320d7fc
lines 32–36why, one step back32}3334#[cfg(test)]35mod path_tests {36 use super::*;Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
line 37why, one step back37 use std::path::MAIN_SEPARATOR;Sort search results when using -X optionMarcoIenibefore adoption320d7fc
lines 38–47why, one step back3839 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() {Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
lines 48–50why, one step back48 let input_path = PathBuf::from(&correct($input));49 let output_string = OsString::from(correct($output));50 assert_eq!($func(&input_path), output_string);Sort search results when using -X optionMarcoIenibefore adoption320d7fc
lines 51–75why, one step back51 }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]Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
line 76why, one step back76 #[cfg(windows)]Sort search results when using -X optionMarcoIenibefore adoption320d7fc
line 77why, one step back77 fn dirname_root() {Fix test on Windowssharkdpbefore adoption95eae00
line 78why, one step back78 assert_eq!(dirname(&PathBuf::from("C:")), OsString::from("C:"));Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c
lines 79–85why, one step back79 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("/"));Sort search results when using -X optionMarcoIenibefore adoption320d7fc
lines 86–87why, one step back86 }87}