feat: macros for reducing error boilerplate
This commit is contained in:
@@ -12,31 +12,25 @@ static COL: &str = "bots";
|
|||||||
impl AbstractBots for MongoDb {
|
impl AbstractBots for MongoDb {
|
||||||
/// Fetch a bot by its id
|
/// Fetch a bot by its id
|
||||||
async fn fetch_bot(&self, id: &str) -> Result<Bot> {
|
async fn fetch_bot(&self, id: &str) -> Result<Bot> {
|
||||||
self.find_one_by_id(COL, id)
|
query!(self, find_one_by_id, COL, id)?.ok_or_else(|| create_error!(NotFound))
|
||||||
.await
|
|
||||||
.map_err(|_| create_database_error!("find_one", COL))?
|
|
||||||
.ok_or_else(|| create_error!(NotFound))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch a bot by its token
|
/// Fetch a bot by its token
|
||||||
async fn fetch_bot_by_token(&self, token: &str) -> Result<Bot> {
|
async fn fetch_bot_by_token(&self, token: &str) -> Result<Bot> {
|
||||||
self.find_one(
|
query!(
|
||||||
|
self,
|
||||||
|
find_one,
|
||||||
COL,
|
COL,
|
||||||
doc! {
|
doc! {
|
||||||
"token": token
|
"token": token
|
||||||
},
|
}
|
||||||
)
|
)?
|
||||||
.await
|
|
||||||
.map_err(|_| create_database_error!("find_one", COL))?
|
|
||||||
.ok_or_else(|| create_error!(NotFound))
|
.ok_or_else(|| create_error!(NotFound))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insert new bot into the database
|
/// Insert new bot into the database
|
||||||
async fn insert_bot(&self, bot: &Bot) -> Result<()> {
|
async fn insert_bot(&self, bot: &Bot) -> Result<()> {
|
||||||
self.insert_one(COL, &bot)
|
query!(self, insert_one, COL, &bot).map(|_| ())
|
||||||
.await
|
|
||||||
.map(|_| ())
|
|
||||||
.map_err(|_| create_database_error!("insert_one", COL))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update bot with new information
|
/// Update bot with new information
|
||||||
@@ -46,49 +40,46 @@ impl AbstractBots for MongoDb {
|
|||||||
partial: &PartialBot,
|
partial: &PartialBot,
|
||||||
remove: Vec<FieldsBot>,
|
remove: Vec<FieldsBot>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
self.update_one_by_id(
|
query!(
|
||||||
|
self,
|
||||||
|
update_one_by_id,
|
||||||
COL,
|
COL,
|
||||||
id,
|
id,
|
||||||
partial,
|
partial,
|
||||||
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
|
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
|
||||||
None,
|
None
|
||||||
)
|
)
|
||||||
.await
|
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
.map_err(|_| create_database_error!("update_one", COL))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Delete a bot from the database
|
/// Delete a bot from the database
|
||||||
async fn delete_bot(&self, id: &str) -> Result<()> {
|
async fn delete_bot(&self, id: &str) -> Result<()> {
|
||||||
self.delete_one_by_id(COL, id)
|
query!(self, delete_one_by_id, COL, id).map(|_| ())
|
||||||
.await
|
|
||||||
.map(|_| ())
|
|
||||||
.map_err(|_| create_database_error!("delete_one", COL))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch bots owned by a user
|
/// Fetch bots owned by a user
|
||||||
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>> {
|
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>> {
|
||||||
self.find(
|
query!(
|
||||||
|
self,
|
||||||
|
find,
|
||||||
COL,
|
COL,
|
||||||
doc! {
|
doc! {
|
||||||
"owner": user_id
|
"owner": user_id
|
||||||
},
|
}
|
||||||
)
|
)
|
||||||
.await
|
|
||||||
.map_err(|_| create_database_error!("find", COL))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the number of bots owned by a user
|
/// Get the number of bots owned by a user
|
||||||
async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result<usize> {
|
async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result<usize> {
|
||||||
self.count_documents(
|
query!(
|
||||||
|
self,
|
||||||
|
count_documents,
|
||||||
COL,
|
COL,
|
||||||
doc! {
|
doc! {
|
||||||
"owner": user_id
|
"owner": user_id
|
||||||
},
|
}
|
||||||
)
|
)
|
||||||
.await
|
|
||||||
.map(|v| v as usize)
|
.map(|v| v as usize)
|
||||||
.map_err(|_| create_database_error!("count", COL))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ pub enum ErrorType {
|
|||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! create_error {
|
macro_rules! create_error {
|
||||||
( $error:ident $( $tt:tt )? ) => {
|
( $error: ident $( $tt:tt )? ) => {
|
||||||
$crate::Error {
|
$crate::Error {
|
||||||
error_type: $crate::ErrorType::$error $( $tt )?,
|
error_type: $crate::ErrorType::$error $( $tt )?,
|
||||||
location: format!("{}:{}:{}", file!(), line!(), column!()),
|
location: format!("{}:{}:{}", file!(), line!(), column!()),
|
||||||
@@ -129,7 +129,7 @@ macro_rules! create_error {
|
|||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! create_database_error {
|
macro_rules! create_database_error {
|
||||||
( $operation:expr, $collection:expr ) => {
|
( $operation: expr, $collection: expr ) => {
|
||||||
create_error!(DatabaseError {
|
create_error!(DatabaseError {
|
||||||
operation: $operation.to_string(),
|
operation: $operation.to_string(),
|
||||||
collection: $collection.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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::ErrorType;
|
use crate::ErrorType;
|
||||||
|
|||||||
Reference in New Issue
Block a user