| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 1 | #![allow(dead_code)] |
| 2 | |
| 3 | use std::error::Error as StdError; |
| 4 | use std::fmt::{self, Display}; |
| David Tolnay | c0e07dc | 2020-09-02 15:39:28 -0700 | [diff] [blame] | 5 | use std::io::{self, Read}; |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 6 | use std::path::{Path, PathBuf}; |
| 7 | |
| David Tolnay | f486d56 | 2020-09-02 09:26:32 -0700 | [diff] [blame] | 8 | pub(crate) type Result<T> = std::result::Result<T, Error>; |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 9 | |
| 10 | #[derive(Debug)] |
| 11 | pub(crate) struct Error { |
| 12 | source: io::Error, |
| 13 | message: String, |
| 14 | } |
| 15 | |
| 16 | impl Display for Error { |
| 17 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 18 | formatter.write_str(&self.message) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | impl StdError for Error { |
| 23 | fn source(&self) -> Option<&(dyn StdError + 'static)> { |
| 24 | Some(&self.source) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | macro_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 Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 37 | pub(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 | |
| 46 | pub(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 | |
| 54 | pub(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 Tolnay | dbff3c4 | 2020-08-31 00:41:53 -0700 | [diff] [blame] | 61 | pub(crate) fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> { |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 62 | let path = path.as_ref(); |
| David Tolnay | dbff3c4 | 2020-08-31 00:41:53 -0700 | [diff] [blame] | 63 | match std::fs::read(path) { |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 64 | Ok(string) => Ok(string), |
| 65 | Err(e) => err!(e, "Failed to read file `{}`", path), |
| 66 | } |
| 67 | } |
| 68 | |
| David Tolnay | c0e07dc | 2020-09-02 15:39:28 -0700 | [diff] [blame] | 69 | pub(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 Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 77 | pub(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 Tolnay | e61db0e | 2020-09-02 09:38:03 -0700 | [diff] [blame] | 85 | pub(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 Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 93 | fn 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 Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 99 | Ok(()) => Ok(()), |
| 100 | Err(e) => err!( |
| 101 | e, |
| 102 | "Failed to create symlink `{}` pointing to `{}`", |
| 103 | dst, |
| 104 | src, |
| 105 | ), |
| 106 | } |
| 107 | } |
| 108 | |
| David Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 109 | #[cfg(unix)] |
| 110 | #[allow(unused_imports)] |
| 111 | pub(crate) use self::symlink_file as symlink_dir; |
| 112 | |
| 113 | #[cfg(unix)] |
| 114 | pub(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 Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 118 | #[cfg(windows)] |
| 119 | pub(crate) fn symlink_file(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> { |
| David Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 120 | symlink( |
| 121 | src.as_ref(), |
| 122 | dst.as_ref(), |
| 123 | std::os::windows::fs::symlink_file, |
| 124 | ) |
| 125 | } |
| 126 | |
| 127 | #[cfg(windows)] |
| 128 | pub(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 Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | pub(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 | } |