• 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 Display for Error {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result17     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
18         formatter.write_str(&self.message)
19     }
20 }
21 
22 impl StdError for Error {
source(&self) -> Option<&(dyn StdError + 'static)>23     fn source(&self) -> Option<&(dyn StdError + 'static)> {
24         let source = self.source.as_ref()?;
25         Some(source)
26     }
27 }
28 
29 macro_rules! err {
30     ($io_error:expr, $fmt:expr $(, $path:expr)* $(,)?) => {
31         Err(Error {
32             source: Option::from($io_error),
33             message: format!($fmt $(, $path.display())*),
34         })
35     }
36 }
37 
copy(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<u64>38 pub(crate) fn copy(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<u64> {
39     let from = from.as_ref();
40     let to = to.as_ref();
41     match std::fs::copy(from, to) {
42         Ok(n) => Ok(n),
43         Err(e) => err!(e, "Failed to copy `{}` -> `{}`", from, to),
44     }
45 }
46 
create_dir_all(path: impl AsRef<Path>) -> Result<()>47 pub(crate) fn create_dir_all(path: impl AsRef<Path>) -> Result<()> {
48     let path = path.as_ref();
49     match std::fs::create_dir_all(path) {
50         Ok(()) => Ok(()),
51         Err(e) => err!(e, "Failed to create directory `{}`", path),
52     }
53 }
54 
current_dir() -> Result<PathBuf>55 pub(crate) fn current_dir() -> Result<PathBuf> {
56     match std::env::current_dir() {
57         Ok(dir) => Ok(dir),
58         Err(e) => err!(e, "Failed to determine current directory"),
59     }
60 }
61 
read(path: impl AsRef<Path>) -> Result<Vec<u8>>62 pub(crate) fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> {
63     let path = path.as_ref();
64     match std::fs::read(path) {
65         Ok(string) => Ok(string),
66         Err(e) => err!(e, "Failed to read file `{}`", path),
67     }
68 }
69 
read_stdin() -> Result<Vec<u8>>70 pub(crate) fn read_stdin() -> Result<Vec<u8>> {
71     let mut bytes = Vec::new();
72     match io::stdin().read_to_end(&mut bytes) {
73         Ok(_len) => Ok(bytes),
74         Err(e) => err!(e, "Failed to read input from stdin"),
75     }
76 }
77 
remove_file(path: impl AsRef<Path>) -> Result<()>78 pub(crate) fn remove_file(path: impl AsRef<Path>) -> Result<()> {
79     let path = path.as_ref();
80     match std::fs::remove_file(path) {
81         Ok(()) => Ok(()),
82         Err(e) => err!(e, "Failed to remove file `{}`", path),
83     }
84 }
85 
remove_dir(path: impl AsRef<Path>) -> Result<()>86 pub(crate) fn remove_dir(path: impl AsRef<Path>) -> Result<()> {
87     let path = path.as_ref();
88     match std::fs::remove_dir(path) {
89         Ok(()) => Ok(()),
90         Err(e) => err!(e, "Failed to remove directory `{}`", path),
91     }
92 }
93 
symlink<'a>( original: &'a Path, link: &'a Path, fun: fn(&'a Path, &'a Path) -> io::Result<()>, ) -> Result<()>94 fn symlink<'a>(
95     original: &'a Path,
96     link: &'a Path,
97     fun: fn(&'a Path, &'a Path) -> io::Result<()>,
98 ) -> Result<()> {
99     match fun(original, link) {
100         Ok(()) => Ok(()),
101         Err(e) => err!(
102             e,
103             "Failed to create symlink `{}` pointing to `{}`",
104             link,
105             original,
106         ),
107     }
108 }
109 
symlink_fail(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()>110 pub(crate) fn symlink_fail(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
111     err!(
112         None,
113         "Failed to create symlink `{}` pointing to `{}`",
114         link.as_ref(),
115         original.as_ref(),
116     )
117 }
118 
119 #[cfg(unix)]
120 #[allow(unused_imports)]
121 pub(crate) use self::symlink_file as symlink_dir;
122 
123 #[cfg(not(any(unix, windows)))]
124 #[allow(unused_imports)]
125 pub(crate) use self::symlink_fail as symlink_dir;
126 
127 #[cfg(unix)]
symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()>128 pub(crate) fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
129     symlink(original.as_ref(), link.as_ref(), std::os::unix::fs::symlink)
130 }
131 
132 #[cfg(windows)]
symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()>133 pub(crate) fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
134     symlink(
135         original.as_ref(),
136         link.as_ref(),
137         std::os::windows::fs::symlink_file,
138     )
139 }
140 
141 #[cfg(windows)]
symlink_dir(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()>142 pub(crate) fn symlink_dir(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
143     symlink(
144         original.as_ref(),
145         link.as_ref(),
146         std::os::windows::fs::symlink_dir,
147     )
148 }
149 
write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()>150 pub(crate) fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()> {
151     let path = path.as_ref();
152     match std::fs::write(path, contents) {
153         Ok(()) => Ok(()),
154         Err(e) => err!(e, "Failed to write file `{}`", path),
155     }
156 }
157