blob: 8f94f005b23164bdf7ba27321ba5f0c2e7859d53 [file] [log] [blame]
David Tolnay0d85ccd2020-08-30 20:18:13 -07001#![allow(dead_code)]
2
3use std::error::Error as StdError;
4use std::fmt::{self, Display};
David Tolnayc0e07dc2020-09-02 15:39:28 -07005use std::io::{self, Read};
David Tolnay0d85ccd2020-08-30 20:18:13 -07006use std::path::{Path, PathBuf};
7
David Tolnayf486d562020-09-02 09:26:32 -07008pub(crate) type Result<T> = std::result::Result<T, Error>;
David Tolnay0d85ccd2020-08-30 20:18:13 -07009
10#[derive(Debug)]
11pub(crate) struct Error {
12 source: io::Error,
13 message: String,
14}
15
16impl Display for Error {
17 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
18 formatter.write_str(&self.message)
19 }
20}
21
22impl StdError for Error {
23 fn source(&self) -> Option<&(dyn StdError + 'static)> {
24 Some(&self.source)
25 }
26}
27
28macro_rules! err {
29 ($io_error:expr, $fmt:expr $(, $path:expr)* $(,)?) => {
30 Err(Error {
31 source: $io_error,
32 message: format!($fmt $(, $path.display())*),
33 })
34 }
35}
36
David Tolnay0d85ccd2020-08-30 20:18:13 -070037pub(crate) fn copy(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<u64> {
38 let from = from.as_ref();
39 let to = to.as_ref();
40 match std::fs::copy(from, to) {
41 Ok(n) => Ok(n),
42 Err(e) => err!(e, "Failed to copy `{}` -> `{}`", from, to),
43 }
44}
45
46pub(crate) fn create_dir_all(path: impl AsRef<Path>) -> Result<()> {
47 let path = path.as_ref();
48 match std::fs::create_dir_all(path) {
49 Ok(()) => Ok(()),
50 Err(e) => err!(e, "Failed to create directory `{}`", path),
51 }
52}
53
54pub(crate) fn current_dir() -> Result<PathBuf> {
55 match std::env::current_dir() {
56 Ok(dir) => Ok(dir),
57 Err(e) => err!(e, "Failed to determine current directory"),
58 }
59}
60
David Tolnaydbff3c42020-08-31 00:41:53 -070061pub(crate) fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> {
David Tolnay0d85ccd2020-08-30 20:18:13 -070062 let path = path.as_ref();
David Tolnaydbff3c42020-08-31 00:41:53 -070063 match std::fs::read(path) {
David Tolnay0d85ccd2020-08-30 20:18:13 -070064 Ok(string) => Ok(string),
65 Err(e) => err!(e, "Failed to read file `{}`", path),
66 }
67}
68
David Tolnayc0e07dc2020-09-02 15:39:28 -070069pub(crate) fn read_stdin() -> Result<Vec<u8>> {
70 let mut bytes = Vec::new();
71 match io::stdin().read_to_end(&mut bytes) {
72 Ok(_len) => Ok(bytes),
73 Err(e) => err!(e, "Failed to read input from stdin"),
74 }
75}
76
David Tolnay0d85ccd2020-08-30 20:18:13 -070077pub(crate) fn remove_file(path: impl AsRef<Path>) -> Result<()> {
78 let path = path.as_ref();
79 match std::fs::remove_file(path) {
80 Ok(()) => Ok(()),
81 Err(e) => err!(e, "Failed to remove file `{}`", path),
82 }
83}
84
David Tolnaye61db0e2020-09-02 09:38:03 -070085pub(crate) fn remove_dir(path: impl AsRef<Path>) -> Result<()> {
86 let path = path.as_ref();
87 match std::fs::remove_dir(path) {
88 Ok(()) => Ok(()),
89 Err(e) => err!(e, "Failed to remove directory `{}`", path),
90 }
91}
92
David Tolnayb6614db2020-09-01 21:17:45 -070093fn symlink<'a>(
94 src: &'a Path,
95 dst: &'a Path,
96 fun: fn(&'a Path, &'a Path) -> io::Result<()>,
97) -> Result<()> {
98 match fun(src, dst) {
David Tolnay0d85ccd2020-08-30 20:18:13 -070099 Ok(()) => Ok(()),
100 Err(e) => err!(
101 e,
102 "Failed to create symlink `{}` pointing to `{}`",
103 dst,
104 src,
105 ),
106 }
107}
108
David Tolnayb6614db2020-09-01 21:17:45 -0700109#[cfg(unix)]
110#[allow(unused_imports)]
111pub(crate) use self::symlink_file as symlink_dir;
112
113#[cfg(unix)]
114pub(crate) fn symlink_file(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
115 symlink(src.as_ref(), dst.as_ref(), std::os::unix::fs::symlink)
116}
117
David Tolnay0d85ccd2020-08-30 20:18:13 -0700118#[cfg(windows)]
119pub(crate) fn symlink_file(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
David Tolnayb6614db2020-09-01 21:17:45 -0700120 symlink(
121 src.as_ref(),
122 dst.as_ref(),
123 std::os::windows::fs::symlink_file,
124 )
125}
126
127#[cfg(windows)]
128pub(crate) fn symlink_dir(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
129 symlink(
130 src.as_ref(),
131 dst.as_ref(),
132 std::os::windows::fs::symlink_dir,
133 )
David Tolnay0d85ccd2020-08-30 20:18:13 -0700134}
135
136pub(crate) fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()> {
137 let path = path.as_ref();
138 match std::fs::write(path, contents) {
139 Ok(()) => Ok(()),
140 Err(e) => err!(e, "Failed to write file `{}`", path),
141 }
142}