bitwarden_uniffi/vault/
collections.rs1use 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 pub fn encrypt(&self, collection_view: CollectionView) -> Result<Collection> {
19 Ok(self.0.encrypt(collection_view)?)
20 }
21
22 pub fn encrypt_list(&self, collection_views: Vec<CollectionView>) -> Result<Vec<Collection>> {
24 Ok(self.0.encrypt_list(collection_views)?)
25 }
26
27 pub fn decrypt(&self, collection: Collection) -> Result<CollectionView> {
29 Ok(self.0.decrypt(collection)?)
30 }
31
32 pub fn decrypt_list(&self, collections: Vec<Collection>) -> Result<Vec<CollectionView>> {
34 Ok(self.0.decrypt_list(collections)?)
35 }
36
37 pub fn get_collection_tree(&self, collections: Vec<CollectionView>) -> Arc<CollectionViewTree> {
41 Arc::new(CollectionViewTree {
42 tree: Tree::from_items(collections),
43 })
44 }
45}
46
47#[derive(uniffi::Object)]
48pub struct CollectionViewTree {
49 tree: Tree<CollectionView>,
50}
51
52#[derive(uniffi::Object)]
53pub struct CollectionViewNodeItem {
54 node_item: NodeItem<CollectionView>,
55}
56
57#[uniffi::export]
58impl CollectionViewTree {
59 pub fn get_item_for_view(
60 &self,
61 collection_view: CollectionView,
62 ) -> Option<Arc<CollectionViewNodeItem>> {
63 self.tree
64 .get_item_by_id(collection_view.id.unwrap_or_default().into())
65 .map(|n| Arc::new(CollectionViewNodeItem { node_item: n }))
66 }
67
68 pub fn get_root_items(&self) -> Vec<Arc<CollectionViewNodeItem>> {
69 self.tree
70 .get_root_items()
71 .into_iter()
72 .map(|n| Arc::new(CollectionViewNodeItem { node_item: n }))
73 .collect()
74 }
75
76 pub fn get_flat_items(&self) -> Vec<Arc<CollectionViewNodeItem>> {
77 self.tree
78 .get_flat_items()
79 .into_iter()
80 .map(|n| Arc::new(CollectionViewNodeItem { node_item: n }))
81 .collect()
82 }
83}
84
85#[uniffi::export]
86impl CollectionViewNodeItem {
87 pub fn get_item(&self) -> CollectionView {
88 self.node_item.item.clone()
89 }
90
91 pub fn get_parent(&self) -> Option<CollectionView> {
92 self.node_item.parent.clone()
93 }
94
95 pub fn get_children(&self) -> Vec<CollectionView> {
96 self.node_item.children.clone()
97 }
98
99 pub fn get_ancestors(&self) -> AncestorMap {
100 AncestorMap {
101 ancestors: self
102 .node_item
103 .ancestors
104 .iter()
105 .map(|(&uuid, name)| (CollectionId::new(uuid), name.clone()))
106 .collect(),
107 }
108 }
109}