feat: get rid of the functions named like the macros

This commit is contained in:
Oskar Manhart 2023-10-10 15:01:42 +02:00
parent 9e8d43c6b8
commit 24b69bf9c6

View File

@ -17,7 +17,6 @@ pub enum WinappsError {
impl WinappsError { impl WinappsError {
/// This function prints the error to the console. /// This function prints the error to the console.
/// It is used internally by the `unrecoverable` and `panic` functions.
/// All lines are logged as seperate messages, and the source of the error is also logged if it exists. /// All lines are logged as seperate messages, and the source of the error is also logged if it exists.
fn error(&self) { fn error(&self) {
let messages: Vec<String> = self.to_string().split('\n').map(|s| s.into()).collect(); let messages: Vec<String> = self.to_string().split('\n').map(|s| s.into()).collect();
@ -92,52 +91,69 @@ impl<T> IntoError<T> for Option<T> {
} }
} }
/// This function unwraps a `Result` or `Option` and returns the value if it exists. /// This macro unwraps either a `Result` or an `Option` and returns the value if it exists.
/// Should the value not exist, then the program will exit with an exit code of 1. /// It returns a `Result<_, WinappsError>` which can be used to return the error.
/// Called internally by the `unwrap_or_exit!` macro, which you should probably use instead. /// It also works for all other types that implement `IntoError`.
pub fn unwrap_or_exit<T, U>(val: U, msg: String) -> T /// Used internally by `winapps::unwrap_or_exit!` and `winapps::unwrap_or_panic!`.
where #[macro_export]
T: Sized + Debug, macro_rules! into_error {
U: IntoError<T>, ($val:expr) => {{
{ fn into_error_impl<T, U>(val: U) -> std::result::Result<T, $crate::errors::WinappsError>
val.into_error(msg).unwrap_or_else(|e| e.exit()) where
} T: std::marker::Sized + std::fmt::Debug,
U: $crate::errors::IntoError<T>,
{
val.into_error(
"Expected a value, got None / an Error. \
See log above for more detail."
.into(),
)
}
/// This function unwraps a `Result` or `Option` and returns the value if it exists. into_error_impl($val)
/// Should the value not exist, then the program will panic. }};
/// Called internally by the `unwrap_or_panic!` macro, which you should probably use instead. ($val:expr, $msg:expr) => {{
pub fn unwrap_or_panic<T, U>(val: U, msg: String) -> T fn into_error_impl<T, U>(
where val: U,
T: Sized + Debug, msg: String,
U: IntoError<T>, ) -> std::result::Result<T, $crate::errors::WinappsError>
{ where
val.into_error(msg).unwrap_or_else(|e| e.panic()) T: std::marker::Sized + std::fmt::Debug,
U: $crate::errors::IntoError<T>,
{
val.into_error(msg)
}
into_error_impl($val, $msg.into())
}};
} }
/// This macro unwraps a `Result` or `Option` and returns the value if it exists. /// This macro unwraps a `Result` or `Option` and returns the value if it exists.
/// Should the value not exist, then the program will exit with exit code 1. /// Should the value not exist, then the program will exit with exit code 1.
/// Optionally, a message can be passed to the function which uses standard `format!` syntax. /// Optionally, a message can be passed to the function using standard `format!` syntax.
/// The result type has to implement `Debug` and `Sized`, and the error type has to implement `Error`, `Send`, `Sync` has to be `'static`. /// The result type has to implement `Debug` and `Sized`, and the source error type has to implement `Error`, `Send`, `Sync` and has to be `'static`.
/// See `winapps::unwrap_or_panic!` for a version that panics instead of exiting.
#[macro_export] #[macro_export]
macro_rules! unwrap_or_exit { macro_rules! unwrap_or_exit {
($expr:expr) => {{ ($expr:expr) => {{
$crate::errors::unwrap_or_exit($expr, "Expected a value, got None / Error".into()) $crate::into_error!($expr).unwrap_or_else(|e| e.exit())
}}; }};
($expr:expr, $($fmt:tt)*) => {{ ($expr:expr, $($fmt:tt)*) => {{
$crate::errors::unwrap_or_exit($expr, format!($($fmt)*)) $crate::into_error!($expr, format!($($fmt)*)).unwrap_or_else(|e| e.exit())
}}; }};
} }
/// This macro unwraps a `Result` or `Option` and returns the value if it exists. /// This macro unwraps a `Result` or `Option` and returns the value if it exists.
/// Should the value not exist, then the program will panic. /// Should the value not exist, then the program will panic.
/// Optionally, a message can be passed to the function which uses standard `format!` syntax. /// Optionally, a message can be passed to the function using standard `format!` syntax.
/// The result type has to implement `Debug` and `Sized`, and the error type has to implement `Error`, `Send`, `Sync` has to be `'static`. /// The result type has to implement `Debug` and `Sized`, and the error type has to implement `Error`, `Send`, `Sync` and has to be `'static`.
/// See `winapps::unwrap_or_exit!` for a version that exits instead of panicking.
#[macro_export] #[macro_export]
macro_rules! unwrap_or_panic { macro_rules! unwrap_or_panic {
($expr:expr) => {{ ($expr:expr) => {{
$crate::errors::unwrap_or_panic($expr, "Expected a value, got None / Error".into()) $crate::into_error!($expr).unwrap_or_else(|e| e.panic())
}}; }};
($expr:expr, $($fmt:tt)*) => {{ ($expr:expr, $($fmt:tt)*) => {{
$crate::errors::unwrap_or_panic($expr, format!($($fmt)*)) $crate::into_error!($expr, format!($($fmt)*)).unwrap_or_else(|e| e.panic())
}}; }};
} }