Skip to content

Commit 69e8263

Browse files
committed
Introduce module-specific Result type for db.rs
1 parent d1c9f5b commit 69e8263

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

payjoin-directory/src/db.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,36 +40,37 @@ impl std::error::Error for Error {
4040
}
4141
}
4242

43+
impl From<RedisError> for Error {
44+
fn from(value: RedisError) -> Self { Error::Redis(value) }
45+
}
46+
47+
pub(crate) type Result<T> = core::result::Result<T, Error>;
48+
4349
impl DbPool {
44-
pub async fn new(timeout: Duration, db_host: String) -> RedisResult<Self> {
50+
pub async fn new(timeout: Duration, db_host: String) -> Result<Self> {
4551
let client = Client::open(format!("redis://{}", db_host))?;
4652
Ok(Self { client, timeout })
4753
}
4854

4955
/// Peek using [`DEFAULT_COLUMN`] as the channel type.
50-
pub async fn push_default(&self, subdirectory_id: &str, data: Vec<u8>) -> RedisResult<()> {
56+
pub async fn push_default(&self, subdirectory_id: &str, data: Vec<u8>) -> Result<()> {
5157
self.push(subdirectory_id, DEFAULT_COLUMN, data).await
5258
}
5359

54-
pub async fn peek_default(&self, subdirectory_id: &str) -> Result<Vec<u8>, Error> {
60+
pub async fn peek_default(&self, subdirectory_id: &str) -> Result<Vec<u8>> {
5561
self.peek_with_timeout(subdirectory_id, DEFAULT_COLUMN).await
5662
}
5763

58-
pub async fn push_v1(&self, subdirectory_id: &str, data: Vec<u8>) -> RedisResult<()> {
64+
pub async fn push_v1(&self, subdirectory_id: &str, data: Vec<u8>) -> Result<()> {
5965
self.push(subdirectory_id, PJ_V1_COLUMN, data).await
6066
}
6167

6268
/// Peek using [`PJ_V1_COLUMN`] as the channel type.
63-
pub async fn peek_v1(&self, subdirectory_id: &str) -> Result<Vec<u8>, Error> {
69+
pub async fn peek_v1(&self, subdirectory_id: &str) -> Result<Vec<u8>> {
6470
self.peek_with_timeout(subdirectory_id, PJ_V1_COLUMN).await
6571
}
6672

67-
async fn push(
68-
&self,
69-
subdirectory_id: &str,
70-
channel_type: &str,
71-
data: Vec<u8>,
72-
) -> RedisResult<()> {
73+
async fn push(&self, subdirectory_id: &str, channel_type: &str, data: Vec<u8>) -> Result<()> {
7374
let mut conn = self.client.get_async_connection().await?;
7475
let key = channel_name(subdirectory_id, channel_type);
7576
() = conn.set(&key, data.clone()).await?;
@@ -81,7 +82,7 @@ impl DbPool {
8182
&self,
8283
subdirectory_id: &str,
8384
channel_type: &str,
84-
) -> Result<Vec<u8>, Error> {
85+
) -> Result<Vec<u8>> {
8586
match tokio::time::timeout(self.timeout, self.peek(subdirectory_id, channel_type)).await {
8687
Ok(redis_result) => match redis_result {
8788
Ok(result) => Ok(result),

payjoin-directory/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ impl From<hyper::http::Error> for HandlerError {
314314
}
315315

316316
fn handle_peek(
317-
result: Result<Vec<u8>, db::Error>,
317+
result: db::Result<Vec<u8>>,
318318
timeout_response: Response<BoxBody<Bytes, hyper::Error>>,
319319
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, HandlerError> {
320320
match result {

0 commit comments

Comments
 (0)