Skip to main content

Module smite

Module smite 

Source
Expand description

Interior Mutability Tuple-Struct with Enum. This tree implementation is an evolution of intmuttree that represents each type of node as variants in an enum, wrapped in a tuple struct.

§A tree structure for XDM

This module implements the Item module’s Node trait.

This implementation uses interior mutability to create and manage a tree structure that is both mutable and fully navigable.

To create a tree, use Node::new() to make a Document-type node. To add a node, first create it using a creation method, defined by the Node trait, such as new_element() or new_text(), then use the push(), insert_before(), or add_attribute() method to attach it to a node in the tree.

NB. The Item module’s Node trait is implemented for Rc<smite::Node>. For convenience, this is defined as the type RNode.

use std::rc::Rc;
use xrust::trees::smite::RNode;
use xrust::item::{Node as ItemNode, NodeType};
use qualname::{QName, NcName};
use xrust::value::Value;
use xrust::xdmerror::Error;

//pub(crate) type ExtDTDresolver = fn(Option<String>, String) -> Result<String, Error>;

// A document always has a NodeType::Document node as the toplevel node.
let mut doc = RNode::new_document();

// Create an element-type node. Upon creation, it is *not* attached to the tree.
let mut top = doc.new_element(
    QName::from_local_name(NcName::try_from("Top-Level").unwrap())
).expect("unable to create element node");

// Nodes are Rc-shared, so it is cheap to clone them.
// Now attach the element node to the tree.
// In this case, it is being attached to the document node, so it will become the root element.
doc.push(top.clone())
    .expect("unable to append child node");

// Now create a text node and attach it to the root element.
top.push(
    doc.new_text(Rc::new(Value::from("content of the element")))
        .expect("unable to create text node")
).expect("unable to append child node");

assert_eq!(doc.to_xml(), "<Top-Level>content of the element</Top-Level>")

Structs§

Ancestors
Attributes
Children
Descendants
NamespaceNodes
Node
Siblings

Functions§

dump_tree

Type Aliases§

RNode
A node in a tree.