Skip to main content

mls_rs/group/
exported_tree.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// Copyright by contributors to this project.
3// SPDX-License-Identifier: (Apache-2.0 OR MIT)
4
5use alloc::{borrow::Cow, vec::Vec};
6use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize};
7
8use crate::{client::MlsError, tree_kem::node::NodeVec};
9
10use super::Roster;
11
12#[derive(Debug, MlsSize, MlsEncode, MlsDecode, PartialEq, Clone)]
13pub struct ExportedTree<'a>(pub(crate) Cow<'a, NodeVec>);
14
15impl<'a> ExportedTree<'a> {
16    pub(crate) fn new(node_data: NodeVec) -> Self {
17        Self(Cow::Owned(node_data))
18    }
19
20    pub(crate) fn new_borrowed(node_data: &'a NodeVec) -> Self {
21        Self(Cow::Borrowed(node_data))
22    }
23
24    pub fn to_bytes(&self) -> Result<Vec<u8>, MlsError> {
25        self.mls_encode_to_vec().map_err(Into::into)
26    }
27
28    pub fn byte_size(&self) -> usize {
29        self.mls_encoded_len()
30    }
31
32    pub fn into_owned(self) -> ExportedTree<'static> {
33        ExportedTree(Cow::Owned(self.0.into_owned()))
34    }
35
36    pub fn roster(&'a self) -> Roster<'a> {
37        Roster {
38            public_tree: &self.0,
39        }
40    }
41}
42
43impl ExportedTree<'static> {
44    pub fn from_bytes(bytes: &[u8]) -> Result<Self, MlsError> {
45        Self::mls_decode(&mut &*bytes).map_err(Into::into)
46    }
47}
48
49impl From<ExportedTree<'_>> for NodeVec {
50    fn from(value: ExportedTree) -> Self {
51        value.0.into_owned()
52    }
53}