diff --git a/Cargo.toml b/Cargo.toml index 841578d..1047b2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,5 +37,6 @@ utoipa = { version = "4.1.0", features = ["axum_extras"] } utoipa-swagger-ui = { version = "5", features = ["axum"] } [dev-dependencies] -tokio = { version = "1.0", features = ["full", "test-util"] } axum-test = "14.0.0" +tempfile = "3.8.1" +tokio = { version = "1.0", features = ["full", "test-util"] } diff --git a/src/config.rs b/src/config.rs index 7d8e2ca..a6fd07a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,13 +5,39 @@ use std::path::PathBuf; use crate::llm::models::Models; +/// Configuration for the chat-flame-backend application. +/// +/// It includes settings for the server port, cache directory, and model information. #[derive(Debug, Deserialize, Clone, Default)] pub struct Config { + /// Port number on which the server will listen. pub port: u16, + + /// Optional path to the directory where cache files are stored. pub cache_dir: Option, + + /// Model to be used by the server. pub model: Models, } +/// Loads the application configuration from a YAML file. +/// +/// # Arguments +/// +/// * `file_path` - Path to the YAML configuration file. +/// +/// # Returns +/// +/// This function returns `Config` on success or an error if the file cannot be read +/// or the contents cannot be parsed into a `Config`. +/// +/// # Examples +/// +/// ``` +/// use chat_flame_backend::config::load_config; +/// let config = load_config("path/to/config.yml").unwrap_or_default(); +/// println!("Server will run on port: {}", config.port); +/// ``` pub fn load_config(file_path: &str) -> Result> { let mut file = File::open(file_path)?; let mut contents = String::new(); @@ -19,3 +45,27 @@ pub fn load_config(file_path: &str) -> Result let config: Config = serde_yaml::from_str(&contents)?; Ok(config) } + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Write; + use tempfile::NamedTempFile; + + #[test] + fn test_load_config() { + let mut temp_file = NamedTempFile::new().unwrap(); + writeln!( + temp_file, + "port: 8080\ncache_dir: /tmp\nmodel: 7b-open-chat-3.5" + ) + .unwrap(); + + let config_path = temp_file.path().to_str().unwrap(); + let config = load_config(config_path).unwrap(); + + assert_eq!(config.port, 8080); + assert_eq!(config.cache_dir, Some(PathBuf::from("/tmp"))); + assert_eq!(config.model, Models::OpenChat35); + } +}