tap_msg/message/
revert.rs

1//! Revert message type for the Transaction Authorization Protocol.
2//!
3//! This module defines the Revert message type, which is used
4//! for requesting reversal of settled transactions in the TAP protocol.
5
6use serde::{Deserialize, Serialize};
7
8use crate::error::{Error, Result};
9use crate::TapMessage;
10
11/// Revert message body (TAIP-4).
12#[derive(Debug, Clone, Serialize, Deserialize, TapMessage)]
13#[tap(message_type = "https://tap.rsvp/schema/1.0#Revert")]
14pub struct Revert {
15    /// ID of the transfer being reverted.
16    #[tap(thread_id)]
17    pub transaction_id: String,
18
19    /// Settlement address in CAIP-10 format to return the funds to.
20    #[serde(rename = "settlementAddress")]
21    pub settlement_address: String,
22
23    /// Reason for the reversal request.
24    pub reason: String,
25}
26
27impl Revert {
28    /// Create a new Revert message
29    pub fn new(transaction_id: &str, settlement_address: &str, reason: &str) -> Self {
30        Self {
31            transaction_id: transaction_id.to_string(),
32            settlement_address: settlement_address.to_string(),
33            reason: reason.to_string(),
34        }
35    }
36}
37
38impl Revert {
39    /// Custom validation for Revert messages
40    pub fn validate_revert(&self) -> Result<()> {
41        if self.transaction_id.is_empty() {
42            return Err(Error::Validation(
43                "Transaction ID is required in Revert".to_string(),
44            ));
45        }
46
47        if self.settlement_address.is_empty() {
48            return Err(Error::Validation(
49                "Settlement address is required in Revert".to_string(),
50            ));
51        }
52
53        if self.reason.is_empty() {
54            return Err(Error::Validation(
55                "Reason is required in Revert".to_string(),
56            ));
57        }
58
59        Ok(())
60    }
61}