1use anyhow::{Context, Result};
2use log::info;
3
4use crate::{cli::SchemaColumnsArgs, schema::Schema, table};
5
6pub fn execute(args: &SchemaColumnsArgs) -> Result<()> {
7 let schema = Schema::load(&args.schema)
8 .with_context(|| format!("Loading schema from {schema:?}", schema = args.schema))?;
9
10 if schema.columns.is_empty() {
11 info!("Schema {:?} does not define any columns", args.schema);
12 return Ok(());
13 }
14
15 let mut rows = Vec::with_capacity(schema.columns.len());
16 for (idx, column) in schema.columns.iter().enumerate() {
17 let position = (idx + 1).to_string();
18 let original_name = column.name.clone();
19 let datatype = column.datatype.to_string();
20 let output_name = column.output_name().to_string();
21 let rename = if output_name != column.name {
22 output_name
23 } else {
24 String::new()
25 };
26 rows.push(vec![position, original_name, datatype, rename]);
27 }
28
29 let headers = vec![
30 "#".to_string(),
31 "name".to_string(),
32 "type".to_string(),
33 "output".to_string(),
34 ];
35 table::print_table(&headers, &rows);
36 info!(
37 "Listed {} column(s) from {:?}",
38 schema.columns.len(),
39 args.schema
40 );
41 Ok(())
42}