use gemini_crate::{client::GeminiClient, errors::Error};
use std::io::{self, Write};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenvy::dotenv().ok();
let client = match GeminiClient::new() {
Ok(c) => c,
Err(Error::Config(msg)) => {
eprintln!("❌ Configuration error: {}", msg);
eprintln!("💡 Make sure to set GEMINI_API_KEY in your .env file");
eprintln!(" Example: GEMINI_API_KEY=your_api_key_here");
return Ok(());
}
Err(e) => {
eprintln!("❌ Failed to create Gemini client: {}", e);
return Ok(());
}
};
println!("🤖 Gemini Chat");
println!("===============");
println!("Start chatting with Gemini AI! Type 'quit' or 'exit' to end the conversation.\n");
loop {
print!("You: ");
io::stdout().flush()?;
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(_) => {}
Err(e) => {
eprintln!("❌ Failed to read input: {}", e);
continue;
}
}
let input = input.trim();
if input.is_empty() {
continue;
}
if input.to_lowercase() == "quit" || input.to_lowercase() == "exit" {
println!("👋 Goodbye!");
break;
}
print!("🤖 Gemini: ");
io::stdout().flush()?;
match client.generate_text("gemini-2.5-flash", input).await {
Ok(response) => {
println!("{}\n", response);
}
Err(Error::Network(e)) => {
eprintln!("🌐 Network error: {}", e);
eprintln!("💡 Check your internet connection and try again.\n");
}
Err(Error::Api(msg)) => {
eprintln!("🚫 API error: {}", msg);
eprintln!("💡 This might be a rate limit or model issue. Try again in a moment.\n");
}
Err(Error::Json(e)) => {
eprintln!("📋 Response parsing error: {}", e);
eprintln!("💡 The API response format might be unexpected.\n");
}
Err(e) => {
eprintln!("❌ Unexpected error: {}\n", e);
}
}
}
Ok(())
}