athena_cli/commands/database/utils/
display.rs

1use aws_sdk_athena::types::Column;
2use prettytable::Cell;
3
4/// Display struct for AWS Athena Column
5pub struct ColumnDisplay {
6    /// Column name
7    name: String,
8    /// Column type
9    column_type: String,
10    /// Column comment/description (may be empty for ColumnInfo)
11    comment: String,
12}
13
14impl From<&Column> for ColumnDisplay {
15    fn from(column: &Column) -> Self {
16        Self {
17            name: column.name().to_string(),
18            column_type: column.r#type().unwrap_or("").to_string(),
19            comment: column.comment().unwrap_or("").to_string(),
20        }
21    }
22}
23
24impl From<Column> for ColumnDisplay {
25    fn from(column: Column) -> Self {
26        Self::from(&column)
27    }
28}
29
30impl ColumnDisplay {
31    /// Convert the ColumnDisplay into a prettytable Row
32    pub fn to_row(&self) -> prettytable::Row {
33        prettytable::Row::new(vec![
34            Cell::new(&self.name),
35            Cell::new(&self.column_type),
36            Cell::new(&self.comment),
37        ])
38    }
39
40    /// Create a formatted table from a slice of Columns
41    pub fn create_columns_table(columns: &[Column]) -> prettytable::Table {
42        let mut table = prettytable::Table::new();
43
44        // Add header row with styling
45        table.add_row(header_row(&["Name", "Type", "Description"]));
46
47        // Add data rows
48        for column in columns {
49            let display = ColumnDisplay::from(column);
50            table.add_row(display.to_row());
51        }
52
53        table
54    }
55}
56
57/// Helper struct for parameter display
58pub struct ParameterDisplay {
59    /// Parameter name
60    name: String,
61    /// Parameter value
62    value: String,
63}
64
65impl ParameterDisplay {
66    /// Convert the ParameterDisplay into a prettytable Row
67    pub fn to_row(&self) -> prettytable::Row {
68        prettytable::Row::new(vec![Cell::new(&self.name), Cell::new(&self.value)])
69    }
70
71    /// Create a formatted table from a map of parameters
72    pub fn create_parameters_table(
73        parameters: &std::collections::HashMap<String, String>,
74        exclude_keys: &[&str],
75    ) -> prettytable::Table {
76        let mut table = prettytable::Table::new();
77
78        // Add header row with styling
79        table.add_row(header_row(&["Parameter", "Value"]));
80
81        // Add data rows
82        for (key, value) in parameters {
83            // Skip excluded keys
84            if exclude_keys.contains(&key.as_str()) {
85                continue;
86            }
87
88            let display = Self {
89                name: key.clone(),
90                value: value.clone(),
91            };
92            table.add_row(display.to_row());
93        }
94
95        table
96    }
97}
98
99/// Display struct for database information
100pub struct DatabaseDisplay {
101    /// Database name
102    name: String,
103    /// Database description
104    description: String,
105}
106
107impl DatabaseDisplay {
108    /// Create a new DatabaseDisplay from AWS SDK types
109    pub fn from_database(db: &aws_sdk_athena::types::Database) -> Self {
110        Self {
111            name: db.name().to_string(),
112            description: db.description().unwrap_or("").to_string(),
113        }
114    }
115
116    /// Convert the DatabaseDisplay into a prettytable Row
117    pub fn to_row(&self) -> prettytable::Row {
118        prettytable::Row::new(vec![Cell::new(&self.name), Cell::new(&self.description)])
119    }
120
121    /// Create a formatted table from a slice of Databases
122    pub fn create_databases_table(
123        databases: &[aws_sdk_athena::types::Database],
124    ) -> prettytable::Table {
125        let mut table = prettytable::Table::new();
126
127        // Add header row with styling using the helper function
128        table.add_row(header_row(&["Name", "Description"]));
129
130        // Add data rows
131        for db in databases {
132            let display = Self::from_database(db);
133            table.add_row(display.to_row());
134        }
135
136        table
137    }
138}
139
140/// Display struct for table metadata
141pub struct TableMetadataDisplay {
142    /// Table name
143    name: String,
144    /// Table type
145    table_type: String,
146    /// Column count
147    column_count: usize,
148}
149
150impl TableMetadataDisplay {
151    /// Create a new TableMetadataDisplay from AWS SDK types
152    pub fn from_table_metadata(table: &aws_sdk_athena::types::TableMetadata) -> Self {
153        Self {
154            name: table.name().to_string(),
155            table_type: table.table_type().unwrap_or("").to_string(),
156            column_count: table.columns().len(),
157        }
158    }
159
160    /// Convert the TableMetadataDisplay into a prettytable Row
161    pub fn to_row(&self) -> prettytable::Row {
162        prettytable::Row::new(vec![
163            Cell::new(&self.name),
164            Cell::new(&self.table_type),
165            Cell::new(&self.column_count.to_string()),
166        ])
167    }
168
169    /// Create a formatted table from a slice of TableMetadata
170    pub fn create_table_metadata_table(
171        tables: &[&aws_sdk_athena::types::TableMetadata],
172    ) -> prettytable::Table {
173        let mut table = prettytable::Table::new();
174
175        // Add header row
176        table.add_row(prettytable::Row::new(vec![
177            Cell::new("Name"),
178            Cell::new("Type"),
179            Cell::new("Columns"),
180        ]));
181
182        // Add data rows
183        for table_meta in tables {
184            let display = Self::from_table_metadata(table_meta);
185            table.add_row(display.to_row());
186        }
187
188        table
189    }
190}
191
192/// Helper function to create a styled header cell
193pub fn header_cell(text: &str) -> Cell {
194    Cell::new(text).style_spec("Fb")
195}
196
197/// Helper function to create a styled header row
198pub fn header_row(headers: &[&str]) -> prettytable::Row {
199    let cells = headers.iter().map(|&text| header_cell(text)).collect();
200    prettytable::Row::new(cells)
201}