Function sise::serialize_tree
source · [−]pub fn serialize_tree(
serializer: &mut Serializer<'_, '_>,
root_node: &TreeNode,
break_line_at: usize
)
Expand description
Serializes a tree of nodes into serializer
.
Example
use sise::sise_tree;
let tree = sise_tree!(["example", ["1", "2", "3"], ["a", "b", "c"]]);
let style = sise::SerializerStyle {
line_break: "\n",
indentation: " ",
};
let mut result = String::new();
let mut serializer = sise::Serializer::new(style, &mut result);
sise::serialize_tree(&mut serializer, &tree, usize::MAX);
// Don't forget to finish the serializer
serializer.finish(false);
let expected_result = "(example (1 2 3) (a b c))";
assert_eq!(result, expected_result);
If you use multi-line style, atoms at the beginning of a list
will be placed in the same line as the openning (
:
use sise::sise_tree;
let tree = sise_tree!(["example", ["1", "2", "3"], ["a", "b", "c"]]);
let style = sise::SerializerStyle {
line_break: "\n",
indentation: " ",
};
let mut result = String::new();
let mut serializer = sise::Serializer::new(style, &mut result);
sise::serialize_tree(&mut serializer, &tree, 0);
// Don't forget to finish the serializer
serializer.finish(true);
let expected_result = "(example\n (1\n 2\n 3\n )\n (a\n b\n c\n )\n)\n";
assert_eq!(result, expected_result);
It does not consume the serializer, so it can also be used to serialize a sub-tree:
use sise::sise_tree;
let tree = sise_tree!(["1", "2", "3"]);
let style = sise::SerializerStyle {
line_break: "\n",
indentation: " ",
};
let mut result = String::new();
let mut serializer = sise::Serializer::new(style, &mut result);
// Serialize the head
serializer.begin_list(usize::MAX);
serializer.put_atom("head", usize::MAX);
// Serialize the subtree
sise::serialize_tree(&mut serializer, &tree, usize::MAX);
// Serialize the tail
serializer.put_atom("tail", usize::MAX);
serializer.end_list();
serializer.finish(false);
let expected_result = "(head (1 2 3) tail)";
assert_eq!(result, expected_result);