Skip to content

Commit

Permalink
refactor: introducing the trait Cluster for the --cluster argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Mcdostone committed Feb 10, 2025
1 parent b999418 commit 6b05802
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion crates/command/examples/my_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use strum::{Display, EnumIter, EnumString};
use tui::TuiError;
use yozefu_command::Cli;

#[derive(Debug, Clone, PartialEq, Eq, Display, EnumString, EnumIter)]
#[derive(Debug, Clone, PartialEq, Eq, Display, EnumString, EnumIter, Default)]
#[strum(serialize_all = "lowercase")]
enum Cluster {
#[default]
Localhost,
Test,
Development,
Expand Down
7 changes: 4 additions & 3 deletions crates/command/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
//! The command line argument Parser struct
use crate::cluster::Cluster;
use crate::command::{Command, MainCommand, UtilityCommands};
use crate::theme::init_themes_file;
use app::configuration::{ClusterConfig, GlobalConfig, SchemaRegistryConfig, YozefuConfig};
use app::APPLICATION_NAME;
use clap::command;
use lib::Error;
use reqwest::Url;
use std::fmt::Debug;
use std::fs;
use std::{fmt::Display, path::PathBuf, str::FromStr};
use tui::error::TuiError;

pub use clap::Parser;
use indexmap::IndexMap;

// https://github.com/clap-rs/clap/issues/975
/// CLI parser
#[derive(Parser)]
#[command(author, version, about = "A terminal user interface to navigate Kafka topics and search for Kafka records.", name = APPLICATION_NAME, bin_name = APPLICATION_NAME, display_name = APPLICATION_NAME, long_about = None, propagate_version = true, args_conflicts_with_subcommands = true)]
pub struct Cli<T>
where
T: Display + Debug + Clone + Sync + Send + 'static + FromStr,
T: Cluster,
<T as FromStr>::Err: Display,
{
#[command(subcommand)]
Expand All @@ -32,7 +33,7 @@ where

impl<T> Cli<T>
where
T: Display + Debug + Clone + Sync + Send + 'static + FromStr,
T: Cluster,
<T as FromStr>::Err: Display,
{
/// Executes the CLI.
Expand Down
6 changes: 6 additions & 0 deletions crates/command/src/cluster.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use std::{fmt::Debug, fmt::Display, str::FromStr};

/// Every cluster identifier must implement this trait to be used in the command line.
pub trait Cluster: Debug + Clone + Sync + Send + 'static + FromStr + Default + Display {}

impl<T> Cluster for T where T: Debug + Clone + Sync + Send + 'static + FromStr + Default + Display {}
6 changes: 3 additions & 3 deletions crates/command/src/command/main_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ where
#[command(author, version, about, long_about = None, propagate_version = true)]
pub struct MainCommand<T>
where
T: Display + Clone + Sync + Send + 'static + FromStr,
T: Display + Clone + Sync + Send + 'static + FromStr + Default,
<T as FromStr>::Err: Display,
{
#[clap(short, long)]
/// Log level set to 'debug'
pub debug: bool,
#[clap(short = 'c', short_alias='e', alias="environment", long, value_parser = parse_cluster::<T>)]
#[clap(short = 'c', short_alias='e', alias="environment", long, value_parser = parse_cluster::<T>, default_value_t)]
/// The cluster to use
cluster: T,
/// Topics to consume
Expand Down Expand Up @@ -106,7 +106,7 @@ pub enum KafkaFormatterOption {

impl<T> MainCommand<T>
where
T: Display + Clone + Sync + Send + 'static + FromStr,
T: Display + Clone + Sync + Send + 'static + FromStr + Default,
<T as FromStr>::Err: Display,
{
pub async fn execute(self, mut yozefu_config: YozefuConfig) -> Result<(), TuiError> {
Expand Down
2 changes: 2 additions & 0 deletions crates/command/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
//! It relies on the [`clap`](https://crates.io/crates/clap) crate.
mod cli;
mod cluster;
mod command;
mod headless;
mod log;
mod theme;
use app::configuration::GlobalConfig;
pub use clap::Parser;
pub use cli::Cli;
pub use cluster::Cluster;
use lib::Error;
pub use tui::TuiError;

Expand Down

0 comments on commit 6b05802

Please sign in to comment.