Struct convo::Tree [−][src]
Expand description
A Tree
is the parent container for a conversation tree. It is a walkable structure which follows the form of a human conversation.
Fields
nodes: IndexMap<String, Node>
Implementations
Try to returns a Tree
which is generated from parsing a string slice.
Arguments
source
- A string slice that holds valid YAML data to parse from. See also: example dialogue files.
Errors
- A
ImportError
will be returned if the source is not valid YAML data or if the tree is not considered legal when parsing. See also: validation rules.
Examples
use convo::Tree; let source = r#"--- root: start nodes: start: dialogue: I am a recursive node. links: - start: Recurse!"#; let tree = Tree::try_from(source).unwrap();
Try to export a Tree
to a file. The preferred file extension is *.convo.yml
.
Errors
- An
ExportError
will be returned if the file is unable to be saved or the tree is not considered legal to export. See also: validation rules.
Examples
use convo::{Tree, Node}; let mut tree = Tree::new(); let root_key = "root"; let root_node = Node::new(root_key, "The only node."); tree.nodes.insert(root_key.to_owned(), root_node); tree.set_root_key(root_key).unwrap(); assert!(tree.try_export("examples/dialogue_files/export.convo.yml").is_ok());
Returns an Option
which references a copy of the root Node
.
This method will return None
if the tree has no root set.
Examples
use convo::{Tree, Node}; let mut tree = Tree::new(); let root_og = Node::new("root", "The only node."); let root_copy = root_og.clone(); tree.nodes.insert("root".to_owned(), root_copy); tree.set_root_key("root").unwrap(); assert_eq!(&root_og, tree.root_node().unwrap());
Try to set the root node key for a Tree
. If Tree#current
is None
, this will automatically be dually initialized to the root key. If you want to set the root node without any validation checks, try set_root_key_unchecked
.
Arguments
node_key
- A string slice that holds a unique identifier which indexes aNode
in theTree#nodes
.
Errors
- A
TreeError
will be returned if the node does not exist in the node map.
Examples
use convo::{Tree, Node}; let mut tree = Tree::new(); let root_node = Node::new("root", "The only node."); tree.nodes.insert("root".to_owned(), root_node); tree.set_root_key("root").unwrap();
Set the root node key for a Tree
without validation checks. Unlike set_root_key
, this method will not incur side effects to Tree#current
in any way.
Arguments
node_key
- A string slice that holds a unique identifier which indexes aNode
in theTree#nodes
.
Examples
use convo::{Tree, Node}; let mut tree = Tree::new(); let root_node = Node::new("root", "The only node."); tree.nodes.insert("root".to_owned(), root_node); unsafe { tree.set_root_key_unchecked("root"); }
Returns an Option
which references a copy of the current Node
.
This method will return None
if the tree has no current set.
Examples
use convo::{Tree, Node}; let mut tree = Tree::new(); let root_og = Node::new("x", "Some node."); let root_copy = root_og.clone(); tree.nodes.insert("x".to_owned(), root_copy); tree.set_current_key("x").unwrap(); assert_eq!(&root_og, tree.current_node().unwrap());
Try to set the current node key for a Tree
. If you want to set the current node without any validation checks, try set_current_key_unchecked
.
Arguments
node_key
- A string slice that holds a unique identifier which indexes aNode
in theTree#nodes
.
Errors
- A
TreeError
will be returned if the node does not exist in the node map.
Examples
use convo::{Tree, Node}; let mut tree = Tree::new(); let current_node = Node::new("x", "Some node."); tree.nodes.insert("x".to_owned(), current_node); tree.set_current_key("x").unwrap();
Set the current node key for a Tree
without validation checks.
Arguments
node_key
- A string slice that holds a unique identifier which indexes aNode
in theTree#nodes
.
Examples
use convo::{Tree, Node}; let mut tree = Tree::new(); let current_node = Node::new("x", "Some node."); tree.nodes.insert("x".to_owned(), current_node); unsafe { tree.set_current_key_unchecked("x"); }
Try to rewind the current node key for a Tree
back to the root key by cloning the root key. If you want to rewind the current node without any validation checks, try rewind_unchecked
.
Errors
Examples
use convo::{Tree, Node}; let mut tree = Tree::new(); let root_node = Node::new("root", "The root."); let current_node = Node::new("x", "Some node."); tree.nodes.insert("root".to_owned(), root_node); tree.nodes.insert("x".to_owned(), current_node); tree.set_root_key("root").unwrap(); tree.set_current_key("x").unwrap(); tree.rewind().unwrap(); assert_eq!("root", tree.current_key().unwrap());
Rewind the current node key for a Tree
back to the root key by cloning the root key.
Examples
use convo::{Tree, Node}; let mut tree = Tree::new(); let current_node = Node::new("x", "Some node."); tree.nodes.insert("x".to_owned(), current_node); tree.set_current_key("x").unwrap(); unsafe { tree.rewind_unchecked(); } assert!(tree.current_key().is_none()); // Because the root was `None`.
Clear the entire tree.
Examples
use convo::{Tree, Node}; let mut tree = Tree::new(); let root_node = Node::new("root", "The root."); tree.nodes.insert("root".to_owned(), root_node); tree.set_root_key("root").unwrap(); tree.reset(); assert_eq!(0, tree.nodes.len()); assert!(tree.root_key().is_none()); assert!(tree.current_key().is_none());
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Tree
impl UnwindSafe for Tree
Blanket Implementations
Mutably borrows from an owned value. Read more
Compare self to key
and return true
if they are equal.