Skip to main content

meal_planner/
config.rs

1//! Runtime configuration for the Meal Planner module.
2
3#[cfg(test)]
4mod tests;
5
6use std::path::{Path, PathBuf};
7
8use crate::MealPlannerError;
9
10/// Configuration shared by the Meal Planner application and its adapters.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct MealPlannerConfig {
13    database_path: PathBuf,
14}
15
16impl MealPlannerConfig {
17    /// Build configuration using the default database location in the server data directory.
18    pub fn from_data_dir(data_dir: impl AsRef<Path>) -> Self {
19        Self {
20            database_path: data_dir.as_ref().join("meal-planner.sqlite"),
21        }
22    }
23
24    /// Build configuration with an explicit SQLite database path.
25    pub fn new(database_path: impl Into<PathBuf>) -> Result<Self, MealPlannerError> {
26        let database_path = database_path.into();
27        if database_path.as_os_str().is_empty() {
28            return Err(MealPlannerError::InvalidConfiguration(
29                "database path must not be empty".into(),
30            ));
31        }
32        Ok(Self { database_path })
33    }
34
35    /// Return the configured SQLite database path.
36    pub fn database_path(&self) -> &Path {
37        &self.database_path
38    }
39}