diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2020-09-13 17:58:51 +0200 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2021-01-28 22:20:12 +0100 |
commit | 5c7677e5fa134b60c7d4c29d3643125901a31fb8 (patch) | |
tree | 42bc74f3d2f257f9c013b986d4b5b421cd0fc361 /rust/src/error.rs | |
parent | 7e169db30f3bc899c8708badf39d8ed268ef1968 (diff) |
WIP rust client
Diffstat (limited to 'rust/src/error.rs')
-rw-r--r-- | rust/src/error.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/rust/src/error.rs b/rust/src/error.rs new file mode 100644 index 0000000..f25bc5d --- /dev/null +++ b/rust/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()) + } +} |