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#[cfg_attr(
13    all(feature = "ffi", not(test)),
14    safer_ffi_gen::ffi_type(clone, opaque)
15)]
16#[derive(Debug, MlsSize, MlsEncode, MlsDecode, PartialEq, Clone)]
17pub struct ExportedTree<'a>(pub(crate) Cow<'a, NodeVec>);
18
19#[cfg_attr(all(feature = "ffi", not(test)), ::safer_ffi_gen::safer_ffi_gen)]
20impl<'a> ExportedTree<'a> {
21    pub(crate) fn new(node_data: NodeVec) -> Self {
22        Self(Cow::Owned(node_data))
23    }
24
25    pub(crate) fn new_borrowed(node_data: &'a NodeVec) -> Self {
26        Self(Cow::Borrowed(node_data))
27    }
28
29    pub fn to_bytes(&self) -> Result<Vec<u8>, MlsError> {
30        self.mls_encode_to_vec().map_err(Into::into)
31    }
32
33    pub fn byte_size(&self) -> usize {
34        self.mls_encoded_len()
35    }
36
37    pub fn into_owned(self) -> ExportedTree<'static> {
38        ExportedTree(Cow::Owned(self.0.into_owned()))
39    }
40
41    pub fn roster(&'a self) -> Roster<'a> {
42        Roster {
43            public_tree: &self.0,
44        }
45    }
46}
47
48#[cfg_attr(all(feature = "ffi", not(test)), ::safer_ffi_gen::safer_ffi_gen)]
49impl ExportedTree<'static> {
50    pub fn from_bytes(bytes: &[u8]) -> Result<Self, MlsError> {
51        Self::mls_decode(&mut &*bytes).map_err(Into::into)
52    }
53}
54
55impl From<ExportedTree<'_>> for NodeVec {
56    fn from(value: ExportedTree) -> Self {
57        value.0.into_owned()
58    }
59}