use crate::db_operations::DbOperations;
use crate::lambda::logging::Logger;
use crate::storage::DatabaseConfig;
use std::sync::Arc;
#[derive(Clone)]
pub enum LambdaStorage {
Config(DatabaseConfig),
DbOps(Arc<DbOperations>),
}
#[derive(Clone)]
pub enum LambdaLogging {
DynamoDb,
Stdout,
Custom(Arc<dyn Logger>),
NoOp,
}
impl std::fmt::Debug for LambdaLogging {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::DynamoDb => write!(f, "DynamoDb"),
Self::Stdout => write!(f, "Stdout"),
Self::Custom(_) => write!(f, "Custom(<logger>)"),
Self::NoOp => write!(f, "NoOp"),
}
}
}
#[derive(Clone)]
pub struct LambdaConfig {
pub storage: LambdaStorage,
pub logging: LambdaLogging,
pub schema_service_url: Option<String>,
pub ai_config: Option<AIConfig>,
}
impl std::fmt::Debug for LambdaConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LambdaConfig")
.field(
"storage",
&match &self.storage {
LambdaStorage::Config(cfg) => format!("Config({:?})", cfg),
LambdaStorage::DbOps(_) => "DbOps(<pre-created>)".to_string(),
},
)
.field("schema_service_url", &self.schema_service_url)
.field("ai_config", &self.ai_config)
.field("logging", &self.logging)
.finish()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AIProvider {
OpenRouter,
Ollama,
}
#[derive(Debug, Clone)]
pub struct AIConfig {
pub provider: AIProvider,
pub openrouter: Option<OpenRouterConfig>,
pub ollama: Option<OllamaConfig>,
pub timeout_seconds: u64,
pub max_retries: u32,
}
#[derive(Debug, Clone)]
pub struct OpenRouterConfig {
pub api_key: String,
pub model: String,
pub base_url: Option<String>,
}
#[derive(Debug, Clone)]
pub struct OllamaConfig {
pub base_url: String,
pub model: String,
}
impl LambdaConfig {
pub fn new(storage_config: DatabaseConfig, logging: LambdaLogging) -> Self {
Self {
storage: LambdaStorage::Config(storage_config),
logging,
schema_service_url: None,
ai_config: None,
}
}
pub fn with_db_ops(db_ops: Arc<DbOperations>, logging: LambdaLogging) -> Self {
Self {
storage: LambdaStorage::DbOps(db_ops),
logging,
schema_service_url: None,
ai_config: None,
}
}
pub fn with_storage_config(mut self, storage_config: DatabaseConfig) -> Self {
self.storage = LambdaStorage::Config(storage_config);
self
}
pub fn with_schema_service_url(mut self, url: String) -> Self {
self.schema_service_url = Some(url);
self
}
pub fn with_openrouter(mut self, api_key: String, model: String) -> Self {
self.ai_config = Some(AIConfig {
provider: AIProvider::OpenRouter,
openrouter: Some(OpenRouterConfig {
api_key,
model,
base_url: None,
}),
ollama: None,
timeout_seconds: 120,
max_retries: 3,
});
self
}
pub fn with_ollama(mut self, base_url: String, model: String) -> Self {
self.ai_config = Some(AIConfig {
provider: AIProvider::Ollama,
openrouter: None,
ollama: Some(OllamaConfig { base_url, model }),
timeout_seconds: 120,
max_retries: 3,
});
self
}
pub fn with_ai_config(mut self, config: AIConfig) -> Self {
self.ai_config = Some(config);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::storage::DatabaseConfig;
use std::path::PathBuf;
#[test]
fn test_lambda_config_creation() {
let storage_config = DatabaseConfig::Local {
path: PathBuf::from("/tmp/folddb"),
};
let config = LambdaConfig::new(storage_config, LambdaLogging::Stdout);
assert!(config.schema_service_url.is_none());
}
#[test]
fn test_lambda_config_with_storage_config() {
let storage_config1 = DatabaseConfig::Local {
path: PathBuf::from("/tmp/test1"),
};
let storage_config2 = DatabaseConfig::Local {
path: PathBuf::from("/tmp/test2"),
};
let config = LambdaConfig::new(storage_config1.clone(), LambdaLogging::Stdout)
.with_storage_config(storage_config2.clone());
match &config.storage {
LambdaStorage::Config(DatabaseConfig::Local { path }) => {
assert_eq!(path, &PathBuf::from("/tmp/test2"));
}
_ => panic!("Expected Local storage config"),
}
}
#[test]
fn test_lambda_config_with_schema_service_url() {
let storage_config = DatabaseConfig::Local {
path: PathBuf::from("/tmp/folddb"),
};
let url = "https://schema.example.com".to_string();
let config = LambdaConfig::new(storage_config, LambdaLogging::Stdout)
.with_schema_service_url(url.clone());
assert_eq!(config.schema_service_url, Some(url));
}
}