Skip to content

Commit

Permalink
Structure db::Errors for context
Browse files Browse the repository at this point in the history
  • Loading branch information
DanGould committed Jun 12, 2024
1 parent f6250eb commit a3a9fa1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
34 changes: 34 additions & 0 deletions payjoin-cli/src/db/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::fmt;

#[cfg(feature = "v2")]
use bitcoincore_rpc::jsonrpc::serde_json;
use sled::Error as SledError;

pub(crate) type Result<T> = std::result::Result<T, Error>;

#[derive(Debug)]
pub(crate) enum Error {
Sled(SledError),
#[cfg(feature = "v2")]
Serialize(serde_json::Error),
#[cfg(feature = "v2")]
Deserialize(serde_json::Error),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Sled(e) => write!(f, "Database operation failed: {}", e),
#[cfg(feature = "v2")]
Error::Serialize(e) => write!(f, "Serialization failed: {}", e),
#[cfg(feature = "v2")]
Error::Deserialize(e) => write!(f, "Deserialization failed: {}", e),
}
}
}

impl std::error::Error for Error {}

impl From<SledError> for Error {
fn from(error: SledError) -> Self { Error::Sled(error) }
}
4 changes: 3 additions & 1 deletion payjoin-cli/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::path::Path;

use anyhow::Result;
use payjoin::bitcoin::consensus::encode::serialize;
use payjoin::bitcoin::OutPoint;
use sled::IVec;

pub(crate) mod error;
use error::*;

pub(crate) const DB_PATH: &str = "payjoin.sled";

pub(crate) struct Database(sled::Db);
Expand Down
9 changes: 5 additions & 4 deletions payjoin-cli/src/db/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ use super::*;
impl Database {
pub(crate) fn insert_recv_session(&self, session: Enrolled) -> Result<()> {
let key = &session.public_key().serialize();
let value = serde_json::to_string(&session)?;
let value = serde_json::to_string(&session).map_err(Error::Serialize)?;
self.0.insert(key.as_slice(), IVec::from(value.as_str()))?;
self.0.flush()?;
Ok(())
}

pub(crate) fn get_recv_session(&self) -> Result<Option<Enrolled>> {
if let Some(ivec) = self.0.get("recv_sessions")? {
let session: Enrolled = serde_json::from_slice(&ivec)?;
let session: Enrolled = serde_json::from_slice(&ivec).map_err(Error::Deserialize)?;
Ok(Some(session))
} else {
Ok(None)
Expand All @@ -31,15 +31,16 @@ impl Database {

pub(crate) fn insert_send_session(&self, session: &mut RequestContext) -> Result<()> {
let key = &session.public_key().serialize();
let value = serde_json::to_string(session)?;
let value = serde_json::to_string(session).map_err(Error::Serialize)?;
self.0.insert(key.as_slice(), IVec::from(value.as_str()))?;
self.0.flush()?;
Ok(())
}

pub(crate) fn get_send_session(&self) -> Result<Option<RequestContext>> {
if let Some(ivec) = self.0.get("send_sessions")? {
let session: RequestContext = serde_json::from_slice(&ivec)?;
let session: RequestContext =
serde_json::from_slice(&ivec).map_err(Error::Deserialize)?;
Ok(Some(session))
} else {
Ok(None)
Expand Down

0 comments on commit a3a9fa1

Please sign in to comment.