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

The walk stopped after 200 commits: lines older than 984f04f point at it.

Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c

lines 1–2why, one step back
1use std::ffi::{OsStr, OsString};
2use std::path::{Path, PathBuf};

Add print_error macros to replace functionsJosh Leeb-du Toitbefore adoption984f04f

line 3why, one step back
3

Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c

line 4why, one step back
4use crate::filesystem::strip_current_dir;

Add print_error macros to replace functionsJosh Leeb-du Toitbefore adoption984f04f

line 5why, one step back
5

Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c

lines 6–8why, one step back
6/// Removes the parent component of the path
7pub fn basename(path: &Path) -> &OsStr {8 path.file_name().unwrap_or(path.as_os_str())

Add print_error macros to replace functionsJosh Leeb-du Toitbefore adoption984f04f

lines 9–11why, one step back
9}
1011/// Removes the extension from the path

Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c

lines 12–14why, one step back
12pub fn remove_extension(path: &Path) -> OsString {
13 let dirname = dirname(path);14 let stem = path.file_stem().unwrap_or(path.as_os_str());

Add print_error macros to replace functionsJosh Leeb-du Toitbefore adoption984f04f

line 15why, one step back
15

Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c

line 16why, one step back
16 let path = PathBuf::from(dirname).join(stem);

Add print_error macros to replace functionsJosh Leeb-du Toitbefore adoption984f04f

line 17why, one step back
17

Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c

line 18why, one step back
18 strip_current_dir(&path).to_owned().into_os_string()

Add print_error macros to replace functionsJosh Leeb-du Toitbefore adoption984f04f

lines 19–21why, one step back
19}
2021/// Removes the basename from the path.

Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c

lines 22–31why, one step back
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())

Add print_error macros to replace functionsJosh Leeb-du Toitbefore adoption984f04f

lines 32–34why, one step back
32}
3334#[cfg(test)]

macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9

line 35why, one step back
35mod path_tests {

Add print_error macros to replace functionsJosh Leeb-du Toitbefore adoption984f04f

line 36why, one step back
36 use super::*;

Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c

line 37why, one step back
37 use std::path::MAIN_SEPARATOR;

Add print_error macros to replace functionsJosh Leeb-du Toitbefore adoption984f04f

lines 38–42why, one step back
38
39 fn correct(input: &str) -> String {40 input.replace('/', &MAIN_SEPARATOR.to_string())41 }42

macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9

lines 43–47why, one step back
43 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 back
48 let input_path = PathBuf::from(&correct($input));
49 let output_string = OsString::from(correct($output));50 assert_eq!($func(&input_path), output_string);

macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9

lines 51–53why, one step back
51 }
52 )+53 }

Add print_error macros to replace functionsJosh Leeb-du Toitbefore adoption984f04f

lines 54–55why, one step back
54 }
55

macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9

lines 56–61why, one step back
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 "" => ""

Add print_error macros to replace functionsJosh Leeb-du Toitbefore adoption984f04f

line 62why, one step back
62

macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9

lines 63–67why, one step back
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"

Add print_error macros to replace functionsJosh Leeb-du Toitbefore adoption984f04f

line 68why, one step back
68

macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9

lines 69–72why, one step back
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"

Add print_error macros to replace functionsJosh Leeb-du Toitbefore adoption984f04f

lines 73–75why, one step back
73 }
7475 #[test]

Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c

line 76why, one step back
76 #[cfg(windows)]

macronize repetitive tests in exec/inputAlexandru Macoveibefore adoptiond8bd5f9

line 77why, one step back
77 fn dirname_root() {

Fix test on Windowssharkdpbefore adoption95eae00

line 78why, one step back
78 assert_eq!(dirname(&PathBuf::from("C:")), OsString::from("C:"));

Reimplement dirname, basename, remove_extension for OsStrsharkdpbefore adoption7e3c69c

lines 79–85why, one step back
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("/"));

Add print_error macros to replace functionsJosh Leeb-du Toitbefore adoption984f04f

lines 86–87why, one step back
86 }
87}