Skip to content

Commit

Permalink
fix: use getter for all_opened
Browse files Browse the repository at this point in the history
dont allow access to struct field
  • Loading branch information
EdJoPaTo committed Oct 31, 2020
1 parent b585b9d commit 5265bf1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<'a> StatefulTree<'a> {
}

fn move_up_down(&mut self, down: bool) {
let visible = flatten(&self.state.opened, &self.items);
let visible = flatten(&self.state.get_all_opened(), &self.items);
let current_identifier = self.state.selected();
let current_index = visible
.iter()
Expand Down
17 changes: 6 additions & 11 deletions src/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use crate::identifier::{TreeIdentifier, TreeIdentifierVec};
use crate::TreeItem;
use std::collections::HashSet;

pub struct Flattened<'a> {
pub identifier: Vec<usize>,
Expand All @@ -15,15 +14,12 @@ impl<'a> Flattened<'a> {
}
}

pub fn flatten<'a>(
opened: &HashSet<TreeIdentifierVec>,
items: &'a [TreeItem<'a>],
) -> Vec<Flattened<'a>> {
pub fn flatten<'a>(opened: &[TreeIdentifierVec], items: &'a [TreeItem<'a>]) -> Vec<Flattened<'a>> {
internal(opened, items, &[])
}

fn internal<'a>(
opened: &HashSet<TreeIdentifierVec>,
opened: &[TreeIdentifierVec],
items: &'a [TreeItem<'a>],
current: TreeIdentifier,
) -> Vec<Flattened<'a>> {
Expand Down Expand Up @@ -78,8 +74,7 @@ fn get_example_tree_items<'a>() -> Vec<TreeItem<'a>> {
#[test]
fn get_opened_nothing_opened_is_top_level() {
let items = get_example_tree_items();
let opened = HashSet::new();
let result = flatten(&opened, &items);
let result = flatten(&[], &items);
let result_text: Vec<_> = result
.iter()
.map(|o| get_naive_string_from_text(&o.item.text))
Expand All @@ -90,7 +85,7 @@ fn get_opened_nothing_opened_is_top_level() {
#[test]
fn get_opened_wrong_opened_is_only_top_level() {
let items = get_example_tree_items();
let opened = [vec![0], vec![1, 1]].iter().cloned().collect();
let opened = [vec![0], vec![1, 1]];
let result = flatten(&opened, &items);
let result_text: Vec<_> = result
.iter()
Expand All @@ -102,7 +97,7 @@ fn get_opened_wrong_opened_is_only_top_level() {
#[test]
fn get_opened_one_is_opened() {
let items = get_example_tree_items();
let opened = [vec![1]].iter().cloned().collect();
let opened = [vec![1]];
let result = flatten(&opened, &items);
let result_text: Vec<_> = result
.iter()
Expand All @@ -114,7 +109,7 @@ fn get_opened_one_is_opened() {
#[test]
fn get_opened_all_opened() {
let items = get_example_tree_items();
let opened = [vec![1], vec![1, 1]].iter().cloned().collect();
let opened = [vec![1], vec![1, 1]];
let result = flatten(&opened, &items);
let result_text: Vec<_> = result
.iter()
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use self::flatten::flatten;
pub struct TreeState {
offset: usize,
selected: TreeIdentifierVec,
pub opened: HashSet<TreeIdentifierVec>,
opened: HashSet<TreeIdentifierVec>,
}

impl Default for TreeState {
Expand Down Expand Up @@ -64,6 +64,10 @@ impl TreeState {
pub fn close_all(&mut self) {
self.opened.clear();
}

pub fn get_all_opened(&self) -> Vec<TreeIdentifierVec> {
self.opened.iter().cloned().collect()
}
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -184,7 +188,7 @@ impl<'a> StatefulWidget for Tree<'a> {
return;
}

let visible = flatten(&state.opened, &self.items);
let visible = flatten(&state.get_all_opened(), &self.items);
if visible.is_empty() {
return;
}
Expand Down

0 comments on commit 5265bf1

Please sign in to comment.