summaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2020-05-04 22:33:14 +0200
committerTom Smeding <tom.smeding@gmail.com>2020-05-04 22:33:24 +0200
commit982fb3227ad438e8c7a16ac75ae6f296df2c8bd9 (patch)
tree7ab2fdd70d633454772d2d568505dc486ed69783 /src/error.rs
Initial working versionHEADmaster
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
new file mode 100644
index 0000000..f25bc5d
--- /dev/null
+++ b/src/error.rs
@@ -0,0 +1,27 @@
+use std::io;
+
+pub trait IntoIOError {
+ fn ioerr(self) -> io::Error;
+ fn perror(self, parent: io::Error) -> io::Error;
+}
+
+// This impl bound is taken directly from the io::Error::new function.
+impl<E: Into<Box<dyn std::error::Error + Send + Sync>>> IntoIOError for E {
+ fn ioerr(self) -> io::Error {
+ io::Error::new(io::ErrorKind::Other, self)
+ }
+
+ fn perror(self, parent: io::Error) -> io::Error {
+ io::Error::new(parent.kind(), format!("{}: {}", self.into(), parent))
+ }
+}
+
+pub trait IntoIOResult<T> {
+ fn iores(self) -> io::Result<T>;
+}
+
+impl<T, E: IntoIOError> IntoIOResult<T> for Result<T, E> {
+ fn iores(self) -> io::Result<T> {
+ self.map_err(|e| e.ioerr())
+ }
+}