-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: use doc comments and doc tests
- Loading branch information
Showing
3 changed files
with
71 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters