vortex_tui/
tree.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Print tree views of Vortex files.
5
6use std::path::Path;
7use std::path::PathBuf;
8
9use vortex::array::stream::ArrayStreamExt;
10use vortex::error::VortexResult;
11use vortex::file::OpenOptionsSessionExt;
12use vortex::session::VortexSession;
13
14/// Command-line arguments for the tree command.
15#[derive(Debug, clap::Parser)]
16pub struct TreeArgs {
17    /// Which kind of tree to display.
18    #[clap(subcommand)]
19    pub mode: TreeMode,
20}
21
22/// What kind of tree to display.
23#[derive(Debug, clap::Subcommand)]
24pub enum TreeMode {
25    /// Display the array encoding tree (loads and materializes arrays)
26    Array {
27        /// Path to the Vortex file
28        file: PathBuf,
29    },
30    /// Display the layout tree structure (metadata only, no array loading)
31    Layout {
32        /// Path to the Vortex file
33        file: PathBuf,
34        /// Show additional metadata information including buffer sizes (requires fetching segments)
35        #[arg(short, long)]
36        verbose: bool,
37    },
38}
39
40/// Print tree views of a Vortex file (layout tree or array tree).
41///
42/// # Errors
43///
44/// Returns an error if the file cannot be opened or read.
45pub async fn exec_tree(session: &VortexSession, args: TreeArgs) -> VortexResult<()> {
46    match args.mode {
47        TreeMode::Array { file } => exec_array_tree(session, &file).await?,
48        TreeMode::Layout { file, verbose } => exec_layout_tree(session, &file, verbose).await?,
49    }
50
51    Ok(())
52}
53
54async fn exec_array_tree(session: &VortexSession, file: &Path) -> VortexResult<()> {
55    let full = session
56        .open_options()
57        .open(file)
58        .await?
59        .scan()?
60        .into_array_stream()?
61        .read_all()
62        .await?;
63
64    println!("{}", full.display_tree());
65
66    Ok(())
67}
68
69async fn exec_layout_tree(session: &VortexSession, file: &Path, verbose: bool) -> VortexResult<()> {
70    let vxf = session.open_options().open(file).await?;
71
72    if verbose {
73        // In verbose mode, fetch segments to display buffer sizes.
74        let output = vxf
75            .footer()
76            .layout()
77            .display_tree_with_segments(vxf.segment_source())
78            .await?;
79        println!("{}", output);
80    } else {
81        // In non-verbose mode, just display layout tree without fetching segments.
82        println!("{}", vxf.footer().layout().display_tree());
83    }
84
85    Ok(())
86}