1use std::borrow::Borrow;
2use std::fs;
3use std::path::{Path, PathBuf};
4use shellexpand;
5
6
7pub fn make_path_buf<S: AsRef<str>>(s: S) -> ::Result<PathBuf> {
8 shellexpand::full(s.as_ref())
9 .map(|s| PathBuf::from(s.borrow() as &str))
10 .map_err(Into::into)
11}
12
13#[cfg(windows)]
14pub fn canonicalize_pretty<P: AsRef<Path>>(path: P) -> ::Result<PathBuf> {
15 path.as_ref()
16 .canonicalize()
17 .map_err(Into::into)
18 .map(|path| {
19 path.to_string_lossy()
20 .trim_left_matches(r"\\?\")
21 .replace(r"\", "/")
22 })
23 .map(|s| PathBuf::from(s))
24}
25
26
27#[cfg(not(windows))]
28pub fn canonicalize_pretty<P: AsRef<Path>>(path: P) -> ::Result<PathBuf> {
29 path.as_ref().canonicalize().map_err(Into::into)
30}
31
32
33pub trait StrSkip {
34 fn skip<'a>(&'a self, n: usize) -> &'a str;
35}
36
37impl StrSkip for str {
38 fn skip<'a>(&'a self, n: usize) -> &'a str {
39 let mut s = self.chars();
40 for _ in 0..n {
41 s.next();
42 }
43 s.as_str()
44 }
45}
46
47#[test]
48fn test_skipped_1() {
49 assert_eq!("hoge".skip(1), "oge");
50 assert_eq!("あいueo".skip(1), "いueo");
51}
52
53
54pub fn write_content<P, F>(path: P, write_fn: F) -> ::Result<()>
55where
56 P: AsRef<Path>,
57 F: FnOnce(&mut fs::File) -> ::Result<()>,
58{
59 fs::create_dir_all(path.as_ref().parent().unwrap())?;
60 let mut file = fs::OpenOptions::new()
61 .write(true)
62 .create(true)
63 .truncate(true)
64 .open(&path)?;
65 write_fn(&mut file)
66}
67
68
69pub mod process {
70 use std::process::{Command, Stdio};
71
72 pub fn inherit(name: &str) -> Command {
73 let mut command = Command::new(name);
74 command.stdin(Stdio::inherit());
75 command.stdout(Stdio::inherit());
76 command.stderr(Stdio::inherit());
77 command
78 }
79
80 pub fn piped(name: &str) -> Command {
81 let mut command = Command::new(name);
82 command.stdin(Stdio::null());
83 command.stdout(Stdio::piped());
84 command.stderr(Stdio::piped());
85 command
86 }
87}