Skip to content

Commit ae9b8b5

Browse files
authored
Added not wasm config for get_ancestors (#386)
Adds conditional code for when wasm feature isn't enabled for get_ancestors. [PM-25167]: https://bitwarden.atlassian.net/browse/PM-25167?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
1 parent 99cca6a commit ae9b8b5

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-24
lines changed

crates/bitwarden-uniffi/src/vault/collections.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use std::{collections::HashMap, sync::Arc};
1+
use std::sync::Arc;
22

33
use bitwarden_collections::{
4-
collection::{Collection, CollectionView},
4+
collection::{Collection, CollectionId, CollectionView},
55
tree::{NodeItem, Tree},
66
};
7-
use uuid::Uuid;
7+
use bitwarden_vault::collection_client::AncestorMap;
88

99
use crate::Result;
1010

@@ -40,25 +40,34 @@ pub struct CollectionViewTree {
4040
}
4141

4242
#[derive(uniffi::Object)]
43-
#[allow(unused)]
4443
pub struct CollectionViewNodeItem {
4544
node_item: NodeItem<CollectionView>,
4645
}
4746

4847
#[uniffi::export]
4948
impl CollectionViewTree {
50-
pub fn get_item_by_id(&self, collection_id: Uuid) -> Option<Arc<CollectionViewNodeItem>> {
49+
pub fn get_item_for_view(
50+
&self,
51+
collection_view: CollectionView,
52+
) -> Option<Arc<CollectionViewNodeItem>> {
5153
self.tree
52-
.get_item_by_id(collection_id)
54+
.get_item_by_id(collection_view.id.unwrap_or_default().into())
5355
.map(|n| Arc::new(CollectionViewNodeItem { node_item: n }))
5456
}
5557

5658
pub fn get_root_items(&self) -> Vec<Arc<CollectionViewNodeItem>> {
5759
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))
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 }))
6271
.collect()
6372
}
6473
}
@@ -77,7 +86,14 @@ impl CollectionViewNodeItem {
7786
self.node_item.children.clone()
7887
}
7988

80-
pub fn get_ancestors(&self) -> HashMap<Uuid, String> {
81-
self.node_item.ancestors.clone()
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+
}
8298
}
8399
}

crates/bitwarden-vault/src/collection_client.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
use std::collections::HashMap;
2+
13
use bitwarden_collections::{
2-
collection::{Collection, CollectionView},
4+
collection::{Collection, CollectionId, CollectionView},
35
tree::{NodeItem, Tree},
46
};
57
use bitwarden_core::Client;
8+
use serde::{Deserialize, Serialize};
69
#[cfg(feature = "wasm")]
7-
use bitwarden_error::js_sys::Map;
10+
use tsify::Tsify;
811
#[cfg(feature = "wasm")]
912
use wasm_bindgen::prelude::wasm_bindgen;
1013

@@ -56,6 +59,16 @@ pub struct CollectionViewNodeItem {
5659
node_item: NodeItem<CollectionView>,
5760
}
5861

62+
#[cfg_attr(
63+
feature = "wasm",
64+
derive(Tsify, Serialize, Deserialize),
65+
tsify(into_wasm_abi, from_wasm_abi)
66+
)]
67+
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
68+
pub struct AncestorMap {
69+
pub ancestors: HashMap<CollectionId, String>,
70+
}
71+
5972
#[cfg_attr(feature = "wasm", wasm_bindgen)]
6073
impl CollectionViewNodeItem {
6174
pub fn get_item(&self) -> CollectionView {
@@ -70,21 +83,21 @@ impl CollectionViewNodeItem {
7083
self.node_item.children.clone()
7184
}
7285

73-
#[cfg(feature = "wasm")]
74-
pub fn get_ancestors(&self) -> Map {
75-
self.node_item
76-
.ancestors
77-
.iter()
78-
.fold(Map::new(), |map, (id, name)| {
79-
map.set(&id.to_string().into(), &name.into());
80-
map
81-
})
86+
pub fn get_ancestors(&self) -> AncestorMap {
87+
AncestorMap {
88+
ancestors: self
89+
.node_item
90+
.ancestors
91+
.iter()
92+
.map(|(&uuid, name)| (CollectionId::new(uuid), name.clone()))
93+
.collect(),
94+
}
8295
}
8396
}
8497

8598
#[cfg_attr(feature = "wasm", wasm_bindgen)]
8699
impl CollectionViewTree {
87-
pub fn get_item_by_id(
100+
pub fn get_item_for_view(
88101
&self,
89102
collection_view: CollectionView,
90103
) -> Option<CollectionViewNodeItem> {

0 commit comments

Comments
 (0)