Skip to content

Added not wasm config for get_ancestors #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 4 additions & 57 deletions crates/bitwarden-uniffi/src/vault/collections.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::{collections::HashMap, sync::Arc};
use std::sync::Arc;

use bitwarden_collections::{
collection::{Collection, CollectionView},
tree::{NodeItem, Tree},
};
use uuid::Uuid;
use bitwarden_collections::collection::{Collection, CollectionView};
use bitwarden_vault::collection_client::CollectionViewTree;

use crate::{error::Error, Result};

Expand All @@ -28,56 +25,6 @@ impl CollectionsClient {
/// Returns the vector of CollectionView objects in a tree structure based on its implemented
/// path().
pub fn get_collection_tree(&self, collections: Vec<CollectionView>) -> Arc<CollectionViewTree> {
Arc::new(CollectionViewTree {
tree: Tree::from_items(collections),
})
}
}

#[derive(uniffi::Object)]
pub struct CollectionViewTree {
tree: Tree<CollectionView>,
}

#[derive(uniffi::Object)]
#[allow(unused)]
pub struct CollectionViewNodeItem {
node_item: NodeItem<CollectionView>,
}

#[uniffi::export]
impl CollectionViewTree {
pub fn get_item_by_id(&self, collection_id: Uuid) -> Option<Arc<CollectionViewNodeItem>> {
self.tree
.get_item_by_id(collection_id)
.map(|n| Arc::new(CollectionViewNodeItem { node_item: n }))
}

pub fn get_root_items(&self) -> Vec<Arc<CollectionViewNodeItem>> {
self.tree
.nodes
.iter()
.filter(|n| n.parent_idx.is_none())
.filter_map(|n| self.get_item_by_id(n.item_id))
.collect()
}
}

#[uniffi::export]
impl CollectionViewNodeItem {
pub fn get_item(&self) -> CollectionView {
self.node_item.item.clone()
}

pub fn get_parent(&self) -> Option<CollectionView> {
self.node_item.parent.clone()
}

pub fn get_children(&self) -> Vec<CollectionView> {
self.node_item.children.clone()
}

pub fn get_ancestors(&self) -> HashMap<Uuid, String> {
self.node_item.ancestors.clone()
Arc::new(self.0.get_collection_tree(collections))
}
}
30 changes: 20 additions & 10 deletions crates/bitwarden-vault/src/collection_client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use std::collections::HashMap;

use bitwarden_collections::{
collection::{Collection, CollectionView},
tree::{NodeItem, Tree},
};
use bitwarden_core::Client;
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use bitwarden_error::js_sys::Map;
use tsify::Tsify;
#[cfg(feature = "uniffi")]
use uniffi;
use uuid::Uuid;
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::wasm_bindgen;

Expand Down Expand Up @@ -47,15 +53,24 @@ impl CollectionsClient {
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
pub struct CollectionViewTree {
tree: Tree<CollectionView>,
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
pub struct CollectionViewNodeItem {
node_item: NodeItem<CollectionView>,
}

#[cfg_attr(feature = "wasm", derive(Tsify, Serialize, Deserialize))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct AncestorMap {
ancestors: HashMap<Uuid, String>,
}

#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl CollectionViewNodeItem {
pub fn get_item(&self) -> CollectionView {
Expand All @@ -70,15 +85,10 @@ impl CollectionViewNodeItem {
self.node_item.children.clone()
}

#[cfg(feature = "wasm")]
pub fn get_ancestors(&self) -> Map {
self.node_item
.ancestors
.iter()
.fold(Map::new(), |map, (id, name)| {
map.set(&id.to_string().into(), &name.into());
map
})
pub fn get_ancestors(&self) -> AncestorMap {
AncestorMap {
ancestors: self.node_item.ancestors.clone(),
}
}
}

Expand Down
Loading