docs: use doc comments and doc tests
This commit is contained in:
+24
-25
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user