vortex_array/operator/
display.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::fmt;
5use std::fmt::{Display, Formatter};
6
7use itertools::Itertools;
8
9use crate::operator::Operator;
10
11impl dyn Operator + '_ {
12    pub fn display_tree(&self) -> impl Display {
13        DisplayTreeExpr(self)
14    }
15}
16
17pub enum DisplayFormat {
18    Compact,
19    Tree,
20}
21
22// TODO(ngates): this is pretty bad right now, and pipelined operators display poorly.
23struct DisplayTreeExpr<'a>(&'a dyn Operator);
24
25impl Display for DisplayTreeExpr<'_> {
26    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
27        pub use termtree::Tree;
28
29        fn make_tree(expr: &dyn Operator) -> Result<Tree<String>, fmt::Error> {
30            let node_name = TreeNodeDisplay(expr).to_string();
31            let child_trees: Vec<_> = expr
32                .children()
33                .iter()
34                .map(|child| make_tree(child.as_ref()))
35                .try_collect()?;
36            Ok(Tree::new(node_name).with_leaves(child_trees))
37        }
38
39        write!(f, "{}", make_tree(self.0)?)
40    }
41}
42
43struct TreeNodeDisplay<'a>(&'a dyn Operator);
44
45impl<'a> Display for TreeNodeDisplay<'a> {
46    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
47        self.0.fmt_as(DisplayFormat::Tree, f)
48    }
49}