Skip to content

Commit

Permalink
refactor: add checks inside Failed state
Browse files Browse the repository at this point in the history
Signed-off-by: Gustavo Inacio <gustavo@semiotic.ai>
  • Loading branch information
gusinacio committed Dec 22, 2023
1 parent e32d86d commit cd3bea8
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions tap_core/src/tap_receipt/received_receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
//! This module is useful for managing and tracking the state of received receipts, as well as
//! their progress through various checks and stages of inclusion in RAV requests and received RAVs.
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use super::{receipt_auditor::ReceiptAuditor, Receipt, ReceiptCheck, ReceiptCheckResults};
Expand All @@ -31,10 +33,30 @@ pub struct Checking {
pub(crate) checks: ReceiptCheckResults,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Failed;
pub struct Failed {
/// A list of checks to be completed for the receipt, along with their current result
pub(crate) checks: ReceiptCheckResults,
}

impl From<Checking> for Failed {
fn from(checking: Checking) -> Self {
Self {
checks: checking.checks,
}
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AwaitingReserve;

impl From<AwaitingReserve> for Failed {
fn from(_checking: AwaitingReserve) -> Self {
Self {
checks: HashMap::new(),
}
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Reserved;

Expand Down Expand Up @@ -206,7 +228,7 @@ impl ReceiptWithState<AwaitingReserve> {
{
match auditor.check_and_reserve_escrow(&self).await {
Ok(_) => Ok(self.perform_state_changes(Reserved)),
Err(_) => Err(self.perform_state_changes(Failed)),
Err(_) => Err(self.perform_state_changes_into()),
}
}
}
Expand Down Expand Up @@ -292,7 +314,7 @@ impl ReceiptWithState<Checking> {
.await;

if self.any_check_resulted_in_error() {
let failed = self.perform_state_changes(Failed);
let failed = self.perform_state_changes_into();
Err(failed)
} else {
let checked = self.perform_state_changes(AwaitingReserve);
Expand Down Expand Up @@ -376,6 +398,19 @@ impl<S> ReceiptWithState<S>
where
S: ReceiptState,
{

fn perform_state_changes_into<T>(self) -> ReceiptWithState<T>
where
T: ReceiptState,
S: Into<T>,
{
ReceiptWithState {
signed_receipt: self.signed_receipt,
query_id: self.query_id,
state: self.state.into(),
}
}

fn perform_state_changes<T>(self, new_state: T) -> ReceiptWithState<T>
where
T: ReceiptState,
Expand Down

0 comments on commit cd3bea8

Please sign in to comment.