feat: macros for reducing error boilerplate

This commit is contained in:
Paul Makles
2023-04-22 15:23:20 +01:00
parent 750037b5d2
commit 40790de909
2 changed files with 39 additions and 31 deletions

View File

@@ -119,7 +119,7 @@ pub enum ErrorType {
#[macro_export]
macro_rules! create_error {
( $error:ident $( $tt:tt )? ) => {
( $error: ident $( $tt:tt )? ) => {
$crate::Error {
error_type: $crate::ErrorType::$error $( $tt )?,
location: format!("{}:{}:{}", file!(), line!(), column!()),
@@ -129,7 +129,7 @@ macro_rules! create_error {
#[macro_export]
macro_rules! create_database_error {
( $operation:expr, $collection:expr ) => {
( $operation: expr, $collection: expr ) => {
create_error!(DatabaseError {
operation: $operation.to_string(),
collection: $collection.to_string()
@@ -137,6 +137,23 @@ macro_rules! create_database_error {
};
}
#[macro_export]
#[cfg(debug_assertions)]
macro_rules! query {
( $self: ident, $type: ident, $collection: expr, $($rest:expr),+ ) => {
Ok($self.$type($collection, $($rest),+).await.unwrap())
};
}
#[macro_export]
#[cfg(not(debug_assertions))]
macro_rules! query {
( $self: ident, $type: ident, $collection: expr, $($rest:expr),+ ) => {
$self.$type($collection, $($rest),+).await
.map_err(|_| create_database_error!(stringify!($type), $collection))
};
}
#[cfg(test)]
mod tests {
use crate::ErrorType;