opencrates 3.0.1

Enterprise-grade AI-powered Rust development companion with comprehensive automation, monitoring, and deployment capabilities
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::path::Path;

/// Represents the main configuration for the OpenCrates application.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
    /// Server configuration settings.
    pub server: ServerConfig,
    /// Database connection and pool settings.
    pub database: DatabaseConfig,
    /// Artificial intelligence provider settings.
    pub ai: AiConfig,
    /// Crate registry settings.
    pub registry: RegistryConfig,
    /// Logging level and format settings.
    pub logging: LoggingConfig,
}

/// Holds the server's host, port, and worker configurations.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
    /// The host address the server will bind to.
    pub host: String,
    /// The port the server will listen on.
    pub port: u16,
    /// The number of worker threads for the server.
    pub workers: usize,
}

/// Contains the database connection URL and pool size.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseConfig {
    /// The URL for connecting to the database.
    pub url: String,
    /// The maximum number of connections in the pool.
    pub max_connections: u32,
}

/// Configures the AI provider, including API keys and model selection.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AiConfig {
    /// The API key for the OpenAI provider.
    pub openai_api_key: Option<String>,
    /// The API key for the Anthropic provider.
    pub anthropic_api_key: Option<String>,
    /// The default AI model to use for generation.
    pub default_model: String,
    /// The maximum number of tokens to generate.
    pub max_tokens: usize,
    /// The temperature for AI model sampling.
    pub temperature: f32,
}

/// Specifies the crate registry URL and cache Time-to-Live (TTL).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegistryConfig {
    /// The URL of the crate registry.
    pub url: String,
    /// The Time-to-Live for the registry cache in seconds.
    pub cache_ttl: u64,
}

/// Defines the logging level and output format.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggingConfig {
    /// The verbosity level of the logs (e.g., "info", "debug").
    pub level: String,
    /// The format for log output (e.g., "json", "text").
    pub format: String,
}

impl Config {
    /// Loads the configuration from a TOML file at the given path.
    ///
    /// # Arguments
    ///
    /// * `path` - A path to the configuration file.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be read or parsed.
    pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
        let content = std::fs::read_to_string(path)?;
        let config: Self = toml::from_str(&content)?;
        Ok(config)
    }

    /// Provides a default configuration for the application.
    ///
    /// This is useful for development and testing, or as a fallback.
    pub fn default() -> Self {
        Self {
            server: ServerConfig {
                host: "127.0.0.1".to_string(),
                port: 8080,
                workers: num_cpus::get(),
            },
            database: DatabaseConfig {
                url: "sqlite:opencrates.db".to_string(),
                max_connections: 10,
            },
            ai: AiConfig {
                openai_api_key: std::env::var("OPENAI_API_KEY").ok(),
                anthropic_api_key: std::env::var("ANTHROPIC_API_KEY").ok(),
                default_model: "gpt-4".to_string(),
                max_tokens: 4096,
                temperature: 0.7,
            },
            registry: RegistryConfig {
                url: "https://crates.io".to_string(),
                cache_ttl: 3600,
            },
            logging: LoggingConfig {
                level: "info".to_string(),
                format: "json".to_string(),
            },
        }
    }
}