Skip to content

Commit

Permalink
docs: use doc comments and doc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Nov 6, 2022
1 parent cf104bb commit a902bce
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl<'a> Flattened<'a> {
}
}

/// Get a flat list of all visible [`TreeItem`s](TreeItem)
pub fn flatten<'a>(opened: &[TreeIdentifierVec], items: &'a [TreeItem<'a>]) -> Vec<Flattened<'a>> {
internal(opened, items, &[])
}
Expand Down
49 changes: 24 additions & 25 deletions src/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
#![allow(clippy::module_name_repetitions)]

/// Reference to a [`TreeItem`](crate::TreeItem) in a [`Tree`](crate::Tree)
pub type TreeIdentifier<'a> = &'a [usize];
/// Reference to a [`TreeItem`](crate::TreeItem) in a [`Tree`](crate::Tree)
pub type TreeIdentifierVec = Vec<usize>;

pub fn get_without_leaf(identifier: &[usize]) -> (&[usize], Option<&usize>) {
/// Split a [`TreeIdentifier`] into its branch and leaf
///
/// # Examples
///
/// ```
/// # use tui_tree_widget::get_identifier_without_leaf;
/// let (branch, leaf) = get_identifier_without_leaf(&[2, 4, 6]);
/// assert_eq!(branch, [2, 4]);
/// assert_eq!(leaf, Some(&6));
///
/// let (branch, leaf) = get_identifier_without_leaf(&[2]);
/// assert_eq!(branch, []);
/// assert_eq!(leaf, Some(&2));
///
/// let (branch, leaf) = get_identifier_without_leaf(&[]);
/// assert_eq!(branch, []);
/// assert_eq!(leaf, None);
/// ```
pub fn get_without_leaf(identifier: TreeIdentifier) -> (TreeIdentifier, Option<&usize>) {
let length = identifier.len();
let length_without_leaf = length.saturating_sub(1);

let head = &identifier[0..length_without_leaf];
let tail = identifier.get(length_without_leaf);
let branch = &identifier[0..length_without_leaf];
let leaf = identifier.get(length_without_leaf);

(head, tail)
}

#[test]
fn get_without_leaf_empty() {
let (head, tail) = get_without_leaf(&[]);
assert_eq!(head.len(), 0);
assert_eq!(tail, None);
}

#[test]
fn get_without_leaf_single() {
let (head, tail) = get_without_leaf(&[2]);
assert_eq!(head.len(), 0);
assert_eq!(tail, Some(&2));
}

#[test]
fn get_without_leaf_multiple() {
let (head, tail) = get_without_leaf(&[2, 4, 6]);
assert_eq!(head, [2, 4]);
assert_eq!(tail, Some(&6));
(branch, leaf)
}
46 changes: 46 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ pub use crate::identifier::{
get_without_leaf as get_identifier_without_leaf, TreeIdentifier, TreeIdentifierVec,
};

/// Keeps the state of what is currently selected and what was opened in a [`Tree`]
///
/// # Example
///
/// ```
/// # use tui_tree_widget::TreeState;
/// let mut state = TreeState::default();
/// ```
#[derive(Debug, Default, Clone)]
pub struct TreeState {
offset: usize,
Expand Down Expand Up @@ -149,6 +157,17 @@ impl TreeState {
}
}

/// One item inside a [`Tree`]
///
/// Can zero or more `children`.
///
/// # Example
///
/// ```
/// # use tui_tree_widget::TreeItem;
/// let a = TreeItem::new_leaf("leaf");
/// let b = TreeItem::new("root", vec![a]);
/// ```
#[derive(Debug, Clone)]
pub struct TreeItem<'a> {
text: Text<'a>,
Expand Down Expand Up @@ -207,6 +226,33 @@ impl<'a> TreeItem<'a> {
}
}

/// A `Tree` which can be rendered
///
/// # Example
///
/// ```
/// # use tui_tree_widget::{Tree, TreeItem, TreeState};
/// # use tui::backend::TestBackend;
/// # use tui::Terminal;
/// # use tui::widgets::{Block, Borders};
/// # fn main() -> std::io::Result<()> {
/// # let mut terminal = Terminal::new(TestBackend::new(32, 32)).unwrap();
/// let mut state = TreeState::default();
///
/// let item = TreeItem::new_leaf("leaf");
/// let items = vec![item];
///
/// terminal.draw(|f| {
/// let area = f.size();
///
/// let tree_widget = Tree::new(items.clone())
/// .block(Block::default().borders(Borders::ALL).title("Tree Widget"));
///
/// f.render_stateful_widget(tree_widget, area, &mut state);
/// })?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct Tree<'a> {
block: Option<Block<'a>>,
Expand Down

0 comments on commit a902bce

Please sign in to comment.