Write a macro that eases communication between DbConn and DiskDb
This commit is contained in:
parent
2b1a0b99f8
commit
085a82776e
@ -84,52 +84,37 @@ impl DbConn {
|
|||||||
|
|
||||||
Self { conn: tx, handle }
|
Self { conn: tx, handle }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn send(&self, req: Request) -> Result<DatabaseResponse, FatalError> {
|
macro_rules! send_request {
|
||||||
|
($s:expr, $req:expr, $resp_h:pat => $block:expr) => {{
|
||||||
let (tx, rx) = bounded::<DatabaseResponse>(1);
|
let (tx, rx) = bounded::<DatabaseResponse>(1);
|
||||||
let request = DatabaseRequest { tx, req };
|
let request = DatabaseRequest { tx, req: $req };
|
||||||
match self.conn.send(request).await {
|
match $s.conn.send(request).await {
|
||||||
Ok(()) => (),
|
Ok(()) => (),
|
||||||
Err(_) => return Err(FatalError::DatabaseConnectionLost),
|
Err(_) => return Err(FatalError::DatabaseConnectionLost),
|
||||||
};
|
};
|
||||||
|
|
||||||
rx.recv()
|
match rx
|
||||||
|
.recv()
|
||||||
.await
|
.await
|
||||||
.map_err(|_| FatalError::DatabaseConnectionLost)
|
.map_err(|_| FatalError::DatabaseConnectionLost)
|
||||||
}
|
{
|
||||||
|
Ok($resp_h) => $block,
|
||||||
|
Ok(_) => Err(FatalError::MessageMismatch),
|
||||||
|
Err(_) => Err(FatalError::DatabaseConnectionLost),
|
||||||
|
}
|
||||||
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Database for DbConn {
|
impl Database for DbConn {
|
||||||
async fn user(&self, uid: &UserId) -> Result<Option<UserRow>, FatalError> {
|
async fn user(&self, uid: &UserId) -> Result<Option<UserRow>, FatalError> {
|
||||||
match self
|
send_request!(self, Request::User(uid.clone()), DatabaseResponse::User(user) => Ok(user))
|
||||||
.send(Request::User(uid.clone()))
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(DatabaseResponse::User(user)) => Ok(user),
|
|
||||||
Ok(_) => Err(FatalError::MessageMismatch),
|
|
||||||
Err(_) => Err(FatalError::DatabaseConnectionLost),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn user_by_username(&self, username: &str) -> Result<Option<UserRow>, FatalError> {
|
async fn user_by_username(&self, username: &str) -> Result<Option<UserRow>, FatalError> {
|
||||||
let (tx, rx) = bounded::<DatabaseResponse>(1);
|
send_request!(self, Request::UserByUsername(username.to_owned()), DatabaseResponse::User(user) => Ok(user))
|
||||||
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn save_user(
|
async fn save_user(
|
||||||
@ -140,129 +125,35 @@ impl Database for DbConn {
|
|||||||
admin: bool,
|
admin: bool,
|
||||||
enabled: bool,
|
enabled: bool,
|
||||||
) -> Result<UserId, FatalError> {
|
) -> Result<UserId, FatalError> {
|
||||||
let (tx, rx) = bounded::<DatabaseResponse>(1);
|
send_request!(self,
|
||||||
|
Request::SaveUser(
|
||||||
let request = DatabaseRequest {
|
|
||||||
tx,
|
|
||||||
req: Request::SaveUser(
|
|
||||||
user_id,
|
user_id,
|
||||||
name.to_owned(),
|
name.to_owned(),
|
||||||
password.to_owned(),
|
password.to_owned(),
|
||||||
admin,
|
admin,
|
||||||
enabled,
|
enabled,
|
||||||
),
|
),
|
||||||
};
|
DatabaseResponse::SaveUser(user_id) => Ok(user_id))
|
||||||
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn users(&mut self) -> Result<Vec<UserRow>, FatalError> {
|
async fn users(&mut self) -> Result<Vec<UserRow>, FatalError> {
|
||||||
let (tx, rx) = bounded::<DatabaseResponse>(1);
|
send_request!(self, Request::Users, DatabaseResponse::Users(lst) => Ok(lst))
|
||||||
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn games(&mut self) -> Result<Vec<GameRow>, FatalError> {
|
async fn games(&mut self) -> Result<Vec<GameRow>, FatalError> {
|
||||||
let (tx, rx) = bounded::<DatabaseResponse>(1);
|
send_request!(self, Request::Games, DatabaseResponse::Games(lst) => Ok(lst))
|
||||||
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn character(&mut self, id: CharacterId) -> Result<Option<CharsheetRow>, FatalError> {
|
async fn character(&mut self, id: CharacterId) -> Result<Option<CharsheetRow>, FatalError> {
|
||||||
let (tx, rx) = bounded::<DatabaseResponse>(1);
|
send_request!(self, Request::Charsheet(id), DatabaseResponse::Charsheet(row) => Ok(row))
|
||||||
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn session(&self, id: SessionId) -> Result<Option<UserRow>, FatalError> {
|
async fn session(&self, id: SessionId) -> Result<Option<UserRow>, FatalError> {
|
||||||
let (tx, rx) = bounded::<DatabaseResponse>(1);
|
send_request!(self, Request::Session(id), DatabaseResponse::Session(row) => Ok(row))
|
||||||
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create_session(&self, id: UserId) -> Result<SessionId, FatalError> {
|
async fn create_session(&self, id: UserId) -> Result<SessionId, FatalError> {
|
||||||
let (tx, rx) = bounded::<DatabaseResponse>(1);
|
send_request!(self, Request::CreateSession(id), DatabaseResponse::CreateSession(session_id) => Ok(session_id))
|
||||||
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user