tap_msg/message/
update_policies.rs

1//! Update Policies message type for the Transaction Authorization Protocol.
2//!
3//! This module defines the UpdatePolicies message type, which is used
4//! to update policies in an existing transaction.
5
6use crate::error::{Error, Result};
7use crate::message::policy::Policy;
8use crate::TapMessage;
9use serde::{Deserialize, Serialize};
10
11/// UpdatePolicies message body (TAIP-7).
12///
13/// This message type allows agents to update their policies for a transaction.
14#[derive(Debug, Clone, Serialize, Deserialize, TapMessage)]
15#[tap(
16    message_type = "https://tap.rsvp/schema/1.0#UpdatePolicies",
17    custom_validation
18)]
19pub struct UpdatePolicies {
20    #[tap(thread_id)]
21    pub transaction_id: String,
22
23    pub policies: Vec<Policy>,
24}
25
26impl UpdatePolicies {
27    /// Creates a new UpdatePolicies message.
28    pub fn new(transaction_id: &str, policies: Vec<Policy>) -> Self {
29        Self {
30            transaction_id: transaction_id.to_string(),
31            policies,
32        }
33    }
34
35    /// Custom validation for UpdatePolicies messages
36    pub fn validate_updatepolicies(&self) -> Result<()> {
37        if self.transaction_id.is_empty() {
38            return Err(Error::Validation(
39                "UpdatePolicies must have a transaction_id".to_string(),
40            ));
41        }
42
43        if self.policies.is_empty() {
44            return Err(Error::Validation(
45                "UpdatePolicies must have at least one policy".to_string(),
46            ));
47        }
48
49        for policy in &self.policies {
50            policy.validate()?;
51        }
52
53        Ok(())
54    }
55}