openmls/messages/proposals/
app_data_update.rs

1use super::*;
2use crate::component::ComponentId;
3
4/// [`AppDataUpdateProposal`] operation types
5#[repr(u8)]
6#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Serialize, Deserialize, Hash)]
7pub enum AppDataUpdateOperationType {
8    /// Update operation
9    Update = 1,
10    /// Remove operation
11    Remove = 2,
12}
13
14#[repr(u8)]
15#[derive(
16    Debug,
17    PartialEq,
18    Eq,
19    Clone,
20    Serialize,
21    Deserialize,
22    TlsDeserialize,
23    TlsDeserializeBytes,
24    TlsSerialize,
25    TlsSize,
26)]
27/// The operation that is part of an [`AppDataUpdateProposal`].
28pub enum AppDataUpdateOperation {
29    /// Update operation, containing update data
30    #[tls_codec(discriminant = 1)]
31    Update(VLBytes) = 1,
32    /// Remove operation
33    #[tls_codec(discriminant = 2)]
34    Remove = 2,
35}
36
37impl AppDataUpdateOperation {
38    /// Returns the operation type.
39    pub fn operation_type(&self) -> AppDataUpdateOperationType {
40        match self {
41            AppDataUpdateOperation::Update(_) => AppDataUpdateOperationType::Update,
42            AppDataUpdateOperation::Remove => AppDataUpdateOperationType::Remove,
43        }
44    }
45}
46
47/// AppDataUpdate Proposal.
48///
49/// ```c
50/// struct {
51///     ComponentID component_id;
52///     AppDataUpdateOperation op;
53///
54///     select (AppDataUpdate.op) {
55///     case update: opaque update<V>;
56///     case remove: struct{};
57///     };
58/// } AppDataUpdate;
59/// ```
60#[derive(
61    Debug,
62    PartialEq,
63    Clone,
64    Serialize,
65    Deserialize,
66    TlsSize,
67    TlsSerialize,
68    TlsDeserialize,
69    TlsDeserializeBytes,
70)]
71pub struct AppDataUpdateProposal {
72    component_id: ComponentId,
73    operation: AppDataUpdateOperation,
74}
75
76impl AppDataUpdateProposal {
77    /// Create a new AppDataUpdateProposal containing an Update operation.
78    pub fn update(component_id: ComponentId, data: impl Into<VLBytes>) -> Self {
79        Self::new(component_id, AppDataUpdateOperation::Update(data.into()))
80    }
81    /// Create a new AppDataUpdateProposal containing a Remove operation.
82    pub fn remove(component_id: ComponentId) -> Self {
83        Self::new(component_id, AppDataUpdateOperation::Remove)
84    }
85    pub(crate) fn new(component_id: ComponentId, operation: AppDataUpdateOperation) -> Self {
86        Self {
87            component_id,
88            operation,
89        }
90    }
91
92    /// Return the [`ComponentId`] for this proposal.
93    pub fn component_id(&self) -> ComponentId {
94        self.component_id
95    }
96    /// Return the [`AppDataUpdateOperation`] for this proposal.
97    pub fn operation(&self) -> &AppDataUpdateOperation {
98        &self.operation
99    }
100}