tap_msg/message/
relationship.rs

1//! Relationship confirmation message types for the Transaction Authorization Protocol.
2//!
3//! This module defines the ConfirmRelationship message type, which is used to confirm
4//! relationships between agents in the TAP protocol.
5
6use crate::error::{Error, Result};
7use crate::TapMessage;
8use serde::{Deserialize, Serialize};
9
10/// ConfirmRelationship message body (TAIP-9).
11///
12/// This message type allows confirming a relationship between agents.
13#[derive(Debug, Clone, Serialize, Deserialize, TapMessage)]
14#[tap(message_type = "https://tap.rsvp/schema/1.0#ConfirmRelationship")]
15pub struct ConfirmRelationship {
16    /// ID of the transaction related to this message.
17    #[serde(rename = "transfer_id")]
18    #[tap(thread_id)]
19    pub transaction_id: String,
20
21    /// DID of the agent whose relationship is being confirmed.
22    #[serde(rename = "@id")]
23    pub agent_id: String,
24
25    /// The entity this agent is acting for.
26    #[serde(rename = "for")]
27    pub for_entity: String,
28
29    /// The role of the agent (optional).
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub role: Option<String>,
32}
33
34impl ConfirmRelationship {
35    /// Creates a new ConfirmRelationship message body.
36    pub fn new(transaction_id: &str, agent_id: &str, for_entity: &str) -> Self {
37        Self {
38            transaction_id: transaction_id.to_string(),
39            agent_id: agent_id.to_string(),
40            for_entity: for_entity.to_string(),
41            role: None,
42        }
43    }
44
45    /// Add a role to the confirmation.
46    pub fn with_role(mut self, role: &str) -> Self {
47        self.role = Some(role.to_string());
48        self
49    }
50}
51
52// Note: The validate() method name conflicts with auto-generated validate,
53// so we rename the custom one
54impl ConfirmRelationship {
55    /// Custom validation for ConfirmRelationship messages
56    pub fn validate_relationship(&self) -> Result<()> {
57        if self.transaction_id.is_empty() {
58            return Err(Error::Validation(
59                "Transfer ID is required in ConfirmRelationship".to_string(),
60            ));
61        }
62
63        if self.agent_id.is_empty() {
64            return Err(Error::Validation(
65                "Agent ID (@id) is required in ConfirmRelationship".to_string(),
66            ));
67        }
68
69        if self.for_entity.is_empty() {
70            return Err(Error::Validation(
71                "For entity is required in ConfirmRelationship".to_string(),
72            ));
73        }
74
75        Ok(())
76    }
77}