1+ //! Configuration management for the Hammerwork CLI.
2+ //!
3+ //! This module handles loading, saving, and managing configuration for the CLI tool.
4+ //! Configuration can be loaded from multiple sources with the following precedence:
5+ //!
6+ //! 1. Environment variables (highest priority)
7+ //! 2. Configuration file
8+ //! 3. Default values (lowest priority)
9+ //!
10+ //! # Configuration File Location
11+ //!
12+ //! The configuration file is stored at platform-specific locations:
13+ //!
14+ //! - **Linux/Mac**: `~/.config/hammerwork/config.toml`
15+ //! - **Windows**: `%APPDATA%\hammerwork\config.toml`
16+ //!
17+ //! # Examples
18+ //!
19+ //! ## Loading Configuration
20+ //!
21+ //! ```rust,no_run
22+ //! use cargo_hammerwork::config::Config;
23+ //!
24+ //! // Load configuration from file and environment
25+ //! let config = Config::load().expect("Failed to load config");
26+ //!
27+ //! // Access configuration values
28+ //! if let Some(db_url) = config.get_database_url() {
29+ //! println!("Database URL: {}", db_url);
30+ //! }
31+ //!
32+ //! println!("Default queue: {:?}", config.get_default_queue());
33+ //! println!("Default limit: {}", config.get_default_limit());
34+ //! ```
35+ //!
36+ //! ## Creating and Saving Configuration
37+ //!
38+ //! ```rust,no_run
39+ //! use cargo_hammerwork::config::Config;
40+ //!
41+ //! // Create a new configuration with custom values
42+ //! let mut config = Config::default();
43+ //! config.database_url = Some("postgresql://localhost/hammerwork".to_string());
44+ //! config.default_queue = Some("default".to_string());
45+ //! config.default_limit = Some(100);
46+ //! config.log_level = Some("debug".to_string());
47+ //!
48+ //! // Save configuration to file
49+ //! config.save().expect("Failed to save config");
50+ //! ```
51+ //!
52+ //! ## Environment Variable Override
53+ //!
54+ //! ```rust,no_run
55+ //! use std::env;
56+ //! use cargo_hammerwork::config::Config;
57+ //!
58+ //! // Set environment variables (unsafe in real code, safe for docs)
59+ //! unsafe {
60+ //! env::set_var("DATABASE_URL", "postgresql://prod-server/hammerwork");
61+ //! env::set_var("HAMMERWORK_DEFAULT_QUEUE", "high-priority");
62+ //! env::set_var("HAMMERWORK_LOG_LEVEL", "warn");
63+ //! }
64+ //!
65+ //! // Load config - environment variables take precedence
66+ //! let config = Config::load().expect("Failed to load config");
67+ //!
68+ //! assert_eq!(config.get_database_url(), Some("postgresql://prod-server/hammerwork"));
69+ //! assert_eq!(config.get_default_queue(), Some("high-priority"));
70+ //! assert_eq!(config.get_log_level(), "warn");
71+ //! ```
72+ //!
73+ //! ## Configuration File Format
74+ //!
75+ //! The configuration file uses TOML format:
76+ //!
77+ //! ```toml
78+ //! database_url = "postgresql://localhost/hammerwork"
79+ //! default_queue = "default"
80+ //! default_limit = 50
81+ //! log_level = "info"
82+ //! connection_pool_size = 5
83+ //! ```
84+ //!
85+ //! # Environment Variables
86+ //!
87+ //! The following environment variables are supported:
88+ //!
89+ //! - `DATABASE_URL` - Database connection URL
90+ //! - `HAMMERWORK_DEFAULT_QUEUE` - Default queue name
91+ //! - `HAMMERWORK_DEFAULT_LIMIT` - Default limit for list operations
92+ //! - `HAMMERWORK_LOG_LEVEL` - Logging level (error, warn, info, debug, trace)
93+ //! - `HAMMERWORK_POOL_SIZE` - Database connection pool size
94+
195use anyhow:: Result ;
296use serde:: { Deserialize , Serialize } ;
397use std:: env;
498use std:: fs;
599use std:: path:: PathBuf ;
6100
101+ /// CLI configuration structure.
102+ ///
103+ /// This struct holds all configuration options for the Hammerwork CLI.
104+ /// Fields are optional to allow partial configuration and merging from multiple sources.
105+ ///
106+ /// # Examples
107+ ///
108+ /// ```rust
109+ /// use cargo_hammerwork::config::Config;
110+ ///
111+ /// // Create configuration with defaults
112+ /// let config = Config::default();
113+ /// assert_eq!(config.get_default_limit(), 50);
114+ /// assert_eq!(config.get_log_level(), "info");
115+ /// assert_eq!(config.get_connection_pool_size(), 5);
116+ /// ```
7117#[ derive( Debug , Clone , Serialize , Deserialize ) ]
8118pub struct Config {
119+ /// Database connection URL (e.g., "postgresql://localhost/hammerwork")
9120 pub database_url : Option < String > ,
121+
122+ /// Default queue name for operations
10123 pub default_queue : Option < String > ,
124+
125+ /// Default limit for list operations
11126 pub default_limit : Option < u32 > ,
127+
128+ /// Log level (error, warn, info, debug, trace)
12129 pub log_level : Option < String > ,
130+
131+ /// Database connection pool size
13132 pub connection_pool_size : Option < u32 > ,
14133}
15134
@@ -26,6 +145,21 @@ impl Default for Config {
26145}
27146
28147impl Config {
148+ /// Load configuration from file and environment variables.
149+ ///
150+ /// Configuration is loaded with the following precedence:
151+ /// 1. Environment variables (highest priority)
152+ /// 2. Configuration file at `~/.config/hammerwork/config.toml`
153+ /// 3. Default values (lowest priority)
154+ ///
155+ /// # Examples
156+ ///
157+ /// ```rust,no_run
158+ /// use cargo_hammerwork::config::Config;
159+ ///
160+ /// let config = Config::load().expect("Failed to load configuration");
161+ /// println!("Database URL: {:?}", config.database_url);
162+ /// ```
29163 pub fn load ( ) -> Result < Self > {
30164 let mut config = Self :: default ( ) ;
31165
@@ -69,6 +203,21 @@ impl Config {
69203 Ok ( config)
70204 }
71205
206+ /// Save configuration to file.
207+ ///
208+ /// The configuration is saved to the platform-specific location:
209+ /// - Linux/Mac: `~/.config/hammerwork/config.toml`
210+ /// - Windows: `%APPDATA%\hammerwork\config.toml`
211+ ///
212+ /// # Examples
213+ ///
214+ /// ```rust,no_run
215+ /// use cargo_hammerwork::config::Config;
216+ ///
217+ /// let mut config = Config::default();
218+ /// config.database_url = Some("postgresql://localhost/hammerwork".to_string());
219+ /// config.save().expect("Failed to save configuration");
220+ /// ```
72221 pub fn save ( & self ) -> Result < ( ) > {
73222 let config_path = Self :: config_file_path ( ) ?;
74223
0 commit comments