From 970a165d680cad70cd9b5bcb2d46b6325d0a1a88 Mon Sep 17 00:00:00 2001 From: InAnYan Date: Mon, 18 Nov 2024 10:33:19 +0200 Subject: [PATCH] Improve commands --- module/move/assistant/src/actions/openai.rs | 26 +++++++++++- .../src/actions/openai_assistants_list.rs | 4 +- .../src/actions/openai_files_list.rs | 4 +- .../assistant/src/actions/openai_runs_list.rs | 4 +- module/move/assistant/src/commands.rs | 41 ++++++++----------- .../move/assistant/src/util/display_table.rs | 35 ++++++++-------- 6 files changed, 67 insertions(+), 47 deletions(-) diff --git a/module/move/assistant/src/actions/openai.rs b/module/move/assistant/src/actions/openai.rs index 82053c4d47..da8be32030 100644 --- a/module/move/assistant/src/actions/openai.rs +++ b/module/move/assistant/src/actions/openai.rs @@ -13,6 +13,8 @@ mod private use crate::*; use ser::DisplayFromStr; + use commands::TableConfig; + /// Collective enum for errors in OpenAI actions. #[ ser::serde_as ] #[ derive( Debug, Error, AsRefStr, ser::Serialize ) ] @@ -26,12 +28,31 @@ mod private #[ from ] #[ serde_as( as = "DisplayFromStr" ) ] openai_api_rs::v1::error::APIError - ) + ), + + /// User chosen a mix of table styles instead of a single one. + /// E.g.: both `--as-table` and `--as-records` were set, however only one style must be chosen + #[ error( "Select only one table style: either `--as-table`, `--as-records`, or `--columns`" ) ] + WrongTableStyle, } /// Shorthand for `Result` in OpenAI actions. pub type Result< T > = core::result::Result< T, Error >; + /// Check the CLI arguments for table style. + /// There are 3 arguments: `--as-table`, `--as-records`, `--columns`. Only one argument + /// should be active a time. + pub fn check_table_style( table_config: &TableConfig ) -> Result< () > + { + if table_config.as_table && ( table_config.as_records || table_config.columns ) + || table_config.as_records && ( table_config.as_table || table_config.columns ) + || table_config.columns && ( table_config.as_records || table_config.as_table ) + { + return Err( Error::WrongTableStyle ) + } + + Ok( () ) + } } crate::mod_interface! @@ -39,6 +60,7 @@ crate::mod_interface! own use { Error, - Result + Result, + check_table_style, }; } \ No newline at end of file diff --git a/module/move/assistant/src/actions/openai_assistants_list.rs b/module/move/assistant/src/actions/openai_assistants_list.rs index 3eabffec6b..2326ac2a6a 100644 --- a/module/move/assistant/src/actions/openai_assistants_list.rs +++ b/module/move/assistant/src/actions/openai_assistants_list.rs @@ -14,7 +14,7 @@ mod private use debug::AssistantObjectWrap; - use actions::openai::Result; + use actions::openai::{ Result, check_table_style }; use commands::TableConfig; use util::display_table::display_tabular_data; @@ -49,6 +49,8 @@ mod private table_config : TableConfig, ) -> Result < ListReport > { + check_table_style( &table_config )?; + let response = client.list_assistant( None, None, None, None ).await?; let assistants = response.data.into_iter().map( AssistantObjectWrap ).collect(); Ok( ListReport { table_config, assistants } ) diff --git a/module/move/assistant/src/actions/openai_files_list.rs b/module/move/assistant/src/actions/openai_files_list.rs index ec2c713b92..12f6ab7bd0 100644 --- a/module/move/assistant/src/actions/openai_files_list.rs +++ b/module/move/assistant/src/actions/openai_files_list.rs @@ -14,7 +14,7 @@ mod private use debug::FileDataWrap; - use actions::openai::Result; + use actions::openai::{ Result, check_table_style }; use commands::TableConfig; use util::display_table::display_tabular_data; @@ -49,6 +49,8 @@ mod private table_config : TableConfig, ) -> Result < ListReport > { + check_table_style( &table_config )?; + let response = client.file_list().await?; let files = response.data.into_iter().map( FileDataWrap ).collect(); Ok( ListReport { table_config, files } ) diff --git a/module/move/assistant/src/actions/openai_runs_list.rs b/module/move/assistant/src/actions/openai_runs_list.rs index ee4c0ffc3e..d5bf8098c6 100644 --- a/module/move/assistant/src/actions/openai_runs_list.rs +++ b/module/move/assistant/src/actions/openai_runs_list.rs @@ -14,7 +14,7 @@ mod private use debug::RunObjectWrap; - use actions::openai::Result; + use actions::openai::{ Result, check_table_style }; use commands::TableConfig; use util::display_table::display_tabular_data; @@ -50,6 +50,8 @@ mod private table_config : TableConfig, ) -> Result < ListReport > { + check_table_style( &table_config )?; + let response = client.list_run( thread_id, None, None, None, None ).await?; let runs = response.data.into_iter().map( RunObjectWrap ).collect(); Ok( ListReport { table_config, runs } ) diff --git a/module/move/assistant/src/commands.rs b/module/move/assistant/src/commands.rs index ae2615ad9a..7f54ec320b 100644 --- a/module/move/assistant/src/commands.rs +++ b/module/move/assistant/src/commands.rs @@ -6,9 +6,7 @@ mod private { - use clap::{ Parser, Subcommand, ValueEnum }; - - use derive_tools::Display; + use clap::{ Parser, Subcommand }; use crate::*; use commands::openai; @@ -31,39 +29,35 @@ mod private OpenAi( openai::Command ), } - const DEFAULT_MAX_TABLE_WIDTH: usize = 130; + // const DEFAULT_MAX_TABLE_WIDTH: usize = 130; + // Commented out as not yet implemented in `format_tools`. /// Common collection of arguments for formatting tabular data. #[ derive( Debug, Parser ) ] pub struct TableConfig { /// Limit table widht. - #[ arg( long, default_value_t = DEFAULT_MAX_TABLE_WIDTH ) ] - pub max_table_width : usize, + // #[ arg( long, default_value_t = DEFAULT_MAX_TABLE_WIDTH ) ] + // pub max_table_width : usize, + // Commented out as not yet implemented in `format_tools`. + + /// Show tabular data as an ordinary table. + #[ arg( long ) ] + pub as_table : bool, - /// Table style option. - #[ arg( long, default_value = "table" ) ] - pub style : TableStyle, + /// Show each record of a tabular data as a separate table. + #[ arg( long ) ] + pub as_records : bool, + + /// Show only keys (columns) of tabular data. + #[ arg( long ) ] + pub columns : bool, /// Filter columns of tabular data. #[ arg( long, value_delimiter( ',' ) ) ] pub filter_columns : Vec< String >, } - /// Table style. - #[ derive( Debug, Clone, Display, ValueEnum ) ] - pub enum TableStyle - { - /// Show data as an ordinary table. - Table, - - /// Show entities as separate tables. - AsRecords, - - /// Show only columns of the tables, no records. - Columns - } - } crate::mod_interface! @@ -81,6 +75,5 @@ crate::mod_interface! Cli, CliCommand, TableConfig, - TableStyle, }; } diff --git a/module/move/assistant/src/util/display_table.rs b/module/move/assistant/src/util/display_table.rs index fb73835a3a..edced5fbdd 100644 --- a/module/move/assistant/src/util/display_table.rs +++ b/module/move/assistant/src/util/display_table.rs @@ -16,7 +16,7 @@ mod private }; use crate::*; - use commands::{ TableConfig, TableStyle }; + use commands::{ TableConfig }; /// Function for displaying tabular data according to `TableConfig`. pub fn display_tabular_data<'a> @@ -26,23 +26,22 @@ mod private table_config : &'a TableConfig, ) -> fmt::Result { - match table_config.style - { - TableStyle::Table => - { - display_table( data, f, &table_config.filter_columns ) - } - - TableStyle::AsRecords => - { - display_records( data, f, &table_config.filter_columns ) - } - - TableStyle::Columns => - { - display_columns( data, f, &table_config.filter_columns ) - } - } + if table_config.as_table + { + display_table( data, f, &table_config.filter_columns ) + } + else if table_config.as_records + { + display_records( data, f, &table_config.filter_columns ) + } + else if table_config.columns + { + display_columns( data, f, &table_config.filter_columns ) + } + else + { + display_table( data, f, &table_config.filter_columns ) + } } fn display_table<'a>