Skip to content

Commit

Permalink
Improve commands
Browse files Browse the repository at this point in the history
  • Loading branch information
InAnYan committed Nov 18, 2024
1 parent aaa50ff commit 970a165
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 47 deletions.
26 changes: 24 additions & 2 deletions module/move/assistant/src/actions/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ]
Expand All @@ -26,19 +28,39 @@ 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!
{
own use
{
Error,
Result
Result,
check_table_style,
};
}
4 changes: 3 additions & 1 deletion module/move/assistant/src/actions/openai_assistants_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 } )
Expand Down
4 changes: 3 additions & 1 deletion module/move/assistant/src/actions/openai_files_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 } )
Expand Down
4 changes: 3 additions & 1 deletion module/move/assistant/src/actions/openai_runs_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 } )
Expand Down
41 changes: 17 additions & 24 deletions module/move/assistant/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
mod private
{

use clap::{ Parser, Subcommand, ValueEnum };

use derive_tools::Display;
use clap::{ Parser, Subcommand };

use crate::*;
use commands::openai;
Expand All @@ -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!
Expand All @@ -81,6 +75,5 @@ crate::mod_interface!
Cli,
CliCommand,
TableConfig,
TableStyle,
};
}
35 changes: 17 additions & 18 deletions module/move/assistant/src/util/display_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down

0 comments on commit 970a165

Please sign in to comment.