gemini_crate 0.1.0

A robust Rust client library for Google's Gemini AI API with built-in error handling, retry logic, and comprehensive model support
Documentation
//! Simple chat example using the Gemini crate
//!
//! This example demonstrates how to create a basic interactive chat application
//! using the gemini_crate library.
//!
//! Usage:
//! 1. Set your GEMINI_API_KEY in a .env file
//! 2. Run: cargo run --example simple_chat
//! 3. Type messages and press Enter to chat with Gemini
//! 4. Type 'quit' or 'exit' to end the conversation

use gemini_crate::{client::GeminiClient, errors::Error};
use std::io::{self, Write};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load environment variables from .env file
    dotenvy::dotenv().ok();

    // Create the Gemini client
    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(());
        }
    };

    // Print welcome message
    println!("🤖 Gemini Chat");
    println!("===============");
    println!("Start chatting with Gemini AI! Type 'quit' or 'exit' to end the conversation.\n");

    // Main chat loop
    loop {
        // Get user input
        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();

        // Check for exit commands
        if input.is_empty() {
            continue;
        }

        if input.to_lowercase() == "quit" || input.to_lowercase() == "exit" {
            println!("👋 Goodbye!");
            break;
        }

        // Show thinking indicator
        print!("🤖 Gemini: ");
        io::stdout().flush()?;

        // Get response from Gemini
        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(())
}