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        history::HistoryCommand, lang::LanguageCommand, list::ListCommand,
53        log_level::LogLevelCommand, restart::RestartCommand, start::StartCommand,
54        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(VersionCommand)
63        .register(ClearCommand)
64        .register(ExitCommand)
65        .register(RestartCommand)
66        // Configuration Commands
67        .register(LogLevelCommand)
68        .register(LanguageCommand::new())
69        .register(ThemeCommand::new())
70        // Utility Commands
71        .register(HistoryCommand)
72        // Server Commands
73        .register(CleanupCommand::new())
74        .register(CreateCommand::new())
75        .register(ListCommand::new())
76        .register(StartCommand::new())
77        .register(StopCommand::new());
78
79    registry
80}
81
82// ✅ VEREINFACHT: Main entry point
83pub async fn run() -> Result<()> {
84    let config = Config::load().await?;
85    run_with_config(config).await
86}
87
88// ✅ OPTIMIERT: Direkte Implementierung ohne Duplikation
89pub async fn run_with_config(config: Config) -> Result<()> {
90    let mut screen = ScreenManager::new(&config).await?;
91    screen.run().await
92}
93
94// ✅ VEREINFACHT: Convenience functions
95pub fn create_handler() -> CommandHandler {
96    CommandHandler::new()
97}
98
99pub async fn load_config() -> Result<Config> {
100    Config::load().await
101}
102
103// ✅ NEU: Registry-Testing Helper (für Unit Tests)
104#[cfg(test)]
105pub fn create_test_registry() -> CommandRegistry {
106    // Für Tests immer fresh Registry ohne static caching
107    build_registry()
108}
109
110// ✅ NEU: Command-Count für Debugging
111pub fn get_command_count() -> usize {
112    DEFAULT_REGISTRY.get().map(|r| r.len()).unwrap_or(0)
113}