Skip to main content

Node

Struct Node 

Source
pub struct Node<BackendT, ContextT, ErrorT, IdT, DataT> {
    pub depth: usize,
    pub kind: NodeKind,
    pub id: IdT,
    pub representation: Representation,
    pub representation_size: XY<usize>,
    pub branch_state: BranchState,
    pub children: Option<NodeList<BackendT, ContextT, ErrorT, IdT, DataT>>,
    pub data: Option<DataT>,
    /* private fields */
}
Expand description

Tree node.

Fields§

§depth: usize

Depth.

§kind: NodeKind

Kind.

§id: IdT

ID.

§representation: Representation

Representation.

§representation_size: XY<usize>

Representation size.

Cached so we only have to calculate it once.

§branch_state: BranchState

State for Branch node.

§children: Option<NodeList<BackendT, ContextT, ErrorT, IdT, DataT>>

Children for Branch node.

§data: Option<DataT>

Data.

Implementations§

Source§

impl<BackendT, ContextT, ErrorT, IdT, DataT> Node<BackendT, ContextT, ErrorT, IdT, DataT>
where BackendT: TreeBackend<ContextT, ErrorT, IdT, DataT>,

Source

pub fn new( depth: usize, kind: NodeKind, id: IdT, representation: Representation, ) -> Self

Constructor.

Source

pub fn symbol(&self, context: ContextT) -> Symbol<'_>

Symbol.

Its char count is always 1.

Source

pub fn at_path(&self, path: NodePath) -> Option<&Self>

Get node at path.

Source

pub fn at_path_mut(&mut self, path: NodePath) -> Option<&mut Self>

Get node at path.

Source

pub fn fill_path(&self, path: &mut NodePath, node: &Self) -> bool

Fill path to node.

Returns true if found.

Source

pub fn populate( &mut self, depth: Option<usize>, context: ContextT, ) -> Result<(), ErrorT>
where ContextT: Clone,

Fetch nodes from backend.

If depth is None will populate all depths.

If depth is 0 will do nothing.

Source

pub fn add_child( &mut self, kind: NodeKind, id: IdT, representation: Representation, )

Add a child node.

Examples found in repository?
examples/simple.rs (line 23)
8fn main() {
9    let mut cursive = Cursive::default();
10
11    let mut tree = SimpleTreeBackend::tree_view(());
12
13    // A few roots
14
15    tree.model.add_root(NodeKind::Leaf, (), "Hello".into());
16    tree.model.add_root(NodeKind::Branch, (), "World".into());
17
18    // Add 10 children to "World"
19    // and expand it
20
21    let world = tree.model.at_path_mut([1].into()).unwrap();
22    for i in 0..10 {
23        world.add_child(NodeKind::Leaf, (), format!("Child #{}", i + 1).into());
24    }
25    world.expand_branch(()).unwrap();
26
27    // Add 10 grandchildren each to "Child #5" to "Child #10"
28    // Note that we change then to a branch in order to support having children
29
30    for c in 4..10 {
31        let child = tree.model.at_path_mut([1, c].into()).unwrap();
32        child.kind = NodeKind::Branch;
33        for gc in 0..10 {
34            child.add_child(NodeKind::Leaf, (), format!("Grandchild #{}", gc + 1).into());
35        }
36    }
37
38    cursive.add_fullscreen_layer(tree.scrollable());
39    cursive.add_global_callback('q', |cursive| cursive.quit());
40
41    cursive.run();
42}
Source

pub fn insert_child( &mut self, index: usize, kind: NodeKind, id: IdT, representation: Representation, )

Inserts a child node.

Source

pub fn expand_branch(&mut self, context: ContextT) -> Result<bool, ErrorT>
where ContextT: Clone,

Expand the branch.

Will return false if not a Branch or already expanded.

Examples found in repository?
examples/simple.rs (line 25)
8fn main() {
9    let mut cursive = Cursive::default();
10
11    let mut tree = SimpleTreeBackend::tree_view(());
12
13    // A few roots
14
15    tree.model.add_root(NodeKind::Leaf, (), "Hello".into());
16    tree.model.add_root(NodeKind::Branch, (), "World".into());
17
18    // Add 10 children to "World"
19    // and expand it
20
21    let world = tree.model.at_path_mut([1].into()).unwrap();
22    for i in 0..10 {
23        world.add_child(NodeKind::Leaf, (), format!("Child #{}", i + 1).into());
24    }
25    world.expand_branch(()).unwrap();
26
27    // Add 10 grandchildren each to "Child #5" to "Child #10"
28    // Note that we change then to a branch in order to support having children
29
30    for c in 4..10 {
31        let child = tree.model.at_path_mut([1, c].into()).unwrap();
32        child.kind = NodeKind::Branch;
33        for gc in 0..10 {
34            child.add_child(NodeKind::Leaf, (), format!("Grandchild #{}", gc + 1).into());
35        }
36    }
37
38    cursive.add_fullscreen_layer(tree.scrollable());
39    cursive.add_global_callback('q', |cursive| cursive.quit());
40
41    cursive.run();
42}
Source

