bitwarden_uniffi/vault/
collections.rs

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