• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![allow(dead_code)]
2 
3 use std::error::Error as StdError;
4 use std::fmt::{self, Display};
5 use std::io::{self, Read};
6 use std::path::{Path, PathBuf};
7 
8 pub(crate) type Result<T> = std::result::Result<T, Error>;
9 
10 #[derive(Debug)]
11 pub(crate) struct Error {
12     source: Option<io::Error>,
13     message: String,
14 }
15 
16 impl Error {
kind(&self) -> io::ErrorKind17     pub fn kind(&self) -> io::ErrorKind {
18         match &self.source {
19             Some(io_error) => io_error.kind(),
20             None => io::ErrorKind::Other,
21         }
22     }
23 }
24 
25 impl Display for Error {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result26     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
27         formatter.write_str(&self.message)
28     }
29 }
30 
31 impl StdError for Error {
source(&self) -> Option<&(dyn StdError + 'static)>32     fn source(&self) -> Option<&(dyn StdError + 'static)> {
33         let source = self.source.as_ref()?;
34         Some(source)
35     }
36 }
37 
38 macro_rules! err {
39     ($io_error:expr, $fmt:expr $(, $path:expr)* $(,)?) => {
40         Err(Error {
41             source: Option::from($io_error),
42             message: format!($fmt $(, $path.display())*),
43         })
44     }
45 }
46 
copy(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<u64>47 pub(crate) fn copy(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<u64> {
48     let from = from.as_ref();
49     let to = to.as_ref();
50     match std::fs::copy(from, to) {
51         Ok(n) => Ok(n),
52         Err(e) => err!(e, "Failed to copy `{}` -> `{}`", from, to),
53     }
54 }
55 
create_dir_all(path: impl AsRef<Path>) -> Result<()>56 pub(crate) fn create_dir_all(path: impl AsRef<Path>) -> Result<()> {
57     let path = path.as_ref();
58     match std::fs::create_dir_all(path) {
59         Ok(()) => Ok(()),
60         Err(e) => err!(e, "Failed to create directory `{}`", path),
61     }
62 }
63 
current_dir() -> Result<PathBuf>64 pub(crate) fn current_dir() -> Result<PathBuf> {
65     match std::env::current_dir() {
66         Ok(dir) => Ok(dir),
67         Err(e) => err!(e, "Failed to determine current directory"),
68     }
69 }
70 
exists(path: impl AsRef<Path>) -> bool71 pub(crate) fn exists(path: impl AsRef<Path>) -> bool {
72     let path = path.as_ref();
73     // If path is a symlink, this returns true, regardless of whether the
74     // symlink points to a path that exists.
75     std::fs::symlink_metadata(path).is_ok()
76 }
77 
read(path: impl AsRef<Path>) -> Result<Vec<u8>>78 pub(crate) fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> {
79     let path = path.as_ref();
80     match std::fs::read(path) {
81         Ok(string) => Ok(string),
82         Err(e) => err!(e, "Failed to read file `{}`", path),
83     }
84 }
85 
read_stdin() -> Result<Vec<u8>>86 pub(crate) fn read_stdin() -> Result<Vec<u8>> {
87     let mut bytes = Vec::new();
88     match io::stdin().read_to_end(&mut bytes) {
89         Ok(_len) => Ok(bytes),
90         Err(e) => err!(e, "Failed to read input from stdin"),
91     }
92 }
93 
remove_file(path: impl AsRef<Path>) -> Result<()>94 pub(crate) fn remove_file(path: impl AsRef<Path>) -> Result<()> {
95     let path = path.as_ref();
96     match std::fs::remove_file(path) {
97         Ok(()) => Ok(()),
98         Err(e) => err!(e, "Failed to remove file `{}`", path),
99     }
100 }
101 
remove_dir(path: impl AsRef<Path>) -> Result<()>102 pub(crate) fn remove_dir(path: impl AsRef<Path>) -> Result<()> {
103     let path = path.as_ref();
104     match std::fs::remove_dir(path) {
105         Ok(()) => Ok(()),
106         Err(e) => err!(e, "Failed to remove directory `{}`", path),
107     }
108 }
109 
symlink<'a>( original: &'a Path, link: &'a Path, fun: fn(&'a Path, &'a Path) -> io::Result<()>, ) -> Result<()>110 fn symlink<'a>(
111     original: &'a Path,
112     link: &'a Path,
113     fun: fn(&'a Path, &'a Path) -> io::Result<()>,
114 ) -> Result<()> {
115     match fun(original, link) {
116         Ok(()) => Ok(()),
117         Err(e) => err!(
118             e,
119             "Failed to create symlink `{}` pointing to `{}`",
120             link,
121             original,
122         ),
123     }
124 }
125 
symlink_fail(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()>126 pub(crate) fn symlink_fail(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
127     err!(
128         None,
129         "Failed to create symlink `{}` pointing to `{}`",
130         link.as_ref(),
131         original.as_ref(),
132     )
133 }
134 
135 #[cfg(unix)]
136 #[allow(unused_imports)]
137 pub(crate) use self::symlink_file as symlink_dir;
138 
139 #[cfg(not(any(unix, windows)))]
140 #[allow(unused_imports)]
141 pub(crate) use self::symlink_fail as symlink_dir;
142 
143 #[cfg(unix)]
symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()>144 pub(crate) fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
145     symlink(original.as_ref(), link.as_ref(), std::os::unix::fs::symlink)
146 }
147 
148 #[cfg(windows)]
symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()>149 pub(crate) fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
150     symlink(
151         original.as_ref(),
152         link.as_ref(),
153         std::os::windows::fs::symlink_file,
154     )
155 }
156 
157 #[cfg(windows)]
symlink_dir(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()>158 pub(crate) fn symlink_dir(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
159     symlink(
160         original.as_ref(),
161         link.as_ref(),
162         std::os::windows::fs::symlink_dir,
163     )
164 }
165 
write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()>166 pub(crate) fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()> {
167     let path = path.as_ref();
168     match std::fs::write(path, contents) {
169         Ok(()) => Ok(()),
170         Err(e) => err!(e, "Failed to write file `{}`", path),
171     }
172 }
173