[][src]Function sise::write_from_tree

pub fn write_from_tree<W: Writer>(
    writer: &mut W,
    root_node: &Node
) -> Result<(), W::Error> where
    W::AtomOptions: Default + WriteFromTreeAtomOptions,
    W::BeginListOptions: Default,
    W::EndListOptions: Default

Write the tree of nodes root_node into writer.

Example

use sise::sise_expr;
use sise::Writer as _;

let tree = sise_expr!(["example", ["1", "2", "3"], ["a", "b", "c"]]);

let mut result = String::new();
let mut writer = sise::CompactStringWriter::new(&mut result);

sise::write_from_tree(&mut writer, &tree).unwrap();
writer.finish(&sise::VoidWriterOptions);

let expected_result = "(example (1 2 3) (a b c))";
assert_eq!(result, expected_result);

If you use SpacedStringWriter, atoms at the beginning of a list will be placed in the same line as the openning (:

use sise::sise_expr;
use sise::Writer as _;

let tree = sise_expr!(["example", ["1", "2", "3"], ["a", "b", "c"]]);

let style = sise::SpacedStringWriterStyle {
   line_break: "\n",
   indentation: " ",
};

let mut result = String::new();
let mut writer = sise::SpacedStringWriter::new(style, &mut result);

sise::write_from_tree(&mut writer, &tree).unwrap();
writer.finish(&sise::VoidWriterOptions);

let expected_result = "(example\n (1\n  2\n  3\n )\n (a\n  b\n  c\n )\n)";
assert_eq!(result, expected_result);

It does not consume the writer, so it can also be used to write a sub-tree:

use sise::sise_expr;
use sise::Writer as _;

let tree = sise_expr!(["1", "2", "3"]);

let mut result = String::new();
let mut writer = sise::CompactStringWriter::new(&mut result);

// Write the head
writer.begin_list(&sise::VoidWriterOptions).unwrap();
writer.write_atom("head", &sise::VoidWriterOptions).unwrap();

// Write the subtree
sise::write_from_tree(&mut writer, &tree).unwrap();

// Write the tail
writer.write_atom("tail", &sise::VoidWriterOptions).unwrap();
writer.end_list(&sise::VoidWriterOptions).unwrap();
writer.finish(&sise::VoidWriterOptions);

let expected_result = "(head (1 2 3) tail)";
assert_eq!(result, expected_result);