rush_sync_server/
lib.rs

1// =====================================================
2// FILE: src/lib.rs - OPTIMIERTE VERSION
3// =====================================================
4
5#[macro_export]
6macro_rules! impl_default {
7    ($type:ty, $body:expr) => {
8        impl Default for $type {
9            fn default() -> Self {
10                $body
11            }
12        }
13    };
14}
15
16#[macro_export]
17macro_rules! matches_exact {
18    ($cmd:expr, $($pattern:literal)|+) => {
19        matches!($cmd.trim().to_lowercase().as_str(), $($pattern)|+)
20    };
21}
22
23// Module definitions
24pub mod commands;
25pub mod core;
26pub mod i18n;
27pub mod input;
28pub mod output;
29pub mod proxy;
30pub mod server;
31pub mod setup;
32pub mod ui;
33
34// Essential re-exports
35pub use commands::{Command, CommandHandler, CommandRegistry};
36pub use core::config::Config;
37pub use core::error::{AppError, Result};
38pub use ui::screen::ScreenManager;
39
40// ✅ OPTIMIERT: Lazy static für Registry - nur einmal erstellen
41use std::sync::OnceLock;
42static DEFAULT_REGISTRY: OnceLock<CommandRegistry> = OnceLock::new();
43
44pub fn create_default_registry() -> CommandRegistry {
45    build_registry()
46}
47
48// ✅ EXTRACTED: Registry-Building separiert für bessere Testbarkeit
49fn build_registry() -> CommandRegistry {
50    use commands::{
51        cleanup::CleanupCommand, clear::ClearCommand, create::CreateCommand, exit::ExitCommand,
52        help::HelpCommand, history::HistoryCommand, lang::LanguageCommand, list::ListCommand,
53        log_level::LogLevelCommand, recovery::RecoveryCommand, restart::RestartCommand,
54        start::StartCommand, stop::StopCommand, theme::ThemeCommand, version::VersionCommand,
55    };
56
57    let mut registry = CommandRegistry::new();
58
59    // ✅ OPTIMIERT: Functional-Style Chain für kompakteren Code
60    registry
61        // Core Commands
62        .register(HelpCommand::new())
63        .register(VersionCommand)
64        .register(ClearCommand)
65        .register(ExitCommand)
66        .register(RestartCommand)
67        // Configuration Commands
68        .register(LogLevelCommand)
69        .register(LanguageCommand::new())
70        .register(ThemeCommand::new())
71        // Utility Commands
72        .register(HistoryCommand)
73        .register(RecoveryCommand::new())
74        // Server Commands
75        .register(CleanupCommand::new())
76        .register(CreateCommand::new())
77        .register(ListCommand::new())
78        .register(StartCommand::new())
79        .register(StopCommand::new());
80
81    registry
82}
83
84// ✅ VEREINFACHT: Main entry point
85pub async fn run() -> Result<()> {
86    let config = Config::load().await?;
87    run_with_config(config).await
88}
89
90// ✅ OPTIMIERT: Direkte Implementierung ohne Duplikation
91pub async fn run_with_config(config: Config) -> Result<()> {
92    let mut screen = ScreenManager::new(&config).await?;
93    screen.run().await
94}
95
96// ✅ VEREINFACHT: Convenience functions
97pub fn create_handler() -> CommandHandler {
98    CommandHandler::new()
99}
100
101pub async fn load_config() -> Result<Config> {
102    Config::load().await
103}
104
105// ✅ NEU: Registry-Testing Helper (für Unit Tests)
106#[cfg(test)]
107pub fn create_test_registry() -> CommandRegistry {
108    // Für Tests immer fresh Registry ohne static caching
109    build_registry()
110}
111
112// ✅ NEU: Command-Count für Debugging
113pub fn get_command_count() -> usize {
114    DEFAULT_REGISTRY.get().map(|r| r.len()).unwrap_or(0)
115}