Struct convo::Tree[][src]

pub struct Tree {
    pub nodes: IndexMap<String, Node>,
    // some fields omitted
}
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>

The nodes in this conversation tree. Each Node is uniquely indexable by its Node#key.

Implementations

Returns a Tree with no nodes.

Examples

use convo::Tree;
let tree = Tree::new();

Try to returns a Tree which is generated from parsing a string slice.

Arguments

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#key. This method will return None if the tree has no root set.

Examples

use convo::{Tree, Node};
let mut tree = Tree::new();
unsafe { tree.set_root_key_unchecked("root"); }
assert_eq!("root", tree.root_key().unwrap());

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 a Node in the Tree#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 a Node in the Tree#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#key. This method will return None if the tree has no current set.

Examples

use convo::{Tree, Node};
let mut tree = Tree::new();
unsafe { tree.set_current_key_unchecked("x"); }
assert_eq!("x", tree.current_key().unwrap());

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 a Node in the Tree#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 a Node in the Tree#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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Compare self to key and return true if they are equal.

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into #41263)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.