bitwarden_uniffi/vault/
collections.rs1use std::{collections::HashMap, sync::Arc};
2
3use bitwarden_collections::{
4 collection::{Collection, CollectionView},
5 tree::{NodeItem, Tree},
6};
7use uuid::Uuid;
8
9use crate::{error::Error, 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 decrypt(&self, collection: Collection) -> Result<CollectionView> {
19 Ok(self.0.decrypt(collection).map_err(Error::Decrypt)?)
20 }
21
22 pub fn decrypt_list(&self, collections: Vec<Collection>) -> Result<Vec<CollectionView>> {
24 Ok(self.0.decrypt_list(collections).map_err(Error::Decrypt)?)
25 }
26
27 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)]
43#[allow(unused)]
44pub struct CollectionViewNodeItem {
45 node_item: NodeItem<CollectionView>,
46}
47
48#[uniffi::export]
49impl CollectionViewTree {
50 pub fn get_item_by_id(&self, collection_id: Uuid) -> Option<Arc<CollectionViewNodeItem>> {
51 self.tree
52 .get_item_by_id(collection_id)
53 .map(|n| Arc::new(CollectionViewNodeItem { node_item: n }))
54 }
55
56 pub fn get_root_items(&self) -> Vec<Arc<CollectionViewNodeItem>> {
57 self.tree
58 .nodes
59 .iter()
60 .filter(|n| n.parent_idx.is_none())
61 .filter_map(|n| self.get_item_by_id(n.item_id))
62 .collect()
63 }
64}
65
66#[uniffi::export]
67impl CollectionViewNodeItem {
68 pub fn get_item(&self) -> CollectionView {
69 self.node_item.item.clone()
70 }
71
72 pub fn get_parent(&self) -> Option<CollectionView> {
73 self.node_item.parent.clone()
74 }
75
76 pub fn get_children(&self) -> Vec<CollectionView> {
77 self.node_item.children.clone()
78 }
79
80 pub fn get_ancestors(&self) -> HashMap<Uuid, String> {
81 self.node_item.ancestors.clone()
82 }
83}