hyprstream_core/cli/context.rs
1//! Application context for CLI commands
2//!
3//! This module provides the Context pattern for dependency injection,
4//! allowing configuration and the registry client to be passed to command handlers
5//! in an idiomatic Rust way.
6
7use crate::config::HyprConfig;
8use hyprstream_rpc_std::registry_client::RegistryClient;
9use std::sync::Arc;
10
11/// Application context passed to all command handlers
12///
13/// This follows the "Context Pattern" common in Rust CLI applications.
14/// Configuration is immutable after creation, and the registry client
15/// is shared across all handlers.
16///
17/// # Example
18///
19/// ```rust,ignore
20/// use hyprstream_rpc_std::registry_client::RegistryClient;
21///
22/// // Start registry service ONCE at CLI level
23/// let client: RegistryClient = RegistryClient::new(
24/// signing_key, identity,
25/// );
26/// let context = AppContext::new(config, client);
27///
28/// // Pass to handlers - all share the same registry
29/// handle_server(context.clone()).await?;
30/// ```
31#[derive(Clone)]
32pub struct AppContext {
33 /// Shared configuration (immutable after creation)
34 config: Arc<HyprConfig>,
35
36 /// Shared registry client for all git operations
37 registry: RegistryClient,
38}
39
40impl AppContext {
41 /// Create new context with configuration and registry client.
42 ///
43 /// The registry client should be started once in main() and shared
44 /// across all components.
45 pub fn new(config: HyprConfig, registry: RegistryClient) -> Self {
46 Self {
47 config: Arc::new(config),
48 registry,
49 }
50 }
51
52 /// Alias for `new()` - maintained for backward compatibility.
53 pub fn with_client(config: HyprConfig, client: RegistryClient) -> Self {
54 Self::new(config, client)
55 }
56
57 /// Get configuration reference
58 pub fn config(&self) -> &HyprConfig {
59 &self.config
60 }
61
62 /// Get the registry client
63 pub fn registry(&self) -> &RegistryClient {
64 &self.registry
65 }
66
67 /// Get the models directory path
68 pub fn models_dir(&self) -> &std::path::Path {
69 self.config.models_dir()
70 }
71
72 /// Get the config directory path
73 pub fn config_dir(&self) -> &std::path::Path {
74 self.config.config_dir()
75 }
76}