diff --git a/visions/server/src/database/mod.rs b/visions/server/src/database/mod.rs index f195caa..3743d54 100644 --- a/visions/server/src/database/mod.rs +++ b/visions/server/src/database/mod.rs @@ -84,52 +84,37 @@ impl DbConn { Self { conn: tx, handle } } +} - async fn send(&self, req: Request) -> Result { +macro_rules! send_request { + ($s:expr, $req:expr, $resp_h:pat => $block:expr) => {{ let (tx, rx) = bounded::(1); - let request = DatabaseRequest { tx, req }; - match self.conn.send(request).await { + let request = DatabaseRequest { tx, req: $req }; + match $s.conn.send(request).await { Ok(()) => (), Err(_) => return Err(FatalError::DatabaseConnectionLost), }; - rx.recv() + match rx + .recv() .await .map_err(|_| FatalError::DatabaseConnectionLost) - } + { + Ok($resp_h) => $block, + Ok(_) => Err(FatalError::MessageMismatch), + Err(_) => Err(FatalError::DatabaseConnectionLost), + } + }}; } #[async_trait] impl Database for DbConn { async fn user(&self, uid: &UserId) -> Result, FatalError> { - match self - .send(Request::User(uid.clone())) - .await - { - Ok(DatabaseResponse::User(user)) => Ok(user), - Ok(_) => Err(FatalError::MessageMismatch), - Err(_) => Err(FatalError::DatabaseConnectionLost), - } + send_request!(self, Request::User(uid.clone()), DatabaseResponse::User(user) => Ok(user)) } async fn user_by_username(&self, username: &str) -> Result, FatalError> { - let (tx, rx) = bounded::(1); - - let request = DatabaseRequest { - tx, - req: Request::UserByUsername(username.to_owned()), - }; - - match self.conn.send(request).await { - Ok(()) => (), - Err(_) => return Err(FatalError::DatabaseConnectionLost), - }; - - match rx.recv().await { - Ok(DatabaseResponse::User(user)) => Ok(user), - Ok(_) => Err(FatalError::MessageMismatch), - Err(_) => Err(FatalError::DatabaseConnectionLost), - } + send_request!(self, Request::UserByUsername(username.to_owned()), DatabaseResponse::User(user) => Ok(user)) } async fn save_user( @@ -140,129 +125,35 @@ impl Database for DbConn { admin: bool, enabled: bool, ) -> Result { - let (tx, rx) = bounded::(1); - - let request = DatabaseRequest { - tx, - req: Request::SaveUser( + send_request!(self, + Request::SaveUser( user_id, name.to_owned(), password.to_owned(), admin, enabled, ), - }; - - match self.conn.send(request).await { - Ok(()) => (), - Err(_) => return Err(FatalError::DatabaseConnectionLost), - }; - - match rx.recv().await { - Ok(DatabaseResponse::SaveUser(user_id)) => Ok(user_id), - Ok(_) => Err(FatalError::MessageMismatch), - Err(_) => Err(FatalError::DatabaseConnectionLost), - } + DatabaseResponse::SaveUser(user_id) => Ok(user_id)) } async fn users(&mut self) -> Result, FatalError> { - let (tx, rx) = bounded::(1); - - let request = DatabaseRequest { - tx, - req: Request::Users, - }; - - match self.conn.send(request).await { - Ok(()) => (), - Err(_) => return Err(FatalError::DatabaseConnectionLost), - }; - - match rx.recv().await { - Ok(DatabaseResponse::Users(lst)) => Ok(lst), - Ok(_) => Err(FatalError::MessageMismatch), - Err(_) => Err(FatalError::DatabaseConnectionLost), - } + send_request!(self, Request::Users, DatabaseResponse::Users(lst) => Ok(lst)) } async fn games(&mut self) -> Result, FatalError> { - let (tx, rx) = bounded::(1); - - let request = DatabaseRequest { - tx, - req: Request::Games, - }; - - match self.conn.send(request).await { - Ok(()) => (), - Err(_) => return Err(FatalError::DatabaseConnectionLost), - }; - - match rx.recv().await { - Ok(DatabaseResponse::Games(lst)) => Ok(lst), - Ok(_) => Err(FatalError::MessageMismatch), - Err(_) => Err(FatalError::DatabaseConnectionLost), - } + send_request!(self, Request::Games, DatabaseResponse::Games(lst) => Ok(lst)) } async fn character(&mut self, id: CharacterId) -> Result, FatalError> { - let (tx, rx) = bounded::(1); - - let request = DatabaseRequest { - tx, - req: Request::Charsheet(id), - }; - - match self.conn.send(request).await { - Ok(()) => (), - Err(_) => return Err(FatalError::DatabaseConnectionLost), - }; - - match rx.recv().await { - Ok(DatabaseResponse::Charsheet(row)) => Ok(row), - Ok(_) => Err(FatalError::MessageMismatch), - Err(_) => Err(FatalError::DatabaseConnectionLost), - } + send_request!(self, Request::Charsheet(id), DatabaseResponse::Charsheet(row) => Ok(row)) } async fn session(&self, id: SessionId) -> Result, FatalError> { - let (tx, rx) = bounded::(1); - - let request = DatabaseRequest { - tx, - req: Request::Session(id), - }; - - match self.conn.send(request).await { - Ok(()) => (), - Err(_) => return Err(FatalError::DatabaseConnectionLost), - }; - - match rx.recv().await { - Ok(DatabaseResponse::Session(row)) => Ok(row), - Ok(_) => Err(FatalError::MessageMismatch), - Err(_) => Err(FatalError::DatabaseConnectionLost), - } + send_request!(self, Request::Session(id), DatabaseResponse::Session(row) => Ok(row)) } async fn create_session(&self, id: UserId) -> Result { - let (tx, rx) = bounded::(1); - - let request = DatabaseRequest { - tx, - req: Request::CreateSession(id), - }; - - match self.conn.send(request).await { - Ok(()) => (), - Err(_) => return Err(FatalError::DatabaseConnectionLost), - }; - - match rx.recv().await { - Ok(DatabaseResponse::CreateSession(session_id)) => Ok(session_id), - Ok(_) => Err(FatalError::MessageMismatch), - Err(_) => Err(FatalError::DatabaseConnectionLost), - } + send_request!(self, Request::CreateSession(id), DatabaseResponse::CreateSession(session_id) => Ok(session_id)) } }