Skip to content

Commit 36ab06d

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

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

payjoin-directory/src/db.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,35 @@ impl std::error::Error for Error {
4040
}
4141
}
4242

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

4957
/// Peek using [`DEFAULT_COLUMN`] as the channel type.
50-
pub async fn push_default(&self, subdirectory_id: &str, data: Vec<u8>) -> RedisResult<()> {
58+
pub async fn push_default(&self, subdirectory_id: &str, data: Vec<u8>) -> Result<()> {
5159
self.push(subdirectory_id, DEFAULT_COLUMN, data).await
5260
}
5361

54-
pub async fn peek_default(&self, subdirectory_id: &str) -> Result<Vec<u8>, Error> {
62+
pub async fn peek_default(&self, subdirectory_id: &str) -> Result<Vec<u8>> {
5563
self.peek_with_timeout(subdirectory_id, DEFAULT_COLUMN).await
5664
}
5765

58-
pub async fn push_v1(&self, subdirectory_id: &str, data: Vec<u8>) -> RedisResult<()> {
66+
pub async fn push_v1(&self, subdirectory_id: &str, data: Vec<u8>) -> Result<()> {
5967
self.push(subdirectory_id, PJ_V1_COLUMN, data).await
6068
}
6169

6270
/// Peek using [`PJ_V1_COLUMN`] as the channel type.
63-
pub async fn peek_v1(&self, subdirectory_id: &str) -> Result<Vec<u8>, Error> {
71+
pub async fn peek_v1(&self, subdirectory_id: &str) -> Result<Vec<u8>> {
6472
self.peek_with_timeout(subdirectory_id, PJ_V1_COLUMN).await
6573
}
6674

@@ -69,7 +77,7 @@ impl DbPool {
6977
subdirectory_id: &str,
7078
channel_type: &str,
7179
data: Vec<u8>,
72-
) -> RedisResult<()> {
80+
) -> Result<()> {
7381
let mut conn = self.client.get_async_connection().await?;
7482
let key = channel_name(subdirectory_id, channel_type);
7583
() = conn.set(&key, data.clone()).await?;
@@ -81,7 +89,7 @@ impl DbPool {
8189
&self,
8290
subdirectory_id: &str,
8391
channel_type: &str,
84-
) -> Result<Vec<u8>, Error> {
92+
) -> Result<Vec<u8>> {
8593
match tokio::time::timeout(self.timeout, self.peek(subdirectory_id, channel_type)).await {
8694
Ok(redis_result) => match redis_result {
8795
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)