1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use clap::Parser;
use rustutils_runnable::Runnable;
use std::error::Error;
use std::io::Write;
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};
#[derive(Parser, Clone, Debug)]
#[clap(author, version, about, long_about = None)]
pub struct Dirname {
#[clap(required = true)]
path: Vec<PathBuf>,
#[clap(short, long)]
zero: bool,
}
pub fn dirname(path: &Path) -> &Path {
match path.parent() {
Some(path) if path.as_os_str().len() == 0 => Path::new("."),
Some(path) => path,
None if path.as_os_str().len() == 0 => Path::new("."),
None => Path::new("/"),
}
}
impl Runnable for Dirname {
fn run(&self) -> Result<(), Box<dyn Error>> {
let mut stdout = std::io::stdout();
for path in &self.path {
stdout.write_all(dirname(&path).as_os_str().as_bytes())?;
if self.zero {
stdout.write_all(&[0])?;
} else {
stdout.write_all(&[b'\n'])?;
}
}
Ok(())
}
}
#[test]
fn dirname_absolute_works() {
assert_eq!(dirname(&Path::new("/")), Path::new("/"));
assert_eq!(dirname(&Path::new("/abc")), Path::new("/"));
assert_eq!(dirname(&Path::new("/abc/")), Path::new("/"));
assert_eq!(dirname(&Path::new("/abc/def")), Path::new("/abc"));
}
#[test]
fn dirname_relative_works() {
assert_eq!(dirname(&Path::new("")), Path::new("."));
assert_eq!(dirname(&Path::new(".")), Path::new("."));
assert_eq!(dirname(&Path::new("abc")), Path::new("."));
assert_eq!(dirname(&Path::new("./abc")), Path::new("."));
assert_eq!(dirname(&Path::new("./abc/")), Path::new("."));
assert_eq!(dirname(&Path::new("./abc/def")), Path::new("./abc"));
assert_eq!(dirname(&Path::new("../abc")), Path::new(".."));
assert_eq!(dirname(&Path::new("../abc/")), Path::new(".."));
assert_eq!(dirname(&Path::new("../../abc")), Path::new("../.."));
}