proxmox/proxmox-lang/src/error.rs
Thomas Lamprecht ae9d6e255c lang: rustfmt
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2022-04-10 12:41:44 +02:00

38 lines
1.1 KiB
Rust

//! A set of macros/helpers for I/O handling. These provide for `std::io::Error` what `anyhow` provides
//! for `anyhow::Error.`
use std::io;
/// Helper to convert non-system-errors into an `io::Error` or `io::ErrorKind::Other`.
///
/// A more convenient way is to use the `io_format_err!` macro.
pub fn io_err_other<E: ToString>(e: E) -> io::Error {
io::Error::new(std::io::ErrorKind::Other, e.to_string())
}
/// Like anyhow's `format_err` but producing a `std::io::Error`.
#[macro_export]
macro_rules! io_format_err {
($($msg:tt)+) => {
::std::io::Error::new(::std::io::ErrorKind::Other, format!($($msg)+))
};
}
/// Shortcut to return an `io::Error::last_os_error`.
///
/// This is effectively `return Err(::std::io::Error::last_os_error().into());`.
#[macro_export]
macro_rules! io_bail_last {
() => {{
return Err(::std::io::Error::last_os_error().into());
}};
}
/// Like anyhow's `bail` but producing a `std::io::Error`.
#[macro_export]
macro_rules! io_bail {
($($msg:tt)+) => {{
return Err($crate::io_format_err!($($msg)+));
}};
}