Struct tref::Forest[][src]

pub struct Forest<T: NodeContent> {
    pub trees: HashMap<String, Tree<T>>,
    pub levels: Option<HashMap<String, Vec<TreeLevel>>>,
}
Expand description

Contains the interfaces to parse, generate, modify and serialize TREF models.

Fields

trees: HashMap<String, Tree<T>>

Hash map with all the trees contained in the Forest.

levels: Option<HashMap<String, Vec<TreeLevel>>>

Optional hash map that contains information about tree levels, to accelerate and simplify some iterators.

Implementations

Generic T is a struct conforming to NodeContent trait.

Parse a TREF file and generate a Forest (without levels).

Arguments

  • reader - Buffer reader where to obtsain the TREF file.

Return

  • A Result with either the Forest representing the parsed TREF or a String describing an error.

Examples

let buf_reader = ... // generate a std::io::BufReader
let forest: Result<Forest<SimpleNode>, String> = Forest::build(buf_reader);
match forest {
    Ok(forest) => {
        // Do whatever with the forest...
        // ...
    },
    Err(msg) => {
        println!("Error parsing TREF: {}", msg);
        // ...
    }
}

Parse a TREF file and generates a Forest with levels.

Arguments

  • reader - Buffer reader where to obtsain the TREF file.

Return

  • A Result with either the Forest representing the parsed TREF or a String describing an error.

Examples

build_levels is used like Forest::build().

Returns a tree model.

Arguments

  • tree_id - ID of the tree.

Return

  • An Option with the tree model.

Examples

if let Some(tree_model) = forest.tree("my_tree") {
    // ...
}
else {
    println!("Tree with ID 'my_tree' not found");
}

Genereta an empty forest.

Return

  • An empty Forest.

Examples

let mut forest: Forest<SimpleNode> = Forest::empty();

Add a new empty tree to the forest.

Arguments

  • tree_id - ID of the tree.

Examples

let mut forest: Forest<SimpleNode> = Forest::empty();
 
// Create new empty tree
forest.new_tree("my_tree")

Set the root node of a tree.

Arguments

  • tree_id - ID of the tree.
  • root_node_content - Content of the node.

Return

  • A Result with either the index of the node created or a String describing an error.

Examples

let mut forest: Forest<SimpleNode> = Forest::empty();
let tree_id = String::from("my_tree");
forest.new_tree(&tree_id);
 
// Set root node to tree
let _root = forest.set_root(&tree_id, "root_node").unwrap();

Create a child node and link it to its parent.

Arguments

  • tree_id - ID of the tree.
  • node_index - Parent node index.
  • node_content - Child node content.

Return

  • A Result with either the index of the node created or a String describing an error.

Examples

let mut forest: Forest<SimpleNode> = Forest::empty();
let tree_id = String::from("my_tree");
forest.new_tree(&tree_id);
let _root = forest.set_root(&tree_id, "root_node").unwrap();
 
// Add two children to root node
let _node_1 = forest.link_node(&tree_id, _root, "node_1").unwrap();
let _node_2 = forest.link_node(&tree_id, _root, "node_2").unwrap();

Unlink a child node.

Arguments

  • tree_id - ID of the tree.
  • node_index - Indedx of the node to unlink.

Return

  • A Result with either the index of the unlinked node or a String describing an error.

Examples

let mut forest: Forest<SimpleNode> = Forest::empty();
let tree_id = String::from("my_tree");
forest.new_tree(&tree_id);
let _root = forest.set_root(&tree_id, "root_node").unwrap();
let _node_1 = forest.link_node(&tree_id, _root, "node_1").unwrap();
let _node_2 = forest.link_node(&tree_id, _root, "node_2").unwrap();
 
// Unlink node_1 from root node
forest.unlink_node(&tree_id, _node_1).unwrap();

Find a node specifing the path from the root.

Arguments

  • tree_id - ID of the tree.
  • path - Vector containing the nodes of the path.

Return

  • An Option with the node index.

Examples

// Path of node: root_node -> child_2 -> child_2_1 -> child_2_1_1
let child_2_1_1 = forest.find_node("my_tree", vec!(String::from("root_node"), String::from("child_2"), String::from("child_2_1"), String::from("child_2_1_1"))).unwrap();

Convert a Forest structure into a TREF file.

Arguments

  • buf_writer - BufWriter where to write the TREF.

Return

  • A boolean, true if serialization was ok, false if not.

Examples

let f = File::create("./serialized.tref").expect("Unable to create file");
let mut buf_writer = BufWriter::new(f);
 
if !forest.serialize(&mut buf_writer) {
    println!("Failed serializing TREF");
}

Trait Implementations

Formats the value using the given formatter. Read more

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

Performs the conversion.

Performs the conversion.

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.