pub fn collapse_branch(&mut self) -> bool

Collapse the branch.

Will return false if not a Branch or already collapsed.

Source

pub fn toggle_branch_state(&mut self, context: ContextT) -> Result<(), ErrorT>
where ContextT: Clone,

Toggle the branch state.

Expand if collapsed, collapse if expanded.

Will do nothing if not a Branch.

Source

pub fn data( &mut self, context: ContextT, ) -> Result<Option<Cow<'_, DataT>>, ErrorT>
where DataT: Clone,

Data.

If not already cached will fetch it from the backend.

Examples found in repository?
examples/file_browser.rs (line 73)
66    fn handle_selection_changed(cursive: &mut Cursive) {
67        let content = match cursive.call_on_name(
68            "tree",
69            |tree: &mut TreeView<Self, Arc<PathBuf>, io::Error, PathBuf, Metadata>| {
70                // Although we're not using the context in data() but we still need to provide it
71                let base_directory = tree.model.context.clone();
72                Ok(match tree.selected_node_mut() {
73                    Some(node) => match node.data(base_directory)? {
74                        Some(metadata) => Some(format_metadata(&metadata)?),
75                        None => None,
76                    },
77                    None => None,
78                })
79            },
80        ) {
81            Some(Ok(Some(text))) => text,
82            Some(Err(error)) => return Self::handle_error(cursive, error),
83            _ => "".into(),
84        };
85
86        cursive.call_on_name("details", |details: &mut TextView| {
87            details.set_content(content);
88        });
89    }

Trait Implementations§

Source§

impl<BackendT, ContextT, ErrorT, IdT, DataT> FromIterator<Node<BackendT, ContextT, ErrorT, IdT, DataT>> for NodeList<BackendT, ContextT, ErrorT, IdT, DataT>

Source§

fn from_iter<IteratorT>(iterator: IteratorT) -> Self
where IteratorT: IntoIterator<Item = Node<BackendT, ContextT, ErrorT, IdT, DataT>>,

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<BackendT, ContextT, ErrorT, IdT, DataT> Freeze for Node<BackendT, ContextT, ErrorT, IdT, DataT>
where IdT: Freeze, DataT: Freeze,

§

impl<BackendT, ContextT, ErrorT, IdT, DataT> RefUnwindSafe for Node<BackendT, ContextT, ErrorT, IdT, DataT>
where IdT: RefUnwindSafe, DataT: RefUnwindSafe, BackendT: RefUnwindSafe, ContextT: RefUnwindSafe, ErrorT: RefUnwindSafe,

§

impl<BackendT, ContextT, ErrorT, IdT, DataT> Send for Node<BackendT, ContextT, ErrorT, IdT, DataT>
where IdT: Send, DataT: Send, BackendT: Send, ContextT: Send, ErrorT: Send,

§

impl<BackendT, ContextT, ErrorT, IdT, DataT> Sync for Node<BackendT, ContextT, ErrorT, IdT, DataT>
where IdT: Sync, DataT: Sync, BackendT: Sync, ContextT: Sync, ErrorT: Sync,

§

impl<BackendT, ContextT, ErrorT, IdT, DataT> Unpin for Node<BackendT, ContextT, ErrorT, IdT, DataT>
where IdT: Unpin, DataT: Unpin, BackendT: Unpin, ContextT: Unpin, ErrorT: Unpin,

§

impl<BackendT, ContextT, ErrorT, IdT, DataT> UnsafeUnpin for Node<BackendT, ContextT, ErrorT, IdT, DataT>
where IdT: UnsafeUnpin, DataT: UnsafeUnpin,

§

impl<BackendT, ContextT, ErrorT, IdT, DataT> UnwindSafe for Node<BackendT, ContextT, ErrorT, IdT, DataT>
where IdT: UnwindSafe, DataT: UnwindSafe, BackendT: UnwindSafe, ContextT: UnwindSafe, ErrorT: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> With for T

Source§

fn wrap_with<U, F>(self, f: F) -> U
where F: FnOnce(Self) -> U,

Calls the given closure and return the result. Read more
Source§

fn with<F>(self, f: F) -> Self
where F: FnOnce(&mut Self),

Calls the given closure on self.
Source§

fn try_with<E, F>(self, f: F) -> Result<Self, E>
where F: FnOnce(&mut Self) -> Result<(), E>,

Calls the given closure on self.
Source§

fn with_if<F>(self, condition: bool, f: F) -> Self
where F: FnOnce(&mut Self),

Calls the given closure if condition == true.