use clap::{Parser, Subcommand};
use commands::explain::GenerateContent;
use std::path::PathBuf;
mod gemini;
mod commands;
mod error;
mod tts;
use error::{Result, GmError};
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
#[arg(long)]
tts: bool,
#[arg(long, default_value = "1.0")]
tts_rate: f32,
#[arg(long, default_value = "1.0")]
tts_volume: f32,
}
#[derive(Subcommand)]
enum Commands {
Explain {
command: String,
},
Summarize {
file: Option<PathBuf>,
#[arg(long)]
stdin: bool,
},
Translate {
text: String,
#[arg(long)]
to: String,
},
Define {
term: String,
#[arg(long)]
context: Option<String>,
},
Brainstorm {
topic: String,
},
Rephrase {
text: String,
#[arg(long)]
style: Option<String>,
},
FixTypos {
text: String,
},
GenerateAlias {
description: String,
},
AnalyzeSentiment {
text: String,
#[arg(long)]
stdin: bool,
},
ExplainCode {
file: PathBuf,
#[arg(long)]
language: Option<String>,
},
ExplainError {
error: String,
#[arg(long)]
stdin: bool,
},
GenerateRegex {
pattern: String,
},
ConvertFormat {
file: Option<PathBuf>,
#[arg(long)]
stdin: bool,
#[arg(long)]
from: String,
#[arg(long)]
to: String,
},
ExplainConcept {
concept: String,
#[arg(long)]
field: Option<String>,
},
FindCommand {
task: String,
},
GenerateBoilerplate {
description: String,
},
ReviewText {
text: String,
#[arg(long)]
stdin: bool,
#[arg(long)]
criteria: Option<String>,
},
SuggestName {
description: String,
},
ExplainAlgorithm {
algorithm: String,
},
GenerateTest {
code: String,
#[arg(long)]
language: Option<String>,
},
ListDir {
path: Option<PathBuf>,
},
}
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
let cli = Cli::parse();
match &cli.command {
Commands::Summarize { file, stdin } => {
if !*stdin && file.is_none() {
return Err(GmError::InvalidInput("Either --stdin or a file path must be provided".to_string()));
}
}
_ => {}
}
let gemini_client = gemini::RealGeminiClient::new()?;
let tts_manager = if cli.tts {
let tts = tts::TTSManager::new()?;
tts.set_rate(cli.tts_rate).await?;
tts.set_volume(cli.tts_volume).await?;
Some(tts)
} else {
None
};
match cli.command {
Commands::Explain { command } => commands::explain::execute(&gemini_client, &command, tts_manager.as_ref()).await,
Commands::Summarize { file, stdin } => commands::summarize::execute(&gemini_client, file, stdin, tts_manager.as_ref()).await,
Commands::Translate { text, to } => commands::translate::execute(gemini_client, text, to, tts_manager.as_ref()).await,
Commands::Define { term, context } => commands::define::execute(gemini_client, term, context, tts_manager.as_ref()).await,
Commands::Brainstorm { topic } => commands::brainstorm::execute(gemini_client, topic, tts_manager.as_ref()).await,
Commands::Rephrase { text, style: _ } => commands::rephrase::execute(gemini_client, text, tts_manager.as_ref()).await,
Commands::FixTypos { text } => commands::fix_typos::execute(gemini_client, text, tts_manager.as_ref()).await,
Commands::GenerateAlias { description } => commands::generate_alias::execute(gemini_client, description, tts_manager.as_ref()).await,
Commands::AnalyzeSentiment { text, stdin } => commands::analyze_sentiment::execute(gemini_client, text, stdin, tts_manager.as_ref()).await,
Commands::ExplainCode { file, language } => commands::explain_code::execute(gemini_client, file, language, tts_manager.as_ref()).await,
Commands::ExplainError { error, stdin } => commands::explain_error::execute(gemini_client, error, stdin, tts_manager.as_ref()).await,
Commands::GenerateRegex { pattern } => commands::generate_regex::execute(gemini_client, pattern, tts_manager.as_ref()).await,
Commands::ConvertFormat { file, stdin, from, to } => commands::convert_format::execute(gemini_client, file, stdin, from, to, tts_manager.as_ref()).await,
Commands::ExplainConcept { concept, field } => commands::explain_concept::execute(gemini_client, concept, field, tts_manager.as_ref()).await,
Commands::FindCommand { task } => commands::find_command::execute(gemini_client, task, tts_manager.as_ref()).await,
Commands::GenerateBoilerplate { description } => commands::generate_boilerplate::execute(gemini_client, description, tts_manager.as_ref()).await,
Commands::ReviewText { text, stdin, criteria } => commands::review_text::execute(gemini_client, text, stdin, criteria, tts_manager.as_ref()).await,
Commands::SuggestName { description } => commands::suggest_name::execute(gemini_client, description, tts_manager.as_ref()).await,
Commands::ExplainAlgorithm { algorithm } => commands::explain_algorithm::execute(gemini_client, algorithm, tts_manager.as_ref()).await,
Commands::GenerateTest { code, language } => commands::generate_test::execute(gemini_client, code, language, tts_manager.as_ref()).await,
Commands::ListDir { path } => commands::list_dir::execute(gemini_client, path, tts_manager.as_ref()).await,
}
}