Skip to main content

bitwarden_uniffi/vault/
collections.rs

1use std::sync::Arc;
2
3use bitwarden_collections::{
4    collection::{Collection, CollectionId, CollectionView},
5    collection_client::{AncestorMap, DecryptCollectionListResult},
6    tree::{NodeItem, Tree},
7};
8
9use crate::Result;
10
11#[allow(missing_docs)]
12#[derive(uniffi::Object)]
13pub struct CollectionsClient(
14    pub(crate) bitwarden_collections::collection_client::CollectionsClient,
15);
16
17#[uniffi::export]
18impl CollectionsClient {
19    /// Encrypt collection
20    pub fn encrypt(&self, collection_view: CollectionView) -> Result<Collection> {
21        Ok(self.0.encrypt(collection_view)?)
22    }
23
24    /// Encrypt collection list
25    pub fn encrypt_list(&self, collection_views: Vec<CollectionView>) -> Result<Vec<Collection>> {
26        Ok(self.0.encrypt_list(collection_views)?)
27    }
28
29    /// Decrypt collection
30    pub fn decrypt(&self, collection: Collection) -> Result<CollectionView> {
31        Ok(self.0.decrypt(collection)?)
32    }
33
34    /// Decrypt collection list
35    pub fn decrypt_list(&self, collections: Vec<Collection>) -> Result<Vec<CollectionView>> {
36        Ok(self.0.decrypt_list(collections)?)
37    }
38
39    /// Decrypt collection list with failures
40    /// Returns both successfully decrypted collections and any that failed to decrypt
41    pub fn decrypt_list_with_failures(
42        &self,
43        collections: Vec<Collection>,
44    ) -> DecryptCollectionListResult {
45        self.0.decrypt_list_with_failures(collections)
46    }
47
48    ///
49    /// Returns the vector of CollectionView objects in a tree structure based on its implemented
50    /// path().
51    pub fn get_collection_tree(&self, collections: Vec<CollectionView>) -> Arc<CollectionViewTree> {
52        Arc::new(CollectionViewTree {
53            tree: Tree::from_items(collections),
54        })
55    }
56}
57
58#[derive(uniffi::Object)]
59pub struct CollectionViewTree {
60    tree: Tree<CollectionView>,
61}
62
63#[derive(uniffi::Object)]
64pub struct CollectionViewNodeItem {
65    node_item: NodeItem<CollectionView>,
66}
67
68#[uniffi::export]
69impl CollectionViewTree {
70    pub fn get_item_for_view(
71        &self,
72        collection_view: CollectionView,
73    ) -> Option<Arc<CollectionViewNodeItem>> {
74        self.tree
75            .get_item_by_id(collection_view.id.unwrap_or_default().into())
76            .map(|n| Arc::new(CollectionViewNodeItem { node_item: n }))
77    }
78
79    pub fn get_root_items(&self) -> Vec<Arc<CollectionViewNodeItem>> {
80        self.tree
81            .get_root_items()
82            .into_iter()
83            .map(|n| Arc::new(CollectionViewNodeItem { node_item: n }))
84            .collect()
85    }
86
87    pub fn get_flat_items(&self) -> Vec<Arc<CollectionViewNodeItem>> {
88        self.tree
89            .get_flat_items()
90            .into_iter()
91            .map(|n| Arc::new(CollectionViewNodeItem { node_item: n }))
92            .collect()
93    }
94}
95
96#[uniffi::export]
97impl CollectionViewNodeItem {
98    pub fn get_item(&self) -> CollectionView {
99        self.node_item.item.clone()
100    }
101
102    pub fn get_parent(&self) -> Option<CollectionView> {
103        self.node_item.parent.clone()
104    }
105
106    pub fn get_children(&self) -> Vec<CollectionView> {
107        self.node_item.children.clone()
108    }
109
110    pub fn get_ancestors(&self) -> AncestorMap {
111        AncestorMap {
112            ancestors: self
113                .node_item
114                .ancestors
115                .iter()
116                .map(|(&uuid, name)| (CollectionId::new(uuid), name.clone()))
117                .collect(),
118        }
119    }
120}