Add write_tree method (#925)
* Add write_tree method
* Fix feature when std is disabled
be975378e34b7c7892b983971819999809e713b8 · ruwaq Elchanan Haas · (7 killa ñawpaq) · tayta 6d8a3832e · kay commitpi sach'ata qhaway
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 34c105793..478aaad8f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog
+## 0.9.3
+
+### Added
+
+- Added write_tree method to utils.
+
## 0.9.2
### Fixed
diff --git a/src/util/mod.rs b/src/util/mod.rs
index 4f829d60c..00f3a8fe0 100644
--- a/src/util/mod.rs
+++ b/src/util/mod.rs
@@ -14,6 +14,8 @@ pub(crate) mod debug;
mod print;
#[cfg(feature = "std")]
pub use print::print_tree;
+#[cfg(feature = "std")]
+pub use print::write_tree;
/// Deserialize a type `S` by deserializing a string, then using the `FromStr`
/// impl of `S` to create the result. The generic type `S` is not required to
diff --git a/src/util/print.rs b/src/util/print.rs
index 4d2fa3961..0a1bbb5ba 100644
--- a/src/util/print.rs
+++ b/src/util/print.rs
@@ -1,20 +1,34 @@
-//! Contains the print_tree function for printing a debug representation of the tree
+//! Contains functions for printing a debug representation of the tree
use crate::tree::{NodeId, PrintTree};
+use std::io;
/// Prints a debug representation of the computed layout for a tree of nodes, starting with the passed root node.
pub fn print_tree(tree: &impl PrintTree, root: NodeId) {
- println!("TREE");
- print_node(tree, root, false, String::new());
+ // Rust code generally makes the assumption that writes to Stdout won't fail, so paincing on error is fine here.
+ write_tree(io::stdout().lock(), tree, root).expect("Wrote tree debug representation to Stdout");
+}
+
+/// Writes a debug representation of the computed layout to the writer, starting with the passed root node.
+pub fn write_tree(mut writer: impl io::Write, tree: &impl PrintTree, root: NodeId) -> io::Result<()> {
+ writeln!(writer, "TREE")?;
+ write_node(&mut writer, tree, root, false, String::new())?;
- /// Recursive function that prints each node in the tree
- fn print_node(tree: &impl PrintTree, node_id: NodeId, has_sibling: bool, lines_string: String) {
+ /// Recursive function that writes each node in the tree
+ fn write_node(
+ writer: &mut impl io::Write,
+ tree: &impl PrintTree,
+ node_id: NodeId,
+ has_sibling: bool,
+ lines_string: String,
+ ) -> io::Result<()> {
let layout = &tree.get_final_layout(node_id);
let display = tree.get_debug_label(node_id);
let num_children = tree.child_count(node_id);
let fork_string = if has_sibling { "├── " } else { "└── " };
#[cfg(feature = "content_size")]
- println!(
+ writeln!(
+ writer,
"{lines}{fork} {display} [x: {x:<4} y: {y:<4} w: {width:<4} h: {height:<4} content_w: {content_width:<4} content_h: {content_height:<4} border: l:{bl} r:{br} t:{bt} b:{bb}, padding: l:{pl} r:{pr} t:{pt} b:{pb}] ({key:?})",
lines = lines_string,
fork = fork_string,
@@ -34,9 +48,10 @@ pub fn print_tree(tree: &impl PrintTree, root: NodeId) {
pt = layout.padding.top,
pb = layout.padding.bottom,
key = node_id,
- );
+ )?;
#[cfg(not(feature = "content_size"))]
- println!(
+ writeln!(
+ writer,
"{lines}{fork} {display} [x: {x:<4} y: {y:<4} width: {width:<4} height: {height:<4}] ({key:?})",
lines = lines_string,
fork = fork_string,
@@ -53,7 +68,9 @@ pub fn print_tree(tree: &impl PrintTree, root: NodeId) {
// Recurse into children
for (index, child) in tree.child_ids(node_id).enumerate() {
let has_sibling = index < num_children - 1;
- print_node(tree, child, has_sibling, new_string.clone());
+ write_node(writer, tree, child, has_sibling, new_string.clone())?;
}
+ Ok(())
}
+ Ok(())
}