Skip to content

Commit fe70e21

Browse files
authored
Remove storage error wrapper (payjoin#781)
This wrapper aimed to reduce boilerplate when converting generic storage errors into `ffi::{receive|send}::PersisterError`. However, since this conversion occurs in only a few places, maintaining the wrapper adds unnecessary overhead in rust-payjoin.
2 parents 8f150fa + adb4ab9 commit fe70e21

File tree

1 file changed

+14
-34
lines changed

1 file changed

+14
-34
lines changed

payjoin/src/persist.rs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<Event, NextState> NextStateTransition<Event, NextState> {
220220
NextStateTransition(AcceptNextState(event, next_state))
221221
}
222222

223-
pub fn save<P>(self, persister: &P) -> Result<NextState, StorageError<P::InternalStorageError>>
223+
pub fn save<P>(self, persister: &P) -> Result<NextState, P::InternalStorageError>
224224
where
225225
P: SessionPersister<SessionEvent = Event>,
226226
{
@@ -311,7 +311,7 @@ where
311311
ApiErr: std::error::Error,
312312
{
313313
#[allow(dead_code)]
314-
pub fn storage_error(self) -> Option<StorageError<StorageErr>> {
314+
pub fn storage_error(self) -> Option<StorageErr> {
315315
match self.0 {
316316
InternalPersistedError::Storage(e) => Some(e),
317317
_ => None,
@@ -368,7 +368,7 @@ where
368368
BadInitInputs(InternalApiError),
369369
/// Error indicating that application failed to save the session event. This should be treated as a transient error
370370
/// but is represented as a separate error because this error is propagated from the application's storage layer
371-
Storage(StorageError<StorageErr>),
371+
Storage(StorageErr),
372372
}
373373

374374
/// Represents a state transition that either progresses to a new state or maintains the current state
@@ -393,21 +393,6 @@ impl<NextState, CurrentState> OptionalTransitionOutcome<NextState, CurrentState>
393393
}
394394
}
395395

396-
/// Wrapper representing a storage error that can be returned from an application's storage layer
397-
#[derive(Debug, Clone)]
398-
pub struct StorageError<Err>(Err);
399-
400-
impl<Err> std::error::Error for StorageError<Err> where Err: std::error::Error {}
401-
402-
impl<Err> std::fmt::Display for StorageError<Err>
403-
where
404-
Err: std::error::Error,
405-
{
406-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
407-
write!(f, "Storage Error: {self:?}")
408-
}
409-
}
410-
411396
/// A session that can persist events to an append-only log.
412397
/// A session represents a sequence of events generated by the BIP78 state machine.
413398
/// The events can be replayed from the log to reconstruct the state machine's state.
@@ -439,8 +424,8 @@ trait InternalSessionPersister: SessionPersister {
439424
fn save_progression_transition<NextState>(
440425
&self,
441426
state_transition: NextStateTransition<Self::SessionEvent, NextState>,
442-
) -> Result<NextState, StorageError<Self::InternalStorageError>> {
443-
self.save_event(&state_transition.0 .0).map_err(StorageError)?;
427+
) -> Result<NextState, Self::InternalStorageError> {
428+
self.save_event(&state_transition.0 .0)?;
444429
Ok(state_transition.0 .1)
445430
}
446431

@@ -454,7 +439,7 @@ trait InternalSessionPersister: SessionPersister {
454439
{
455440
match state_transition.0 {
456441
Ok(AcceptCompleted(success_value)) => {
457-
self.close().map_err(|e| InternalPersistedError::Storage(StorageError(e)))?;
442+
self.close().map_err(InternalPersistedError::Storage)?;
458443
Ok(success_value)
459444
}
460445
Err(RejectTransient(err)) => Err(InternalPersistedError::Transient(err).into()),
@@ -474,8 +459,7 @@ trait InternalSessionPersister: SessionPersister {
474459
{
475460
match state_transition.0 {
476461
Ok(AcceptNextState(event, next_state)) => {
477-
self.save_event(&event)
478-
.map_err(|e| InternalPersistedError::Storage(StorageError(e)))?;
462+
self.save_event(&event).map_err(InternalPersistedError::Storage)?;
479463
Ok(next_state)
480464
}
481465
Err(RejectBadInitInputs(err)) => Err(InternalPersistedError::BadInitInputs(err).into()),
@@ -504,8 +488,7 @@ trait InternalSessionPersister: SessionPersister {
504488
{
505489
match state_transition.0 {
506490
Ok(AcceptOptionalTransition::Success(AcceptNextState(event, next_state))) => {
507-
self.save_event(&event)
508-
.map_err(|e| InternalPersistedError::Storage(StorageError(e)))?;
491+
self.save_event(&event).map_err(InternalPersistedError::Storage)?;
509492
Ok(OptionalTransitionOutcome::Progress(next_state))
510493
}
511494
Ok(AcceptOptionalTransition::NoResults(current_state)) =>
@@ -529,8 +512,7 @@ trait InternalSessionPersister: SessionPersister {
529512
{
530513
match state_transition.0 {
531514
Ok(AcceptNextState(event, next_state)) => {
532-
self.save_event(&event)
533-
.map_err(|e| InternalPersistedError::Storage(StorageError(e)))?;
515+
self.save_event(&event).map_err(InternalPersistedError::Storage)?;
534516
Ok(next_state)
535517
}
536518
Err(RejectTransient(err)) => Err(InternalPersistedError::Transient(err).into()),
@@ -547,8 +529,7 @@ trait InternalSessionPersister: SessionPersister {
547529
{
548530
match state_transition.0 {
549531
Ok(AcceptNextState(event, next_state)) => {
550-
self.save_event(&event)
551-
.map_err(|e| InternalPersistedError::Storage(StorageError(e)))?;
532+
self.save_event(&event).map_err(InternalPersistedError::Storage)?;
552533
Ok(next_state)
553534
}
554535
Err(e) => {
@@ -573,10 +554,9 @@ trait InternalSessionPersister: SessionPersister {
573554
where
574555
Err: std::error::Error,
575556
{
576-
self.save_event(&fatal_rejection.0)
577-
.map_err(|e| InternalPersistedError::Storage(StorageError(e)))?;
557+
self.save_event(&fatal_rejection.0).map_err(InternalPersistedError::Storage)?;
578558
// Session is in a terminal state, close it
579-
self.close().map_err(|e| InternalPersistedError::Storage(StorageError(e)))
559+
self.close().map_err(InternalPersistedError::Storage)
580560
}
581561
}
582562

@@ -832,7 +812,7 @@ mod tests {
832812
fn test_next_state_transition() {
833813
let event = InMemoryTestEvent("foo".to_string());
834814
let next_state = "Next state".to_string();
835-
let test_cases: Vec<TestCase<InMemoryTestState, StorageError<std::convert::Infallible>>> = vec![
815+
let test_cases: Vec<TestCase<InMemoryTestState, std::convert::Infallible>> = vec![
836816
// Success
837817
TestCase {
838818
expected_result: ExpectedResult {
@@ -1039,7 +1019,7 @@ mod tests {
10391019

10401020
#[test]
10411021
fn test_persisted_error_helpers() {
1042-
let storage_err = StorageError(InMemoryTestError {});
1022+
let storage_err = InMemoryTestError {};
10431023
let api_err = InMemoryTestError {};
10441024

10451025
// Test Storage error case

0 commit comments

Comments
 (0)