Maintain/Run/Fn.rs
1//=============================================================================//
2// File Path: Element/Maintain/Source/Run/Fn.rs
3//=============================================================================//
4// Module: Fn
5//
6// Brief Description: The main entry point for the run orchestrator.
7//
8// RESPONSIBILITIES:
9// ================
10//
11// Primary:
12// - Initialize the logger
13// - Parse command-line arguments
14// - Execute the run orchestration process
15// - Handle success and error conditions appropriately
16//
17// Secondary:
18// - Provide a clean interface for calling the run system
19// - Exit with appropriate status codes
20//
21// ARCHITECTURAL ROLE:
22// ===================
23//
24// Position:
25// - Interface/Entry point layer
26// - Main public API
27//
28// Dependencies (What this module requires):
29// - External crates: log
30// - Internal modules: Definition::Argument, Process, Logger
31// - Traits implemented: None
32//
33// Dependents (What depends on this module):
34// - Application entry point (main function)
35//
36// IMPLEMENTATION DETAILS:
37// =======================
38//
39// Design Patterns:
40// - Entry point pattern
41// - Early exit on error pattern
42//
43// Performance Considerations:
44// - Complexity: O(1) - delegates to Process function
45// - Memory usage patterns: Minimal overhead
46// - Hot path optimizations: None
47//
48// Thread Safety:
49// - Thread-safe: No (called only once at program start)
50// - Synchronization mechanisms used: None
51//
52// Error Handling:
53// - Error types returned: None (logs and exits)
54// - Recovery strategies: Exit with status code 1 on error
55//
56// EXAMPLES:
57// =========
58//
59// Example 1: Direct invocation
60/// ```rust
61/// use crate::Maintain::Source::Run::Fn;
62/// Fn();
63/// ```
64//
65// Example 2: Usage in main function
66/// ```rust
67/// fn main() {
68/// crate::Maintain::Source::Run::Fn();
69/// }
70/// ```
71//
72//=============================================================================//
73// IMPLEMENTATION
74//=============================================================================//
75
76use crate::Run::Definition::Argument;
77use crate::Run::Process;
78use crate::Run::Logger;
79
80use clap::Parser;
81use log::{error, info};
82
83/// The main entry point of the run binary.
84///
85/// This function serves as the primary entry point for the run orchestrator.
86/// It performs three main steps:
87///
88/// 1. **Initialization**: Sets up the global logger with appropriate formatting
89/// and log level based on the `RUST_LOG` environment variable.
90///
91/// 2. **Argument Parsing**: Parses command-line arguments and environment
92/// variables using the `Argument` struct, which includes automatic validation
93/// and help text generation.
94///
95/// 3. **Orchestration**: Delegates to the `Process` function to execute the
96/// complete run orchestration workflow, including:
97/// - Validating project directory and configuration files
98/// - Resolving environment variables from profiles
99/// - Starting the development server with hot-reload
100/// - Managing watch mode for file changes
101/// - Executing the run command
102/// - Handling graceful shutdown
103///
104/// # Behavior
105///
106/// On successful run completion:
107/// - Logs an informational message: "Run process completed successfully."
108/// - Exits with status code 0 (implicit)
109///
110/// On run failure:
111/// - Logs an error message with the error details
112/// - Exits with status code 1 via `std::process::exit(1)`
113///
114/// # Example Invocation
115///
116/// Basic run with debug profile:
117/// ```sh
118/// cargo run --bin Maintain -- --run --profile debug
119/// ```
120///
121/// Run with hot-reload disabled:
122/// ```sh
123/// cargo run --bin Maintain -- --run --profile debug --no-hot-reload
124/// ```
125///
126/// Run with custom workbench:
127/// ```sh
128/// cargo run --bin Maintain -- --run --profile debug --workbench Wind
129/// ```
130///
131/// # Error Handling
132///
133/// The function uses Rust's question mark operator (`?`) to propagate errors
134/// from the `Process` function. When an error occurs:
135///
136/// 1. The error is logged with `error!()` macro
137/// 2. The program exits with status code 1
138///
139/// This ensures that:
140/// - Run failures are clearly reported
141/// - The workspace is left in a consistent state
142///
143/// # Logging
144///
145/// The function requires the logger to be initialized first (via `Logger()`).
146/// All subsequent operations use structured logging with targets:
147///
148/// - `Run` - General run orchestration messages
149/// - `Run::Process` - Process management messages
150/// - `Run::Environment` - Environment variable resolution
151/// - `Run::HotReload` - Hot-reload related messages
152///
153/// Control log verbosity with `RUST_LOG`:
154/// ```sh
155/// export RUST_LOG=debug # More verbose output
156/// export RUST_LOG=error # Only errors
157/// ```
158///
159/// # Thread Safety
160///
161/// This function is not designed to be called concurrently. It should be
162/// called exactly once at program startup. The underlying `Process` function
163/// and logging infrastructure handle their own synchronization requirements.
164///
165/// # Implementation Notes
166///
167/// The function name `Fn` is intentionally short to serve as a concise
168/// entry point, following the naming convention of the Build module.
169///
170/// The function does not return a value because it either completes
171/// successfully (and the program exits) or exits with an error status.
172/// This pattern is common for Rust binaries that serve as command-line
173/// tools or development scripts.
174pub fn Fn() {
175 // Step 1: Initialize the logger with colored output
176 Logger::Logger();
177
178 // Step 2: Parse command-line arguments and environment variables
179 let argument = Argument::parse();
180
181 log::debug!("Parsed arguments: {:?}", argument);
182
183 // Step 3: Execute the run orchestration process
184 match Process::Process(&argument) {
185 Ok(_) => info!("Run process completed successfully."),
186
187 Err(e) => {
188 error!("Run process failed: {}", e);
189
190 std::process::exit(1);
191 },
192 }
193}
194
195#[cfg(test)]
196mod tests {
197 use super::*;
198
199 // Note: Actual tests require integration test setup
200 // because Fn() initializes the logger and processes arguments.
201 // Unit tests should focus on the underlying Process function
202 // instead.
203
204 #[test]
205 fn test_fn_exists() {
206 // Verify the function compiles and is callable
207 // (actual execution would be integration tests)
208 let _ = Fn as fn();
209 }
210}