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    pub settlement_address: String,
21
22    /// Reason for the reversal request.
23    pub reason: String,
24}
25
26impl Revert {
27    /// Create a new Revert message
28    pub fn new(transaction_id: &str, settlement_address: &str, reason: &str) -> Self {
29        Self {
30            transaction_id: transaction_id.to_string(),
31            settlement_address: settlement_address.to_string(),
32            reason: reason.to_string(),
33        }
34    }
35}
36
37impl Revert {
38    /// Custom validation for Revert messages
39    pub fn validate_revert(&self) -> Result<()> {
40        if self.transaction_id.is_empty() {
41            return Err(Error::Validation(
42                "Transaction ID is required in Revert".to_string(),
43            ));
44        }
45
46        if self.settlement_address.is_empty() {
47            return Err(Error::Validation(
48                "Settlement address is required in Revert".to_string(),
49            ));
50        }
51
52        if self.reason.is_empty() {
53            return Err(Error::Validation(
54                "Reason is required in Revert".to_string(),
55            ));
56        }
57
58        Ok(())
59    }
60}